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

jon
Some minor things in the paper edition that says “3 2020” on the title page verso, not mentioned in the book’s errata online: p. 186 But...
New
johnp
Running the examples in chapter 5 c under pytest 5.4.1 causes an AttributeError: ‘module’ object has no attribute ‘config’. In particula...
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
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
adamwoolhether
I’m not quite sure what’s going on here, but I’m unable to have to containers successfully complete the Readiness/Liveness checks. I’m im...
New
brunogirin
When I run the coverage example to report on missing lines, I get: pytest --cov=cards --report=term-missing ch7 ERROR: usage: pytest [op...
New
Henrai
Hi, I’m working on the Chapter 8 of the book. After I add add the point_offset, I’m still able to see acne: In the image above, I re...
New
dtonhofer
@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....
New
bjnord
Hello @herbert ! Trying to get the very first “Hello, Bracket Terminal!" example to run (p. 53). I develop on an Amazon EC2 instance runn...
New
roadbike
From page 13: On Python 3.7, you can install the libraries with pip by running these commands inside a Python venv using Visual Studio ...
New

Other popular topics Top

AstonJ
If it’s a mechanical keyboard, which switches do you have? Would you recommend it? Why? What will your next keyboard be? Pics always w...
New
AstonJ
What chair do you have while working… and why? Is there a ‘best’ type of chair or working position for developers?
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
Margaret
Hello content creators! Happy new year. What tech topics do you think will be the focus of 2021? My vote for one topic is ethics in tech...
New
dimitarvp
Small essay with thoughts on macOS vs. Linux: I know @Exadra37 is just waiting around the corner to scream at me “I TOLD YOU SO!!!” but I...
New
AstonJ
In case anyone else is wondering why Ruby 3 doesn’t show when you do asdf list-all ruby :man_facepalming: do this first: asdf plugin-upd...
New
Exadra37
Oh just spent so much time on this to discover now that RancherOS is in end of life but Rancher is refusing to mark the Github repo as su...
New
OvermindDL1
Woooooooo! This is such a huge release for it, and 2 years incoming! In short, the library is now using an updated hyper backend (not j...
New
PragmaticBookshelf
Author Spotlight James Stanier @jstanier James Stanier, author of Effective Remote Work , discusses how to rethink the office as we e...
New
First poster: joeb
The File System Access API with Origin Private File System. WebKit supports new API that makes it possible for web apps to create, open,...
New

Latest in PragProg

View all threads ❯