0

For a programming project, I am tasked with taking a set of ANTLR grammar rules for Java and extending them such that they also contain AST rules for the Eclipse JDT API DOM.

For example:

param
    :   type ID
    ;

Would become:

param returns [SingleVariableDeclaration result = ast.newSingleVariableDeclaration()]
:   paramType=type     { result.setType($paramType.result); }
    ID                 { result.setName(ast.newSimpleName($ID.text)); }
;

The first part of the project was creating the grammer rules themselves, and that wasn't too bad, but this part is really throwing me for a loop. Are there any useful resources, examples, or pointers someone could give me as far as adding the AST rules are concerned?

One of the tips I was given was to use the AST viewer in Eclipse to help pinpoint which parts of the API to look at in the Eclipse documentation, but I'm not sure how this helps.

Some of the rules I need to implement yet are array access, for loops, and so on.

Thanks!

The Whether Man
  • 362
  • 5
  • 21
  • Cluttering your grammar isn't a good way to perform a CST to AST translation. You should use visitors instead, your code will be *much* cleaner. See [this](http://stackoverflow.com/a/29996191/3764814) answer of mine. – Lucas Trzesniewski Oct 11 '15 at 15:58
  • @LucasTrzesniewski I understand what you're saying, but I guess I'm not sure how to approach the problem with what you're suggesting. I'm trying to add AST rules to my Antlr grammar .g file. – The Whether Man Oct 12 '15 at 00:08

0 Answers0