antlr / antlr/antlr4

rule named same as parser rule attributies will generate a wrong parser file

Open
#4,152 0 comments 1 reaction 0 assignees View on GitHub
Dominant language
Java
Stars
19k
Forks
3.5k
PR merge metrics
No merged PRs in 30d

Description

When a grammar's action use a parser rule attribute, and have a rule have a same name. like bellow
The generate parser code can not compile. because ANTLR tool add a redundant LabelRef after PropertiesRef。

grammar file :
`
grammar A;

IO: 'a'..'z'+;
decl: text ID ';'
{System.out.println($text + $start);}
;

text: 'int' | 'float' ;
start: 'start';
`

the generated action part:
` System.out.println(_input.getText(_localctx.start, _input.LT(-1))((DeclContext)_localctx).text + _localctx.start((DeclContext)_localctx).start);
`

Contributor guide

Open the contributing guide

Research direction

Start by reproducing the issue with the grammar A example and inspect the generated action containing PropertiesRef and the redundant LabelRef. Trace how the parser generator resolves the rule named text and the parser rule attribute start. Done means the generated parser compiles and emits correct references for both.

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.