-2

I have tokenized with a space delimiter into a vector and then I have made it into stack. I want to use the stack to validate whether the expression or assignment is correct.

assignment shall have the form: id = exp; expression shall have the form: id op id {op id} -- any length as long as pairs of op and id are added A parenthesis pair may be used to group any id op id combination. Therefore: id op (id op id) op id AND id op id op (id op id) -- valid expressions

  • I do not understand, to be able to put an expression in a stack (following postfix or prefix notation typically) you already validated the expression, and the "( ')' disappeared. Else you 'stack' is not a stack as in an execution but just a collection to memorize the items in the order they was read, without even knowing what each item is. – bruno May 08 '19 at 22:25

1 Answers1

0

You want this :-

https://en.wikipedia.org/wiki/Shunting-yard_algorithm

This will convert your infix expression with brackets to a postfix expression on a stack that you can evaluate. If the shunting fails then the expression is invalid i.e. with appropriate checks you can catch x=3+((-+) as a malformed expression.

Jon Guiton
  • 1,360
  • 1
  • 9
  • 11