small medium large xlarge

20 Apr 2016, 12:49
Bernhard Gschaider (1 post)

If I interpret the printer in Pattern 14 correctly then a AST

(= x (* (+ 1 2) (+ 3 4)))

(which basically is x=(1+2)*(3+4) ) would be printed out as


and I don’t see how that could be parsed back into the original AST.

I understand that to keep the example simple it doesn’t handle the (). But then I’d make the printer write it out in a different form so that nobody gets the idea that its output is valid input for the parser

20 Apr 2016, 16:13
Terence Parr (53 posts)

Hi. yeah, basically you need to surround all subtrees with ( ) in the output or, if you wanna get fancy, check precedence of current tree’s parent op and left/right child op and only put ( ) if needed.

You must be logged in to comment