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.