I am having trouble creating a parse tree for the expression:
4 + (5 * 7). I made one for the expression 4 + 5 * 7 but now the parentheses are throwing me off. Here is what I have for the first parse tree:
But like I said the expression 4 + (5 * 7) is throwing me off due to the parentheses. The grammar rules are:
Any help is appreciated, thanks in advance.
Related
I am writing a commandline calculator which, if I don't fudge it, will have fancy things like natural expression evaluation. I want an efficient method of splitting the input expressions into their components for easy evaluation, regardless of if tokens are separated by spaces or not.
even expressions like this should be processable-
2x^5 + 6d - h
For example,
2x^5+2y^4-62
would be split into
2
*
x
^
5
+
2
*
y
^
4
-
62
and then will be evaluated. I made an attempt at this, but it is very messy and ultimately doesn't work. Please give me a few hints on how to tokenise my input with the stl.
In a coding problem I've been working on for some time now, I've come to a step where I have to evaluate a mathematical expression that looks like this :
3 * 2 ^ 3 ^ 2 * 5
and should be evaluated like this :
3 * 2 ^ 3 ^ 2 * 5 = 3 * 2^(3 * 2) * 5 = 3 * 64 * 5 = 960.
In the current form of my implementation, I have two vectors, one contains the operands as integers, while the other one contains the operators as chars.
For the current case, they would be : vector<int> operands = { 3, 2, 3, 2, 5 } and vector<char> operators = { '*', '^', '^', '*' }.
This is just a sample case, the order of operations may differ in the sense that multiplication might not always be the first/last operation to be performed.
I've been stuck at this particular step for a while now, namely evaluating the expression encapsulated by the two vector containers to an integer. I've looked at some mathematical parsers I could find on the web, but I still don't see how to implement a proper evaluation.
A solution would be very much appreciated.
Simply compute the value as you parse the expression, maintaining one variable for the final product and one for the current multiplicand (i.e. the current group of exponents with the corresponding base). Apply each exponential operand sequentially as you see it, thus performing left-associative exponentiation.
As an aside, I wouldn't bother storing the entire expression in some kind of vectorized format; I see no useful reason for doing so.
What you would like is possible with expression templates. They make it possible to evaluate expressions in non-standard order and/or behavior - using them you can also define multiple meaning for the same operator in an expression.
This question already has answers here:
Building a math expression evaluator
(2 answers)
Closed 7 years ago.
I'm implementing a calculator in C ++ that respects the priorities of parentheses. I am trying to use std :: string :: find_last_of to find the last occurrence of ( and std :: string :: find to find the first occurrence of ). Once that is done, I reckon the content of the extracted substring and continuing with the rest of the string.
For example:
1 + 1 * (9 * (11-1 + 2))
I find the last occurrence of (, the first of ) and calculating 11-1 + 2. And so it continues.
Is this the right approach to this problem or is there something better?
Thank you.
You can use Reversed Polish Notation:
You will need to convert the expression into reversed polish notation and then implement a stack based algorithm to pop and push to the final answer.
Have a look at the Shunting- Yard algorithm here: To convert the expression into RPN.
https://en.wikipedia.org/wiki/Shunting-yard_algorithm
Also Have a look at
Writing a simple equation parser
For help to implement the stack, have a look here:
C++ Calculator using Stacks and Queues
One of the standard approaches to this problem consists of 2 steps:
1) Convert your expression to Reverse Polish notation: https://en.wikipedia.org/wiki/Reverse_Polish_notation using Shunting-yard algorithm https://en.wikipedia.org/wiki/Shunting-yard_algorithm
2) Evaluate converted expression using stack.
The another way to do this:
1) Easy step: Write Backus–Naur Form of your expressions https://en.wikipedia.org/wiki/Backus%E2%80%93Naur_Form
And after you have 2 options:
2a) Build Finite-State Machine: https://en.wikipedia.org/wiki/Finite-state_machine (exists a lot of tools to do this: ANTLR, ...).
2b) Recursive descent method: https://en.wikipedia.org/wiki/Recursive_descent_parser
I'm looking for something like this: 1* 24 3*
But that doesn't take into account different order like the string 231. Any ideas how to specify the order using regular expressions?
I do not know what your intention was with 1^* 2^4 3^*, but going from your description,
/^([13]*2){0,4}[13]*$/
will match any combination of 1, 2 and 3, but with no more than four of 2.
However, you did not specify the regexp engine, so it might not work in some (notably, those without the {} repetition operator), in which case you will need to unpack it.
EDIT: Having seen tripleee's comment, I finally understood the original attempt. Anyway, in formal regular expression, you definitely need to unpack (not only you don't have {}, you don't even have x? as shorthand for (x|ε)):
(1|3)*
(2 (1|3)*
(2 (1|3)*
(2 (1|3)*
(2 (1|3)*
|ε)
|ε)
|ε)
|ε)
If I understood your requirement correctly you want to allow all combinations of 1,2,3 with restriction that digit 2 shouldn't be there more than 4 times.
You need to use lookahead for this regex:
^(?!(.*?2){5,})[123]+$
Online Demo: http://regex101.com/r/dP2bK3
This question already has answers here:
Validate a Boolean expression with brackets in C#
(6 answers)
Closed 8 years ago.
I have a input text box to enter a logic criteria.
Following are the possible inputs:
1 OR 2
1 AND 2
(1 OR 2) AND 3
(1 OR 2) OR 3
(1 AND 2) AND 3
(1 AND 2) OR 3
1 AND (2 OR 3)
1 OR (2 OR 3)
1 AND (2 AND 3)
1 OR (2 AND 3)
(1 OR 2) AND (3 OR 4)
(1 OR 2) OR (3 OR 4)
(1 AND 2) OR (3 AND 4)
(1 OR 2) AND (3 OR 4) AND (5 OR 6)
Can anyone provide a RegEx to validate such input?
Assuming you can nest parentheses, you cannot do this with straight regular expressions, because you cannot validate arbitrarily nested parentheses with a regular expression.
The more typical way to validate this input is to break the process into two steps. Use a family of regular expressions to tokenize the input, and then use a simple grammar to validate the resulting sequence of tokens. An LALR(1) grammar such as what yacc supports makes this problem trivial.
I think there are some extended regex forms that add the necessary functionality that you could match arbitrarily nested parentheses. I have to admit I'm not readily familiar with any of them, since they quickly get more complicated to use than just writing some looping logic around a much simpler set of matches.
Match just "1 and 2 or 4", no parentheses:
^\d+(?:\s*(?:AND|OR)\s*\d+)*$
Next, instead of each \d+, also allow the same expression, wrapped with parentheses:
^(?:\d+|\(\d+(?:\s*(?:AND|OR)\s*\d+)*\))(?:\s*(?:AND|OR)\s*(?:\d+|\(\d+(?:\s*(?:AND|OR)\s*\d+)*\)))*$
OK - it isn't beautiful, but it works. Obviously, this assume just a single level of parentheses.
As the comments say, depending on your language and requirements you may find a nicer solution.
Working example: http://www.debuggex.com/r/eMBWubl5yAp6hUqQ
You need to parse it with looping with my solution. loop this regex (\({0,1}\d+ (OR|AND) \d+\){0,1}) and repalce all the match value into digit(like 1). until none in the text match (\({0,1}\d+ (OR|AND) \d+\){0,1}). if you have only digit then that text is valid, if not it doesn't valid.