antlr4
antlr4 copied to clipboard
Go Target, creates uncompilable xxx_parser.go
- [x] I am not submitting a question on how to use ANTLR; instead, go to [antlr4-discussion google group]
- [x] I have done a search of the existing issues to make sure I'm not sending in a duplicate
What happens
When creating a parser/lexer -Dlanguage=Go the resulting code is not compilable.
See the excerpt from smali_parser.go .
type SmaliParser struct {
*antlr.BaseParser
}
// NewSmaliParser produces a new parser instance for the optional input antlr.TokenStream.
//
// The *SmaliParser instance produced may be reused by calling the SetInputStream method.
// The initial parser configuration is expensive to construct, and the object is not thread-safe;
// however, if used within a Golang sync.Pool, the construction cost amortizes well and the
// objects can be used in a thread-safe manner.
func NewSmaliParser(input antlr.TokenStream) *SmaliParser {
this := new(SmaliParser)
deserializer := antlr.NewATNDeserializer(nil)
deserializedATN := deserializer.DeserializeFromUInt16(parserATN)
decisionToDFA := make([]*antlr.DFA, len(deserializedATN.DecisionToState))
for index, ds := range deserializedATN.DecisionToState {
decisionToDFA[index] = antlr.NewDFA(ds, index)
}
this.BaseParser = antlr.NewBaseParser(input)
this.Interpreter = antlr.NewParserATNSimulator(this, deserializedATN, decisionToDFA, antlr.NewPredictionContextCache())
this.RuleNames = ruleNames
this.LiteralNames = literalNames
this.SymbolicNames = symbolicNames
this.GrammarFileName = "SmaliParser.g4"
return this
}
Similar issue in the lever
type SmaliLexer struct {
*antlr.BaseLexer
channelNames []string
modeNames []string
// TODO: EOF string
}
// NewSmaliLexer produces a new lexer instance for the optional input antlr.CharStream.
//
// The *SmaliLexer instance produced may be reused by calling the SetInputStream method.
// The initial lexer configuration is expensive to construct, and the object is not thread-safe;
// however, if used within a Golang sync.Pool, the construction cost amortizes well and the
// objects can be used in a thread-safe manner.
func NewSmaliLexer(input antlr.CharStream) *SmaliLexer {
l := new(SmaliLexer)
lexerDeserializer := antlr.NewATNDeserializer(nil)
lexerAtn := lexerDeserializer.DeserializeFromUInt16(serializedLexerAtn)
lexerDecisionToDFA := make([]*antlr.DFA, len(lexerAtn.DecisionToState))
for index, ds := range lexerAtn.DecisionToState {
lexerDecisionToDFA[index] = antlr.NewDFA(ds, index)
}
l.BaseLexer = antlr.NewBaseLexer(input)
l.Interpreter = antlr.NewLexerATNSimulator(l, lexerAtn, lexerDecisionToDFA, antlr.NewPredictionContextCache())
l.channelNames = lexerChannelNames
l.modeNames = lexerModeNames
l.RuleNames = lexerRuleNames
l.LiteralNames = lexerLiteralNames
l.SymbolicNames = lexerSymbolicNames
l.GrammarFileName = "SmaliLexer.g4"
// TODO: l.EOF = antlr.TokenEOF
return l
}
In this case the fields l.Interpreter, l.RuleNames , l.SymbolicNames, l.GrammarFileName are undefined.
What is supposed to happen
The resulting code should be compilable
How to reproduce
#!/bin/bash
### In reference to this issue https://github.com/AlexeySoshin/smali2java/issues/11
rm -r antlr
mkdir antlr
cd antlr
antlr_file="antlr-4.9.2-complete.jar"
antlr_url="https://www.antlr.org/download/$antlr_file"
antlr_grammar_url="https://raw.githubusercontent.com/psygate/smali-antlr4-grammar/acff6f3afcfa9f6c49cc40aa55360447d88c58ca"
# filenames
smaliLexer="SmaliLexer.g4"
smaliParser="SmaliParser.g4"
curl_cmd="curl --silent"
$curl_cmd --output $antlr_file $antlr_url
$curl_cmd --output $smaliLexer $antlr_grammar_url/$smaliLexer
$curl_cmd --output $smaliParser $antlr_grammar_url/$smaliParser
java -cp $antlr_file org.antlr.v4.Tool -Dlanguage=Go -visitor -o gen $smaliLexer
java -cp $antlr_file org.antlr.v4.Tool -Dlanguage=Go -visitor -o gen $smaliParser
Compiles without any errors for me, Msys2/Win10. What is your Go version?
$ make
java -jar c:/users/kenne/downloads/antlr-4.9.2-complete.jar -encoding utf-8 -Dlanguage=Go -o parser -lib parser parser/SmaliLexer.g4
java -jar c:/users/kenne/downloads/antlr-4.9.2-complete.jar -encoding utf-8 -Dlanguage=Go -o parser -lib parser parser/SmaliParser.g4
export GO111MODULE=auto; go build Program.go
export GO111MODULE=auto; go get github.com/antlr/antlr4/runtime/Go/antlr
$ go version
go version go1.16 windows/amd64
That said, I have problems with the implementation of superClass with Go. And, it is known to be slow.
This is because you are using the visitor pattern which is not fully implemented. I have opened a PR to hopefully fix this.
https://github.com/antlr/antlr4/pull/3299
Any updates?