[antlr-interest] various question on tree parsing

Lloyd Dupont ld at galador.net
Sat Jul 21 16:59:10 PDT 2007


I have finished my recognizer (i.e. parser + lexer), tested it with various input, and now I want to do something with the result.
as I build an AST my 1st idea was to build a tree parser.

- here comes the 1st question:

yesterday I read the book about tree grammar, apparently tree grammar cannot output tree.
so how can I do constant reduction with TreeGrammar?
I mean let's say I have the following tree fragment

^(PLUS ID["a"] ^(PLUS INT["3"] INT["4"]))

I would like to simplify it as
^(PLUS ID["a"] INT["7"])

and while a tree grammar "seem" perfect for that, I wonder how to get around the fact it cannot output an other tree?
(maybe modifying the input tree itself? how do I do that?)


- 2nd:
let's say I have this tree instead
^(PLUS INT["3"] ^(PLUS ID["a"] INT["4"]))

it could benefit from the same reduction but it is less obvious, any tip on how to do that?


- 3rd:
I was thinking to do multiple analysis pass on the tree: variable checking, constant reduction, code generation.
and while ANTLR book recommand to do (multiple?) tree parser I'm thinking than generating my own class of tree instead of AST and simply do the work in code with special method in my tree implementation seems easier.
What do you think?

Like the above question: I could call a method "simplify" on my tree and it would recursively check itself for binary expression with 2 constant and simplify them. That solve my problem that tree grammar cannot output tree.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070722/6f353319/attachment.html 


More information about the antlr-interest mailing list