antlr / antlr/antlr4

Go Target, creates uncompilable xxx_parser.go

Open
#3,262 3 comments 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
19k
Forks
3.5k
PR merge metrics
No merged PRs in 30d

Description

- [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 .

```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

```go
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

```sh
#!/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
```

Contributor guide

Open the contributing guide

Research direction

Run the provided shell reproduction with ANTLR 4.9.2 and inspect the generated SmaliParser.go and SmaliLexer.go files. Trace the undefined Interpreter, RuleNames, SymbolicNames, and GrammarFileName fields in the generated Go code, then verify that the resulting parser and lexer compile successfully.

Written by the indexing model from the issue text.

Assessment

Tech stack
go, java
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.