antlr4: Go target generated code for Java.g4 grammar doesn't compile
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 :=
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 22 (15 by maintainers)
Commits related to this issue
- Potential fix for antlr/antlr4 #1397 — committed to pboyer/antlr4 by pboyer 8 years ago
- Merge pull request #1436 from pboyer/patch-5 Fix for #1397 — committed to antlr/antlr4 by parrt 8 years ago
Thanks so much for checking, @ereyes01!