question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Go target generated code for Java.g4 grammar doesn't compile

See original GitHub issue

Before submitting an issue to ANTLR, please check off these boxes:

  • [*] I am not submitting a question on how to use ANTLR; instead, go to antlr4-discussion google group or ask at stackoverflow
  • [*] I have done a search of the existing issues to make sure I’m not sending in a duplicate

Expected behavior

antlr4 -Dlanguage=Go Java.g4 should generate Go code that compiles. Java.g4 was taken from github.com/antlr/grammars-v4

Other info:

antlr4 command is: alias antlr4='java -Xmx500M -cp "/home/ereyes/.m2/repository/org/antlr/antlr4/4.6-SNAPSHOT/antlr4-4.6-SNAPSHOT.jar:$CLASSPATH" org.antlr.v4.Tool'

$ go version
go version go1.7.1 linux/amd64

Actual behavior

The generated code does not compile. Here’s what the Go compiler says:

$ go install ./
# /home/ereyes/code/antlr4-java/parser
./java_lexer.go:726: syntax error: unexpected _input, expecting comma or )
./java_lexer.go:739: syntax error: unexpected _input, expecting comma or )

Steps to reproduce the behavior

$ cd ~/code
$ git clone https://github.com/antlr/grammars-v4
$ mkdir -p antlr4-java/src/parser
$ export GOPATH=$HOME/code/antlr4-java
$ cd antlr4-java/src/parser
$ cp ~/code/grammars-v4/java/Java.g4 ./
$ antlr4 -Dlanguage=Go Java.g4
$ go get github.com/antlr/antlr4/runtime/Go/antlr
$ go install ./

I looked at the error and attempted to fix by applying the patch below, but it still doesn’t fix the problem:

diff --git a/java_lexer.go b/java_lexer.go
index b7b70c6..4b5114f 100644
--- a/java_lexer.go
+++ b/java_lexer.go
@@ -723,7 +723,7 @@ func (p *JavaLexer) JavaLetter_Sempred(localctx antlr.RuleContext, predIndex int
 			return Character.isJavaIdentifierStart(_input.LA(-1))
 
 	case 1:
-			return Character.isJavaIdentifierStart(Character.toCodePoint((char)_input.LA(-2), (char)_input.LA(-1)))
+			return Character.isJavaIdentifierStart(Character.toCodePoint((char)(_input.LA(-2)), (char)(_input.LA(-1))))
 
 	default:
 		panic("No predicate with index: " + fmt.Sprint(predIndex))
@@ -736,7 +736,7 @@ func (p *JavaLexer) JavaLetterOrDigit_Sempred(localctx antlr.RuleContext, predIn
 			return Character.isJavaIdentifierPart(_input.LA(-1))
 
 	case 3:
-			return Character.isJavaIdentifierPart(Character.toCodePoint((char)_input.LA(-2), (char)_input.LA(-1)))
+			return Character.isJavaIdentifierPart(Character.toCodePoint((char)(_input.LA(-2)), (char)(_input.LA(-1))))
 
 	default:
 		panic("No predicate with index: " + fmt.Sprint(predIndex))

… I tried to fix the parentheses in the cast in java_lexer.go. I also wondered if the intention was to cast to rune instead of char, which isn’t a built-in type in Go. Anyways, here are the compiler errors after applying this patch:

./java_lexer.go:723: undefined: Character in Character.isJavaIdentifierStart
./java_lexer.go:723: undefined: _input in _input.LA
./java_lexer.go:726: undefined: Character in Character.isJavaIdentifierStart
./java_lexer.go:726: undefined: char
./java_lexer.go:726: undefined: _input in _input.LA
./java_lexer.go:726: undefined: char
./java_lexer.go:726: undefined: _input in _input.LA
./java_lexer.go:736: undefined: Character in Character.isJavaIdentifierPart
./java_lexer.go:736: undefined: _input
./java_parser.go:14393: no new variables on left side of :=
./java_lexer.go:736: too many errors

… and after changing char to rune:

./java_lexer.go:723: undefined: Character in Character.isJavaIdentifierStart
./java_lexer.go:723: undefined: _input in _input.LA
./java_lexer.go:726: undefined: Character in Character.isJavaIdentifierStart
./java_lexer.go:726: undefined: _input in _input.LA
./java_lexer.go:736: undefined: Character in Character.isJavaIdentifierPart
./java_lexer.go:736: undefined: _input in _input.LA
./java_lexer.go:739: undefined: Character in Character.isJavaIdentifierPart
./java_lexer.go:739: undefined: _input in _input.LA
./java_parser.go:14393: no new variables on left side of :=

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:22 (16 by maintainers)

github_iconTop GitHub Comments

1reaction
pboyercommented, Dec 1, 2016

Thanks so much for checking, @ereyes01!

0reactions
Nhoyacommented, Feb 22, 2019

If I use

java -jar 'C:\Javalib\antlr-4.7.2-complete.jar' -lib .\JavaLexer.g4 .\JavaParser.g4 -o build

I expect to see both the java_lexer.go and java_parser.go to be generated in the build directory. If this is not the case I’m missing something. Can you please link me the part of the documentation I’m skipping?

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - Antlr4 (CSharp Target), Grammar = Java.g4 - Stack Overflow
I am now attempting to generate a parser for the Java.g4 grammar which I obtained from github.com/antlr/grammars-v4/java. The JavaLexer.cs file ...
Read more >
Parsing with ANTLR 4 and Go | Gopher Academy Blog
A single grammar can be used to generate parsers in Java, Go, C, etc. Unlike Bison/Yacc which typically embeds target language code into...
Read more >
ANTLR
ANTLR (ANother Tool for Language Recognition) is a powerful parser generator for reading, processing, executing, or translating structured text or binary files.
Read more >
Java with ANTLR - Baeldung
ANTLR works by generating Java code corresponding to the grammar files that we give it, and the maven plugin makes it easy: mvn...
Read more >
ANTLR Magic — Developing Mainframe Language ... - Medium
>antlr4 ShortestCobolGrammar.g4 // generate antlr artifacts, ... That is why you have to compile the generated Java source files.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found