The Definitive ANTLR 4 Reference - "Java.g4" grammer (used on page 40) no longer passes ANTLR

@parrt

In the context of Chapter 4.3, the grammar Java.g4, meant to parse Java 6 compilation units, no longer passes ANTLR (currently 4.10.1)

That file is included in the Zip file of examples & source code that comes with the book (as code/tour/Java.g4)

Two problems:

The definition of “expression” uses “<assoc=right>” on each operator from line 546 onwards. However, “<assoc=right>” now needs to be placed in front:

    |   <assoc=right> expression
        ('^='
        |'+='
        |'-='
        |'*='
        |'/='
        |'&='
        |'|='
        |'='
        |'>' '>' '='
        |'>' '>' '>' '='
        |'<' '<' '='
        |'%='
        ) 

The definitin for “Escape Sequence” contains the string ‘"’ which is considered an illegal escape. The backslash has to be removed.

In fact, the current ANTLR distro comes with its own Java.g4, or rather two of them (very confusing!)

A grammer for Java 7 from 2013:

antlr4-4.10.1/runtime-testsuite/test/org/antlr/v4/test/runtime/java/api/Java.g4

A grammar for Java 5 from 2008;

antlr4-4.10.1/tool-testsuite/test/org/antlr/v4/test/tool/Java.g4

The example code should probably be upgrade to

antlr4-4.10.1/runtime-testsuite/test/org/antlr/v4/test/runtime/java/api/Java.g4

which seems to work.

Hiya. Yeah, we use the grammar as part of our testing but you should look at the grammar repository for current grammars. Those are in test suite directories and so not meant for you are use. Please see GitHub - antlr/grammars-v4: Grammars written for ANTLR v4; expectation that the grammars are free of actions.

Thank you Mr. Parr.

Thank you @dtonhofer and @parrt I was able to make it work using the grammars from

https://github.com/antlr/grammars-v4/tree/master/java/java

You need to generate the Java files in order: Lexer first and Parser last

antlr4 JavaLexer.g4
antlr4 JavaParser.g4

And finally this is the code that I have used for the ExtractInterfaceListener.java file:

import org.antlr.v4.runtime.TokenStream;

public class ExtractInterfaceListener extends JavaParserBaseListener {
    JavaParser parser;
    public ExtractInterfaceListener(JavaParser parser) {this.parser = parser;}

    @Override
    public void enterClassDeclaration(JavaParser.ClassDeclarationContext ctx) {
        // Errata Suggestion: previous implementation
        // System.out.println("interface I" + ctx.Identifier() + " {");
        // Errata Suggestion: fix
        System.out.println("interface I" + ctx.identifier().getText() + " {");
    }

    @Override
    public void exitClassDeclaration(JavaParser.ClassDeclarationContext classDeclarationContext) {
        System.out.println("}");
    }

    @Override
    public void enterMethodDeclaration(JavaParser.MethodDeclarationContext ctx) {
        // need parser to get tokens
        TokenStream tokens = parser.getTokenStream();
        
        /*
        // Errata Suggestion: previous implementation
        String type = "void";

        if ( ctx.type()!=null ) {
            type = tokens.getText(ctx.type());
        }
        */
        // Errata Suggestion: fix
        String type = ctx.typeTypeOrVoid().getText();

        String args = tokens.getText(ctx.formalParameters());

        // Errata Suggestion: previous implementation
        // System.out.println("\t" + type + " " + ctx.Identifier() + args + ";");
        // Errata Suggestion: fix
        System.out.println("\t" + type + " " + ctx.identifier().getText() + args + ";");
    }
}

I hope it will help someone.