0

I am trying to construct an AST using the latest version of ANTLR (v4), i found a couple of links here in stack-overflow and also on the net, that shows you how to do it for a simple grammars. However, i wanted an AST for the Java.g4 grammar (Java language). Currently , i was following the approached recommended here (https://codevomit.wordpress.com/2015/04/25/antlr4-project-with-maven-tutorial-episode-3/). However based on this approach, i needed to label all the production rules that have alternatives.

Hence, i would greatly appreciate it if someone informed me if there is a different alternative for building an AST using ANTLR4 or if there is an already labeled java.g4 grammar.

  • You can use this grammar for java https://github.com/antlr/grammars-v4/blob/master/java/Java.g4 . There is also a grammar for java8 https://github.com/antlr/grammars-v4/tree/master/java8 – Vincent Aranega Jul 03 '15 at 09:46
  • @VincentAranega Thanks. However,I am already playing with those grammars. What i need is a **labeled grammar** for the alternative production rules so that I can have different visitor methods for the different labels when antlr4 generates JavaVisitor.java. This will allow me to have the abstraction that I want. – user1577269 Jul 03 '15 at 09:54
  • ANTLR4 will build a parse tree for you, which is pretty convenient; it takes effort to build a real AST, and it may not pay off as much as you hope in terms of effort. See http://stackoverflow.com/a/1916687/120163 – Ira Baxter Jul 03 '15 at 10:48
  • @IraBaxter Thanks. Is there a more convenient way to build an AST for a *function body* (an AST representation of a method) other than employing ANTLR4. – user1577269 Jul 15 '15 at 06:52
  • @user1577269: You seem to have 3 questions: a) can you use ANTLR4 to parse *just* function bodies, b) is there a convenient way to build ASTs with ANTLR4, and c) is there a way to do this other than ANTLR4. Regarding a), you could bend the grammar by making the goal rule also allow a "function" (presumably you mean method). b) AFAIK, no, but I've already responded to this in previous comment. c) If you want to get *AST*s, the ONLY convenient answer I know is our DMS Software Reengineering Toolkit, and yes, you can ask it to parse just a method. I'll elaborate in an answer if you ask me to. – Ira Baxter Jul 15 '15 at 14:23
  • See http://stackoverflow.com/a/6378997/120163 for "ast" vs "cst" examples – Ira Baxter Jul 15 '15 at 14:27
  • I have created a sample project creating AST for Java using Antlr v4 https://github.com/adamsiemion/antlr-java-ast – Adam Siemion Nov 08 '16 at 09:07

1 Answers1

1

All of the subrule alternates do not need to be labeled. Label just those alternates where generation of a discrete context object is desired. Really just the statement, expression, and (maybe) primary rules warrant labeling.

Labeling itself is trivial.

GRosenberg
  • 5,418
  • 2
  • 15
  • 20
  • For OP's specific problem, he probably wants to label the (partial) rule for method declarations. Then apparantly ANTLR can be launched to parse "just" that rule/label. (I only know what I read; I haven't actually done this). – Ira Baxter Jul 15 '15 at 16:56
  • Labeling link is not accesible anonymously (requests contacting admin at theantlrguy.atlassian.net) – Jorge Lavín Jul 10 '18 at 13:44