dtonhofer

dtonhofer

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.

Marked As Solved

ariel.lenis

ariel.lenis

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.

Popular Prag Prog topics Top

belgoros
Following the steps described in Chapter 6 of the book, I’m stuck with running the migration as described on page 84: bundle exec sequel...
New
mikecargal
Title: Hands-On Rust (Chap 8 (Adding a Heads Up Display) It looks like ​.with_simple_console_no_bg​(SCREEN_WIDTH*2, SCREEN_HEIGHT*2...
New
mikecargal
Title: Hands-on Rust: question about get_component (page 295) (feel free to respond. “You dug you’re own hole… good luck”) I have somet...
New
New
swlaschin
The book has the same “Problem space/Solution space” diagram on page 18 as is on page 17. The correct Problem/Solution space diagrams ar...
New
nicoatridge
Hi, I have just acquired Michael Fazio’s “Kotlin and Android Development” to learn about game programming for Android. I have a game in p...
New
oaklandgit
Hi, I completed chapter 6 but am getting the following error when running: thread 'main' panicked at 'Failed to load texture: IoError(O...
New
akraut
The markup used to display the uploaded image results in a Phoenix.LiveView.HTMLTokenizer.ParseError error. lib/pento_web/live/product_l...
New
andreheijstek
After running /bin/setup, the first error was: The foreman' command exists in these Ruby versions: That was easy to fix: gem install fore...
New
SlowburnAZ
Getting an error when installing the dependencies at the start of this chapter: could not compile dependency :exla, "mix compile" failed...
New

Other popular topics Top

AstonJ
A thread that every forum needs! Simply post a link to a track on YouTube (or SoundCloud or Vimeo amongst others!) on a separate line an...
New
ohm
Which, if any, games do you play? On what platform? I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New
DevotionGeo
I know that these benchmarks might not be the exact picture of real-world scenario, but still I expect a Rust web framework performing a ...
New
brentjanderson
Bought the Moonlander mechanical keyboard. Cherry Brown MX switches. Arms and wrists have been hurting enough that it’s time I did someth...
New
DevotionGeo
I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
New
AstonJ
Seems like a lot of people caught it - just wondered whether any of you did? As far as I know I didn’t, but it wouldn’t surprise me if I...
New
New
PragmaticBookshelf
A Hero’s Journey with Chris Pine @chrispine Chris Pine, author of Learn to Program, Third Edition, discusses his journey to beco...
New
PragmaticBookshelf
Author Spotlight: Sophie DeBenedetto @SophieDeBenedetto The days of the traditional request-response web application are long gone, b...
New
PragmaticBookshelf
A Ruby-Centric Chat with Noel Rappin @noelrappin Once you start noodling around with Ruby you quickly figure out, as Noel Rappi...
New

Latest in PragProg

View all threads ❯