antlr / antlr/antlr4

NullPointerException in LexerGrammar.createLexerInterpreter() on invalid grammar

Open
#2,153 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

When creating a grammar that is missing a single quote to close a token, not getting a proper exception.
Instead, getting a NullPointerException when trying to actually use the lexer.

Sample test code (JUnit):
```java
@Test
public void bugReproduction() throws Exception {
String grammarWithMissingQuote = "grammar test;\nr0: r1+;\nr1: 'ABC' | 'ABCDE;";
LexerGrammar lg = new LexerGrammar(grammarWithMissingQuote);
CharStream inputStream = CharStreams.fromReader(new StringReader("AB"));
lg.createLexerInterpreter(inputStream);
}
```
This throws NullPointerException straight out of Grammar.createLexerInterpreter at line 1299, due to ```implicitLexer``` not being initialized. I would expect the LexerGrammar constructor to throw instead of succeeding but not really initializing.

Contributor guide

Open the contributing guide

Research direction

Start with the JUnit reproduction in the issue and inspect LexerGrammar.createLexerInterpreter, Grammar.createLexerInterpreter, and the LexerGrammar constructor. Run the reproduction with the grammar containing the missing quote. Done means invalid grammars produce a proper exception during construction or use, rather than a NullPointerException caused by an uninitialized implicitLexer.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.