Java tree parser output for ANTLR

I found a sample template on the ANTLR website, its Javatreeparser.g, which the site says can create the AST I need, but since I'm new to ANTLR, how do I show it? What I have done so far is to put the grammar file alongside my existing Java grammar. But I have no idea how to use and output the AST that I need from a file. How to do it?

+3


source to share


2 answers


I found a sample template on the ANTLR website, its Javatreeparser.g, which the site says can create the AST I need.

No, the combined grammar Java.g

from the ANTLR wiki creates a lexer and parser for Java source files. The parser then creates an AST of that source, and this AST can then be used JavaTreeParser.g

to move it. The tree grammar is JavaTreeParser.g

not used to create an ACT. This is done by a parser created with Java.g

.

What I have done so far is to put the grammar file alongside my existing Java grammar.

This is not true. The tree grammar JavaTreeParser.g

expects an AST to be injected, generated by a parser created from Java.g

. You can't just plug in another parser (or a different tree grammar, for that matter).

But I have no idea how to use and output the AST that I need from a file. How to do it?

See previous Q&A: Visualizing an AST generated with ANTLR (in .NET environment)

EDIT

I didn't want to post this right away because I wanted you to try yourself first (yes, I mean!);)

Here's a quick demo:

  • copy Java.g

    to the directory and remove the ads from it @header{...}

    and @lexer:::header{...}

    ;
  • copy antlr-3.3.jar

    to the same directory;
  • Create a file Main.java

    and Test.java

    the directory (see. below).

Test.java

public class Test {

    int i = 1 + 2;
    String s;

    Test(String s) {
        this.s = s;
    }
}

      



Main.java

import org.antlr.runtime.*;
import org.antlr.runtime.tree.*;
import org.antlr.stringtemplate.*;

public class Main {
    public static void main(String[] args) throws Exception {
        JavaLexer lexer = new JavaLexer(new ANTLRFileStream("Test.java"));
        JavaParser parser = new JavaParser(new CommonTokenStream(lexer));
        CommonTree tree = (CommonTree)parser.javaSource().getTree();
        DOTTreeGenerator gen = new DOTTreeGenerator();
        StringTemplate st = gen.toDOT(tree);
        System.out.println(st);
    }
}

      

Now create a lexer and parser:

java -cp antlr-3.3.jar org.antlr.Tool Java.g 

      

Then compile all source files .java

:

javac -cp antlr-3.3.jar *.java 

      

Finally, run the class Main

and output the output to a file named ast.dot

.

java -cp .:antlr-3.3.jar Main > ast.dot

      

(in the Windows, do: java -cp .;antlr-3.3.jar Main > ast.dot

)

If you open the file now ast.dot

, you see the DOT representation of the AST generated by the parser. You can render this AST by copying the DOT source into it: http://graphviz-dev.appspot.com , resulting in the following image:

enter image description here

+8


source


I really recommend that you use antlr4.

Install CLASSPATH (including antlr-4.5.3-complete.jar) and JAVA_HOME first.

Second, generate a lexer and parser from Java.g4 grammar:

java -cp antlr-4.5.2-complete.jar Java.g4

      

Third, compile all Java * .java genereated:



javac Java*.java

      

Finally, run TestRig:

java org.antlr.v4.runtime.misc.TestRig Java compilationUnit -gui Test.java

      

Visually, you will see the AST as follows:

enter image description here

+1


source







All Articles