antlr / antlr/antlr4

No error/incorrect code generation when importing lexer grammar with modes into a combined grammar

Open
#970 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
19k
Forks
3.5k
PR merge metrics
No merged PRs in 30d

Description

Using ANTLR 4.5.1, if you import a lexer with modes into a combined grammar it does not report the error that modes are not supported in combined grammars. Furthermore, it does not output the modes in the `modeNames` field of the lexer. I assume that means the generated lexer won't handle the modes correctly, though I haven't tested because I have moved on to separate lexer and parser grammars due to all the limitations & issues of combined grammars.

I reported this as a distinct issue from #160 and #256, because the first seems to be about making it actually work and the second is about importing into a lexer grammar. This issue is just to get a proper error message for importing into combined grammars.

To reproduce, use the following files:

**Mode.g4**

```
lexer grammar Mode;
Char : . -> mode(MODE1);
mode MODE1;
WS : ' ' -> mode(DEFAULT_MODE);
```

**Parse.g4**

```
grammar Parse;
import Mode;
file : Char* EOF;
```

Then generate lexer and parser for Parse.g4

Contributor guide

Open the contributing guide

Research direction

Start by running ANTLR generation with the supplied Mode.g4 and Parse.g4 grammars and compare the generated lexer output with the reported missing modeNames entry. Trace the combined-grammar import handling until the unsupported lexer modes case is reached; done means generation reports a clear error instead of silently producing output.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.