Search code examples
javaparsingexpressionshunting-yard

Understanding absolute values in expression parsing to binary tree?


I am writing a parser for expressions, which include tokens ),(,|,*,+,-,/, variables and constants.

So far my code works with *,/,-,+,(,), but I do not know how to deal with absolute values.

With parentheses, I start with ( and end with ), which is very simple, but how do I determine which "|" is an opening and which is a closing one?

For parsing I use two stacks and the shunting yard algorithm (or at least the way I understood and wrote it).


Solution

  • Some context is needed. If the previous symbol is nothing or one of *,/,-,+,(, the | is opening; otherwise, it is closing.