
dtonhofer
The Definitive ANTLR 4 Reference - "Java.g4" grammer (used on page 40) no longer passes ANTLR
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
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










Other popular topics










Latest in PragProg
Latest (all)
Categories:
Popular Portals
- /elixir
- /opensuse
- /rust
- /kotlin
- /ruby
- /erlang
- /python
- /react
- /clojure
- /quarkus
- /go
- /react-native
- /vapor
- /v
- /wasm
- /django
- /security
- /nodejs
- /centos
- /rails
- /haskell
- /fable
- /gleam
- /swift
- /deno
- /js
- /tailwind
- /assemblyscript
- /laravel
- /symfony
- /phoenix
- /crystal
- /typescript
- /debian
- /adonisjs
- /julia
- /arch-linux
- /svelte
- /spring
- /c-plus-plus
- /preact
- /flutter
- /actix
- /java
- /angular
- /ocaml
- /kubuntu
- /zig
- /scala
- /zotonic
- /vim
- /rocky
- /lisp
- /keyboards
- /html
- /emacs
- /vuejs
- /nim
- /nerves
- /elm