I’m just learning the Scala parser combinator library. I’ve experimented a working parser that parses some arithmetic expressions with an abstract syntax tree.
So when I call
phrase(expr)(tokens)
and my parser parses all the input then gives me an evaluation. But how can I have a step by step evaluation?
Say
3 + 4 * 7
it prints
3 + 28
then
31
in separate lines.
I’ve scan through the apis but the docs there are not very helpful …
Thanks for help.
Here’s a really simple implementation of what you are trying to do:
First, we define an expression hierarchy. You will need to tailor this to your specific problem.
Then, a function that combines only the bottom-most branch.
Then, a function that combines the tree one level at a time, printing as it goes.
Now, the Parser. Again, you’ll have to tailor this to your data.
And here’s how you use it: