Skip to content

ryanovic/ExpressionParser

Repository files navigation

Description

Sample implementation of operator-precedence expression parser. Parser supports basic operations(addition, comparison, asssigment, etc...), variables(lower case english alphabet) and integers. Expressions are read from the console - row by row. Variables state is shared between expressions:

Please, enter the expression:
a = 3
Result: 3
b = 4
Result: 4
a + b
Result: 7

Implementation notes

  • There are two global states - operator pending and operand pending
  • '+', '++', '-', '--' are treated according to the global state to distinquish postfix and prefix operators.
  • Each operator is handled in two steps. First, it reads and completes all lower or with the same priority operators are currently on the stack, then put the pending one on the top.
  • Right assiciative operators only reduce operators with lower priority to maintain right to left evaluation order.
  • '(' has a special(the lowest) precedence, so it can't be reduced by any other operator execpt ')'.
  • Trinary('?:') operator is handled as a special case. When '?' encountered it reduces the stack according to the general rules(for right associativity operators), then pushed with 'partial'(lowest) priority. Then the following ':' token reduces all operations between '?' and the current point, and replaces former with the expected 'conditional' priority operation.

About

Sample operator-precedence expression parser implementation

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages