0
votes

I got this demo code from school and it is supposed to show me a parse tree. After I generate the parser and test the rule in that class, I'm supposed to run the Java class, but I don't seem the get a parser tree. Does anyone know what I'm doing wrong?

enter image description here

enter image description here

And the code from the Java file

MyAntlrDemo

import java.io.IOException;

import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.ParseTree;

/**
 * Antlr Java Parser Demo: read a Java file and print methods found by visiting the parse tree.
 * Don't forget to generate ANTLR Java parser class files first!
 */
public class MyAntlrDemo extends Java8ParserBaseVisitor<Void> {
    /**
     * Main Method
     */
    public static void main(String[] args) throws IOException {
        //String file = "./src/MyAntlrDemo.java"; // input this source  file
        String file = "./exampleJava.txt";  // a shorter Java example input

        CharStream chars = CharStreams.fromFileName(file);
        Java8Lexer lexer = new Java8Lexer(chars);
        CommonTokenStream tokens = new CommonTokenStream(lexer); // lexer converts text to tokens
        Java8Parser parser = new Java8Parser(tokens);
        ParseTree tree = parser.compilationUnit(); // create tree from staring rule: compilationunit
        System.out.println("Number of syntax errors found: " + parser.getNumberOfSyntaxErrors());
        // walk the tree and do something with it
        MyAntlrDemo visitor = new MyAntlrDemo(); // extends JavaBaseVisitor<Void>
        System.out.println("STARTING VISIT");
        visitor.visit(tree); // calls visitCompilationUnit and other overridden methods below
    }

    @Override
    public Void visitCompilationUnit(Java8Parser.CompilationUnitContext ctx) {
        System.out.println("Inside visitCompilationUnit");
        return visitChildren(ctx);
    }

    @Override
    public Void visitMethodDeclaration(Java8Parser.MethodDeclarationContext ctx) {
        System.out.println("found method:" + ctx.getText());
        System.out.println("Methodname: " + ctx.methodHeader().methodDeclarator().Identifier().getText());
        return super.visitMethodDeclaration(ctx);
    }

}

Also the .txt file

ExampleJava.txt

class Jan {
  private int foo; // comment
  public Jan() { }
  public int eenMethode(){
    return foo * 2;
  }
}

The output message:

enter image description here

ANTLR preview:

enter image description here

1
In your Antlr preview screen capture, the start rule isn't compilationUnit as you do in main().kaby76
@kaby76 do you mean I should test rule compilationUnit, because when I do that, I still don't get to see a parser tree. But the problem is also, the ANTLR preview is supposed to show the input code on the left side, but it doesn't seem to cooperate.Liza Darwesh

1 Answers

2
votes

you have to change the choice in last image or paste your code in antlr preview

after check the sample and see the parse tree in right section you have to right click in file => "configure antlr" => set generated path => set generated directory => save

right click in file => "generate antlr recognizer"

generated files included parse tree will be in generated directory you choose