Displaying the postfix/prefix expression as a parse tree using C/C++ - c++

I have successfully converted infix expression to postfix expression and was also able to evaluate the postfix expression but I am facing problem in generating a parse tree for the same with C/C++
My output :
enter the expression string a+b*c
the expression is correct
the postfix expression is - abc *+
enter the value of a-1
enter the value of b-2
enter the value of c-3
the postfix expression is -abc*+
result= 7
I also require to display: Syntax tree
+
/ \
* a
/ \
b c
Any feedback would be very helpful in my project.
Thanks in Adv.
#LD: Thanks for your consistent help. I need the pseudocode in turbo C. I don't know Ruby.

It's much easier to "draw" them like the following:
+
a
*
b
c
Or, if you want to use simple character graphics (I've changed the + and * operators to Add and Mul, to avoid clashing with the graphics):
Add
+-- a
+-- Mul
+-- b
+-- c
The trick to doing this is possible draw a subtree in isolation (e.g. the mul tree), and then draw it with suitable prefixes when drawing the outer tree.
In fact, if you are familiar with C++ stream buffers, you could create a prefixing stream buffer that handles the prefixes and simply print the inner tree.
The big difference compared to the style you suggested is that your style simply doesn't scale. If, for example the top operator would have two big subtrees, they would have been drawn extremely far apart.
EDIT: A slightly more complex tree could be drawn like this:
Add
+---Sub
| +---Div
| | +---p
| | +---q
| +---y
+---Mul
+---b
+---c
EDIT: On request, here comes some pseudo-code (which, incidentally, is acceptable by a Ruby interpreter). You must, however, use a suitable C++ data structure to represent the tree.
# Return the drawn tree as an array of lines.
#
# node ::= string
# node ::= [string, node, node]
def render_tree(node, prefix0 = "", prefix = "")
if (node.is_a?(String))
puts prefix0 + node # Value
else
puts prefix0 + node[0] # Operator
render_tree(node[1], prefix + "+---", prefix + "| ")
render_tree(node[2], prefix + "+---", prefix + " ")
end
end
render_tree(["Add", ["Sub", ["Div", "p", "q"], "y"], ["Mul", "b", "c"]])

Related

(Ocaml) Using 'match' to extract list of chars from a list of chars

I have just started to learn ocaml and I find it difficult to extract small list of chars from a bigger list of chars.
lets say I have:
let list_of_chars = ['#' ; 'a' ; 'b' ; 'c'; ... ; '!' ; '3' ; '4' ; '5' ];;
I have the following knowledge - I know that in the
list above I have '#' followed by a '!' in some location further in the list .
I want to extract the lists ['a' ;'b' ;'c' ; ...] and ['3' ; '4' ; '5'] and do something with them,
so I do the following thing:
let variable = match list_of_chars with
| '#'::l1#['!']#l2 -> (*[code to do something with l1 and l2]*)
| _ -> raise Exception ;;
This code doesn't work for me, it's throwing errors. Is there a simple way of doing this?
(specifically for using match)
As another answer points out, you can’t use pattern matching for this because pattern matching only lets you use constructors and # is not a constructor.
Here is how you might solve your problem
let split ~equal ~on list =
let rec go acc = function
| [] -> None
| x::xs -> if equal x on then Some (rev acc, xs) else go (x::acc) xs
in
go [] list
let variable = match list_of_chars with
| '#'::rest ->
match split rest ~on:'!' ~equal:(Char.equal) with
| None -> raise Exception
| Some (left,right) ->
... (* your code here *)
I’m now going to hypothesise that you are trying to do some kind of parsing or lexing. I recommend that you do not do it with a list of chars. Indeed I think there is almost never a reason to have a list of chars in ocaml: a string is better for a string (a chat list has an overhead of 23x in memory usage) and while one might use chars as a kind of mnemonic enum in C, ocaml has actual enums (aka variant types or sum types) so those should usually be used instead. I guess you might end up with a chat list if you are doing something with a trie.
If you are interested in parsing or lexing, you may want to look into:
Ocamllex and ocamlyacc
Sedlex
Angstrom or another parser generator like it
One of the regular expression libraries (eg Re, Re2, Pcre (note Re and Re2 are mostly unrelated)
Using strings and functions like lsplit2
# is an operator, not a valid pattern. Patterns need to be static and can't match a varying number of elements in the middle of a list. But since you know the position of ! it doesn't need to be dynamic. You can accomplish it just using :::
let variable = match list_of_chars with
| '#'::a::b::c::'!'::l2 -> let l1 = [a;b;c] in ...
| _ -> raise Exception ;;

Is there a way to usefully index a text column containing regex patterns?

I'm using PostgreSQL, currently version 9.2 but I'm open to upgrading.
In one of my tables, I have a column of type text that stores regex patterns.
CREATE TABLE foo (
id serial,
pattern text,
PRIMARY KEY(id)
);
CREATE INDEX foo_pattern_idx ON foo(pattern);
Then I do queries on it like this:
INSERT INTO foo (pattern) VALUES ('^abc.*$');
SELECT * FROM foo WHERE 'abc literal string' ~ pattern;
I understand that this is sort of a reverse LIKE or reverse pattern match. If it was the other, more common way, if my haystack was in the database, and my needle was anchored, I could use a btree index more or less effectively depending on the exact search pattern and data.
But the data that I have is a table of patterns and other data associated with the patterns. I need to ask the database which rows have patterns that match my query text. Is there a way to make this more efficient than a sequential scan that checks every row in my table?
There is no way.
Indexes require IMMUTABLE expressions. The result of your expression depends on the input string. I don't see any other way than to evaluate the expression for every row, meaning a sequential scan.
Related answer with more details for the IMMUTABLE angle:
Does PostgreSQL support "accent insensitive" collations?
Just that there is no workaround for your case, which is impossible to index. The index needs to store constant values in its tuples, which is just not available because the resulting value for every row is computed based on the input. And you cannot transform the input without looking at the column value.
Postgres index usage is bound to operators and only indexes on expressions left of the operator can be used (due to the same logical restraints). More:
Can PostgreSQL index array columns?
Many operators define a COMMUTATOR which allows the query planner / optimizer to flip the indexed expressions to the left. Simple example: The commutator of = is =. the commutator of > is < and vice versa. The documentation:
the index-scan machinery expects to see the indexed column on the left of the operator it is given.
The regular expression match operator ~ has no commutator, again, because that's not possible. See for yourself:
SELECT oprname, oprright::regtype, oprleft::regtype, oprcom
FROM pg_operator
WHERE oprname = '~'
AND 'text'::regtype IN (oprright, oprleft);
oprname | oprright | oprleft | oprcom
---------+----------+-----------+------------
~ | text | name | 0
~ | text | text | 0
~ | text | character | 0
~ | text | citext | 0
And consult the manual here:
oprcom ... Commutator of this operator, if any
...
Unused column contain zeroes. For example, oprleft is zero for a prefix operator.
I have tried before and had to accept it's impossible on principal.

Bison: Shift Reduce Conflict

I believe I am having trouble understanding how shift reduce conflicts work. I understand that bison can look ahead by one, so I don't understand why I am having the issue.
In my language a List is defined as a set of numbers or lists between [ ].
For example [] [1] [1 2] [1 [2] 3] are all valid lists.
Here are the definitions that are causing problems
value: num
| stringValue
| list
;
list: LEFTBRACE RIGHTBRACE
| LEFTBRACE list RIGHTBRACE
| num list
| RIGHTBRACE
;
The conflict happens from the number, it doesn't know weather to shift by the list rule, or reduce by the value rule. I am confused because can't it check to see if a list is following the number?
Any incite on how I should proceed would be greatly appreciated.
I think I'd define things differently, in a way that avoids the problem to start with, something like:
value: num
| stringvalue
| list
;
items:
| items value
;
list: LEFTBRACE items RIGHTBRACE;
Edit: Separating lists of numbers from lists of strings can't be done cleanly unless you eliminate empty lists. The problem that arises is that you want to allow an empty list to be included in a list of numbers or a list of strings, but looking at the empty list itself doesn't let the parser decide which. For example:
[ [][][][][][][][] 1 ]
To figure out what kind of list this is, the parser would have to look ahead all the way to the 1 -- but an LALR(N) parser can only lookahead N symbols to make that decision. Yacc (and Byacc, Bison, etc.) only do LALR(1), so they can only look ahead one symbol. That leaves a few possibilities:
eliminate the possibility of empty lists entirely
Have the lexer treat an arbitrary number of consecutive empty lists as a single token
use a parser generator that isn't limited to LALR(1) grammars
Inside of a yacc grammar, however, I don't think there's much you can do -- your grammar simply doesn't fit yacc's limitations.
With a bottom up parser it is generally a good idea to avoid right recursion which is what you have in the starred line of the grammar below.
list: LEFTBRACE RIGHTBRACE
| LEFTBRACE list RIGHTBRACE
**| num list**
| RIGHTBRACE
Instead have you thought about something like this?
value:value num
| value string
| value list
| num
| string
| list
list: LEFTBRACE RIGHTBRACE
| LEFTBRACE value RIGHTBRACE
This way you have no right recursion and the nesting logic of the grammar is more simply expressed.

How can I use a regular expression to match something in the form 'stuff=foo' 'stuff' = 'stuff' 'more stuff'

I need a regexp to match something like this,
'text' | 'text' | ... | 'text'(~text) = 'text' | 'text' | ... | 'text'
I just want to divide it up into two sections, the part on the left of the equals sign and the part on the right. Any of the 'text' entries can have "=" between the ' characters though. I was thinking of trying to match an even number of 's followed by a =, but I'm not sure how to match an even number of something.. Also note I don't know how many entries on either side there could be. A couple examples,
'51NL9637X33' | 'ISL6262ACRZ-T' | 'QFN'(~51NL9637X33) = '51NL9637X33' | 'ISL6262ACRZ-T' | 'INTERSIL' | 'QFN7SQ-HT1_P49' | '()'
Should extract,
'51NL9637X33' | 'ISL6262ACRZ-T' | 'QFN'(~51NL9637X33)
and,
'51NL9637X33' | 'ISL6262ACRZ-T' | 'INTERSIL' | 'QFN7SQ-HT1_P49' | '()'
'227637' | 'SMTU2032_1' | 'SKT W/BAT'(~227637) = '227637' | 'SMTU2032_1' | 'RENATA' | 'SKT28_5X16_1-HT5_4_P2' | '()' :SPECIAL_A ='BAT_CR2032', PART_NUM_A='202649'
Should extract,
'227637' | 'SMTU2032_1' | 'SKT W/BAT'(~227637)
and,
'227637' | 'SMTU2032_1' | 'RENATA' | 'SKT28_5X16_1-HT5_4_P2' | '()' :SPECIAL_A ='BAT_CR2032', PART_NUM_A='202649'
Also note the little tilda bit at the end of the first section is optional, so I can't just look for that.
Actually I wouldn't use a regex for that at all. Assuming your language has a split operation, I'd first split on the | character to get a list of:
'51NL9637X33'
'ISL6262ACRZ-T'
'QFN'(~51NL9637X33) = '51NL9637X33'
'ISL6262ACRZ-T'
'INTERSIL'
'QFN7SQ-HT1_P49'
'()'
Then I'd split each of them on the = character to get the key and (optional) value:
'51NL9637X33' <null>
'ISL6262ACRZ-T' <null>
'QFN'(~51NL9637X33) '51NL9637X33'
'ISL6262ACRZ-T' <null>
'INTERSIL' <null>
'QFN7SQ-HT1_P49' <null>
'()' <null>
You haven't specified why you think a regex is the right tool for the job but most modern languages also have a split capability and regexes aren't necessarily the answer to every requirement.
I agree with paxdiablo in that regular expressions might not be the most suitable tool for this task, depending on the language you are working with.
The question "How do I match an even number of characters?" is interesting nonetheless, and here is how I'd do it in your case:
(?:'[^']*'|[^=])*(?==)
This expression matches the left part of your entry by looking for a ' at its current position. If it finds one, it runs forward to the next ' and thereby only matching an even number of quotes. If it does not find a ' it matches anything that is not an equal sign and then assures that an equal sign follows the matched string. It works because the regex engine evaluates OR constructs from left to right.
You could get the left and right parts in two capturing groups by using
((?:'[^']*'|[^=])*)=(.*)
I recommend http://gskinner.com/RegExr/ for tinkering with regular expressions. =)
As paxdiablo said, you almost certainly don't want to use a regex here. The split suggestion isn't bad; I myself would probably use a parser here—there's a lot of structure to exploit. The idea here is that you formally specify the syntax of what you have—sort of like what you gave us, only rigorous. So, for instance: a field is a sequence of non-single-quote characters surrounded by single quotes; a fields is any number of fields separated by white space, a |, and more white space; a tilde is non-right-parenthesis characters surrounded by (~ and ); and an expr is a fields, optional whitespace, an optional tilde, a =, optional whitespace, and another fields. How you express this depends on the language you are using. In Haskell, for instance, using the Parsec library, you write each of those parsers as follows:
import Text.ParserCombinators.Parsec
field :: Parser String
field = between (char '\'') (char '\'') $ many (noneOf "'\n")
tilde :: Parser String
tilde = between (string "(~") (char ')') $ many (noneOf ")\n")
fields :: Parser [String]
fields = field `sepBy` (try $ spaces >> char '|' >> spaces)
expr :: Parser ([String],Maybe String,[String])
expr = do left <- fields
spaces
opt <- optionMaybe tilde
spaces >> char '=' >> spaces
right <- fields
(char '\n' >> return ()) <|> eof
return (left, opt, right)
Understanding precisely how this code works isn't really important; the basic idea is to break down what you're parsing, express it in formal rules, and build it back up out of the smaller components. And for something like this, it'll be much cleaner than a regex.
If you really want a regex, here you go (barely tested):
^\s*('[^']*'((\s*\|\s*)'[^'\n]*')*)?(\(~[^)\n]*\))?\s*=\s*('[^']*'((\s*\|\s*)'[^'\n]*')*)?\s*$
See why I recommend a parser? When I first wrote this, I got at least two things wrong which I picked up (one per test), and there's probably something else. And I didn't insert capturing groups where you wanted them because I wasn't sure where they'd go. Now yes, I could have made this more readable by inserting comments, etc. And after all, regexen have their uses! However, the point is: this is not one of them. Stick with something better.

Validating linear equations with regular expressions?

How can I validate linear equations with regular expressions or is there another way besides using regular expressions. I will use ^ to denote an exponent.
2x + 3 = 8 //This should validate fine
3x + 2y + 4z = 12 //This should validate fine
4x^2 + 2y = 22 //This should not validate because of the power.
4xy + 3y = 45 //This should not validate because of the product of two unknowns.
2/x + 4y = 22 //This should not validate because of the unknown in the denominator
(3/4)x + 3y + 2z = 40 //This should validate fine.
I'd start by writing a definition of a valid linear equation using Backus-Naur notation, with things like:
<integer> := <digit> | <integer> <digit>
<constant> := <integer> | ...
<variable> := <letter>
<term> := <constant> | <variable> | <constant> <variable>
and so on.
There are many ways to turn that into a validator. Having some experience with it, I'd use yacc or bison to write a parser that would only generate a parse tree if the input was a valid linear equation.
You might find regular expressions are too limited to do what you need - I just don't use them enough to know.
The cases you've mentioned are easy:
fail if /[xyz]\s*\^/;
fail if /\/\s*[xyz]/;
fail if /([xyz]\s*){2,}/;
(this is Perl syntax, assuming $_ contains the expression, and fail is whatever it is you do when you want to give up.)
Here you can replace xyz with whatever is a valid expression for one variable.
But in general this will require actual parsing of the expression, which is a job for lex/yacc or something like that, not a regular expression.
For example if "xy" is a legitimate variable name, then of course this all crumbles.