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 Pragmatic Bookshelf topics Top

jeffmcompsci
Title: Design and Build Great Web APIs - typo “https://company-atk.herokuapp.com/2258ie4t68jv” (page 19, third bullet in URL list) Typo:...
New
lirux
Hi Jamis, I think there’s an issue with a test on chapter 6. I own the ebook, version P1.0 Feb. 2019. This test doesn’t pass for me: ...
New
raul
Page 28: It implements io.ReaderAt on the store type. Sorry if it’s a dumb question but was the io.ReaderAt supposed to be io.ReadAt? ...
New
Chrichton
Dear Sophie. I tried to do the “Authorization” exercise and have two questions: When trying to plug in an email-service, I found the ...
New
leonW
I ran this command after installing the sample application: $ cards add do something --owner Brian And got a file not found error: Fil...
New
patoncrispy
I’m new to Rust and am using this book to learn more as well as to feed my interest in game dev. I’ve just finished the flappy dragon exa...
New
AndyDavis3416
@noelrappin Running the webpack dev server, I receive the following warning: ERROR in tsconfig.json TS18003: No inputs were found in c...
New
taguniversalmachine
It seems the second code snippet is missing the code to set the current_user: current_user: Accounts.get_user_by_session_token(session["...
New
New
ggerico
I got this error when executing the plot files on macOS Ventura 13.0.1 with Python 3.10.8 and matplotlib 3.6.1: programming_ML/code/03_...
New

Other popular topics Top

Devtalk
Hello Devtalk World! Please let us know a little about who you are and where you’re from :nerd_face:
New
New
AstonJ
I ended up cancelling my Moonlander order as I think it’s just going to be a bit too bulky for me. I think the Planck and the Preonic (o...
New
AstonJ
Do the test and post your score :nerd_face: :keyboard: If possible, please add info such as the keyboard you’re using, the layout (Qw...
New
AstonJ
We’ve talked about his book briefly here but it is quickly becoming obsolete - so he’s decided to create a series of 7 podcasts, the firs...
New
PragmaticBookshelf
Rails 7 completely redefines what it means to produce fantastic user experiences and provides a way to achieve all the benefits of single...
New
PragmaticBookshelf
Build efficient applications that exploit the unique benefits of a pure functional language, learning from an engineer who uses Haskell t...
New
New
AstonJ
Was just curious to see if any were around, found this one: I got 51/100: Not sure if it was meant to buy I am sure at times the b...
New
PragmaticBookshelf
Author Spotlight: Tammy Coron @Paradox927 Gaming, and writing games in particular, is about passion, vision, experience, and immersio...
New