sky wrote:
I want to know if there is a systematic way to generate a tree grammar file from a parser grammar file
You already described a systematic way to do this: copy the parser / production rules in the grammar of the tree and leave the rewrite rules in it. This will probably handle most of your rules, but with other parser rules (using AST's built-in rewrite rules), it might look a little different. Because of this, there is no automatic way to generate a tree grammar.
sky wrote:
PS I read an article that insists that "Manual tree movement is better than tree-like . " Is this reliable information?
Yes it is. Note that Terence Parr (creator of ANTLR) has published an article about the ANTLR wiki itself, so its author (Andy Tripp) raises valid points.
sky wrote:
If so, would it be better if I made a manual walk on a tree than writing an ANTLR tree grammar file?
As Andy mentioned in his conclusion: βThe decision about whether to use theβ Tree Grammar βapproach to translation, and not justβ do it manually, βis a matter of taste." So, if you think that writing a grammar of a tree is too much trouble, go by hand. It is up to you: there is no better way.
sky wrote:
And then, how do I make a manual walker tree with my ANTLR parser grammar file (it does AST using rewrite rules)?
Your parser will create an AST, which by default is of type CommonTree (API-doc) . You can use this tree to get children, parent, marker type, etc .: all you need to manually go through the tree.
EDIT
Please note that in the next version of ANTLR (version 4) it is possible (most likely) to automatically generate a tree walker using a combined or parser grammar.
Cm:
source share