kaby76 / Antlr4BuildTasks

Third-party build tool for 'Official' Antlr4 tool and runtime parsers using .Net. Drop-in replacement for 'Antlr4cs' Antlr4 tool and build rules.
MIT License
73 stars 10 forks source link

Creates parser that uses wrong fieldname when referencing field causing CS1001. #81

Closed mrt181 closed 4 months ago

mrt181 commented 4 months ago

I am using this file (had to add .txt to it to upload it here): SqlBase.g4.txt

This creates the SqlBaseParser with Error CS1001 : Identifier expected on line 7895.

    ((DereferenceContext)_localctx).base = _prevctx;

The field that should be referenced is on line 7097

    public PrimaryExpressionContext @base;

Updating line 7895 manually to @base fixes the compile error.

kaby76 commented 4 months ago

This is a problem with the Antlr4 tool. The problem is that the tool should internally rename "base" to "base_" to avoid the "symbol conflict" in the grammar. For background, read https://github.com/antlr/antlr4/pull/3451. Although the code that was updated supposedly renames "base" as a parser or lexer symbol, it seems it doesn't rename if the name is used as an attribute.

Please raise this issue in Antlr4: https://github.com/antlr/antlr4/issues

As you discovered, the workaround is to painfully rename the offending symbol. You could use Trash to do this automatically.

02/23-05:23:10 ~/test
$ trparse SqlBase.g4 | trrename 'base,base_' | trsponge -o xxx
Using built-in parser.
CSharp 0 SqlBase.g4 success 0.0674912
Writing to xxx/SqlBase.g4
02/23-05:23:57 ~/test
$ diff xxx/SqlBase.g4 .
231c231
<     | base_=primaryExpression STRUCT_FIELD_REF ASTERISK   #selectStructAll
---
>     | base=primaryExpression STRUCT_FIELD_REF ASTERISK   #selectStructAll
331c331
<     | base_=primaryExpression STRUCT_FIELD_REF fieldName=identifier                        #dereference
---
>     | base=primaryExpression STRUCT_FIELD_REF fieldName=identifier                        #dereference
02/23-05:24:06 ~/test
$