3

I want to write a translator from a DSL to Java using ANTLR. So, I wrote the lexer and the parser using two different grammars. Now I have to write the tree grammar and I want to know which are the best practices (or recommended practices) for obtaining my result. More exactly, I would like to know which are the best ways to do stuff like: enrich the tree with attributes (for example, adding types) and optimizations.

Should I write different tree grammars for identifying types and for optimizations and then call then serially after the parser and before the final code generation tree grammar? Is there another way which is easier to maintain? I also though about manually parsing the tree generated by the parser in order to identify the types. But this is quite to maintain.

Thanks you.

Ion Ionascu
  • 552
  • 1
  • 4
  • 15

1 Answers1

0

There are no real best practices: just common sense and personal preference.

However, it is more logical to separate the adding of certain attributes to nodes from optimization actions (rewrite ^(* 0 ^(...)) to 0) in separate passes over the AST. Don't worry too much about performance: tree walking is pretty fast: most of the time is usually spent during parsing. And with ANTLR 3.2's addition of tree pattern matching, you can write pretty small tree grammars to perform a very specific operation on your AST (easy to maintain!).

Also see this previous Q&A that is about manually walking the AST or using a tree grammar for it: Systematic way to generate ANTLR tree grammar?

Community
  • 1
  • 1
Bart Kiers
  • 166,582
  • 36
  • 299
  • 288