antlr4 icon indicating copy to clipboard operation
antlr4 copied to clipboard

AParser.cpp:121:23: error: no declaration matches ‘AParser::RuleContext* AParser::CmdContext::rule_()’

Open jirislaby opened this issue 9 months ago • 1 comments

With this grammar:

grammar A;

cmd : rule ;

rule : 'a' | 'b' ;

I see:

antlr4 -Dlanguage=Cpp A.g4 && g++ AParser.cpp -o /dev/null -I /usr/include/antlr4-runtime/ -c
AParser.cpp:121:23: error: no declaration matches ‘AParser::RuleContext* AParser::CmdContext::rule_()’
  121 | AParser::RuleContext* AParser::CmdContext::rule_() {
      |                       ^~~~~~~
In file included from AListener.h:8,
                 from AParser.cpp:5:
AParser.h:46:18: note: candidate is: ‘antlr4::RuleContext* AParser::CmdContext::rule_()’
   46 |     RuleContext *rule_();
      |                  ^~~~~
AParser.h:42:10: note: ‘class AParser::CmdContext’ defined here
   42 |   class  CmdContext : public antlr4::ParserRuleContext {
      |          ^~~~~~~~~~

It's becase that rule in the grammar creates confusion between generated AParser::RuleContext and antlr4::RuleContext. This fix is obvious. The generated AParser.h should have:

AParser::RuleContext* rule_();

instead of current:

RuleContext* rule_();

jirislaby avatar Mar 21 '25 09:03 jirislaby

Over in the CSharp target, we get this:

$ make
bash build.sh
Restore complete (0.8s)

Build succeeded in 1.2s
Restore complete (0.4s)
  Test succeeded with 1 warning(s) (2.8s) → bin\Debug\net8.0\Test.dll
    C:\msys64\home\Kenne\temp\Generated-CSharp\AParser.cs(129,23): warning CS0108: 'AParser.RuleContext' hides inherited member 'Parser.RuleContext'. Use the new keyword if hiding was intended.

Build succeeded with 1 warning(s) in 3.8s

Workload updates are available. Run `dotnet workload list` for more information.
03/21-07:19:52 ~/temp/Generated-CSharp

In Dart, we get this:

$ make
bash build.sh
Resolving dependencies... (2.6s)
+ _fe_analyzer_shared 67.0.0 (80.0.0 available)
+ analyzer 6.4.1 (7.3.0 available)
+ crypto 3.0.3 (3.0.6 available)
...
+ webkit_inspection_protocol 1.2.1
+ yaml 3.1.2 (3.1.3 available)
Changed 48 dependencies!
1 package is discontinued.
33 packages have newer versions incompatible with dependency constraints.
Try `dart pub outdated` for more information.
ABaseListener.dart:5:1: Error: 'RuleContext' is imported from both 'AParser.dart' and 'package:antlr4/src/rule_context.dart'.
import 'AParser.dart';
^^^^^^^^^^^
AListener.dart:5:1: Error: 'RuleContext' is imported from both 'AParser.dart' and 'package:antlr4/src/rule_context.dart'.
import 'AParser.dart';
^^^^^^^^^^^
Error: AOT compilation failed
Generating AOT kernel dill failed!
mingw32-make: *** [makefile:3: build] Error 64
03/21-07:22:59 ~/temp/Generated-Dart
$

In TypeScript, we get this:

$ make
bash build.sh
CLI_BASEDIR = C:\Program Files\nodejs

changed 20 packages in 3s
CLI_BASEDIR = C:\Program Files\nodejs

added 13 packages, and audited 14 packages in 2s

3 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities
AParser.ts:9:2 - error TS2395: Individual declarations in merged declaration 'RuleContext' must be all exported or all local.

9  RuleContext, ParserRuleContext, PredictionMode, PredictionContextCache,
   ~~~~~~~~~~~
...

In Antlr4ng/antlr-ng, we don't get any errors.

Using "rule" as a parser symbol causes a "symbol conflict." This was supposed to be taken care of by https://github.com/antlr/antlr4/pull/3451. Any fixes for Cpp should be checked and fixed for the other targets.

kaby76 avatar Mar 21 '25 11:03 kaby76