antlr / antlr/antlr4

Unrelated rules change matching output

Open
#985 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

```
grammar Test;
x: ab 'b' ab EOF;
y: ab+ EOF;
ab: 'a' 'b'?;
```

Output of

```
$ rm -f Test*.[^g]*; antlr Test.g && javac Test*.java \
&& echo -n "abab" | grun Test ab -tree
```

is

```
(ab a b)
```

as expected.

Now I comment out rule `y` in the grammar:

```
grammar Test;
x: ab 'b' ab EOF;
//y: ab+ EOF;
ab: 'a' 'b'?;
```

And the output of the above command changes to:

```
(ab a)
```

I'm not absolutely sure it's a bug, maybe it's undefined behavior (i.e. it is not specified if `ab` should match greedily or not here), but still it feels a bit suspicious to me.

Tested with ANTLR 4.5.1.

Contributor guide

Open the contributing guide

Research direction

Reproduce the issue using Test.g, antlr Test.g, the generated Test*.java files, and the grun Test ab -tree command. Compare the generated parser and matching output with rule y present and commented out, then determine the intended behavior for ab. Done means the unrelated rule no longer changes matching output, with the behavior covered by a regression test.

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.