antlr / antlr/antlr4

parser.getInterpreter().setPredictionMode() missing in python 3 runtime

Open
#4,243 0 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

Page 159 of The Definitive ANTLR 4 Reference advises to "make the parser report all ambiguities" using:
```
parser.getInterpreter().setPredictionMode(PredictionMode.LL_EXACT_AMBIG_DETECTION)
```
Using ANTLR 4.12.0 with the Python 3 runtime a `getInterpreter()` method does not exist on the parser, nor does a `setPredictionMode()` method exist on the interpreter (when inspecting via `parser._interp`).

**The following code fails on the final line:**
```
lexer = Lang0Lexer(antlr4.InputStream(state.input_text))
stream = antlr4.CommonTokenStream(lexer)
parser = Lang0Parser(stream)
parser.removeErrorListeners()
parser.addErrorListener(antlr4.DiagnosticErrorListener())
parser.getInterpreter().setPredictionMode(antlr4.PredictionMode.LL_EXACT_AMBIG_DETECTION)
```

with error:

> AttributeError: 'Lang0Parser' object has no attribute 'getInterpreter'

The following hack appears to work:

```
parser._interp.predictionMode = antlr4.PredictionMode.LL_EXACT_AMBIG_DETECTION
```

Possible partial fix:

Add the following method to class `Recognizer` in `Recogniser.py` (here: https://github.com/antlr/antlr4/blob/master/runtime/Python3/src/antlr4/Recognizer.py):

```
def getInterpreter(self):
return _interp
```

Then at least autocomplete will match something resembling the Java code.

Contributor guide

Open the contributing guide

Research direction

Start with runtime/Python3/src/antlr4/Recognizer.py and compare the Python 3 runtime API with the Java usage described in the issue. Inspect parser._interp and the reported predictionMode workaround to determine the corresponding public interface. Done means the documented Python example can configure LL_EXACT_AMBIG_DETECTION without the reported AttributeError.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.