Search code examples
c++g++antlrantlr4

Errors in generated parser "error: no declaration matches"


This function in my C++ file is interacting with antlr:

antlr4::tree::ParseTree* getTree(std::string caDefinition){
antlr4::ANTLRInputStream input(caDefinition);
CAsyntaxLexer lexer(&input);
antlr4::CommonTokenStream tokens(&lexer);

    tokens.fill();
    CAsyntaxParser parser(&tokens);
    auto tree = parser.prog();
    std::cout << tree->toStringTree(&parser) << std::endl << std::endl;
    return tree;
}

The syntax is defined as:

grammar CAsyntax;
prog: definition ';' rule ';';
definition: 'let' stateflag ':' NAME ',' COLOR
    | definition ';' definition;
stateflag: BOOLFLAG;
rule: rule ';' rule
    | stateflag '->' transition;
transition: state 'if' cond
    | state 'if' cond 'else' transition
    | '(' transition ')'
    | state;
cond: val COMPARISON val
    | cond OP cond
    | 'not' cond
    | '(' cond ')';
val: 'rand'
    | number
    | neighborhood '.' state;
number: INTEGER
    | REAL;
neighborhood: 'nm'
    | 'nv';
state: stateflag;

OP: '|' | '&' | '^';
COMPARISON: '>' | '<' | '=' | 'mod';
BOOLFLAG: 's'INTEGER;
COLOR: '#' [0-9a-f] [0-9a-f] [0-9a-f] [0-9a-f] [0-9a-f] [0-9a-f];
NAME: [a-zA-Z]+ ;
INTEGER: [0-9]+;
REAL: INTEGER '.' [0-9]+;
WS: [ \t\r\n] -> skip;

The C++ files were generated with antlr4 -Dlanguage=Cpp CAsyntax.g4

I've tried a number of methods, including using the cmake method that antlr suggests.

I'm compiling my code with:

g++ main.cpp grammar/*.cpp -lGL -lglfw -lGLEW -lpng -I/usr/include/antlr4-runtime -o automaton

For some reason, doing -lantlr4-runtime instead of -I/usr/include/antlr4-runtime gives fatal errors about being unable to find "antlr4-runtime.h".

I am using antlr version 4.13.1-1

I was hoping it would give me a simple tree in string form to stdout, much like in the demo example which I compiled, and it worked (using cmake).

main.cpp: In function ‘int main(int, char**)’:
main.cpp:319:45: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
  319 |         HotShader computeShader = HotShader("compute.glsl");
      |                                             ^~~~~~~~~~~~~~
grammar/CAsyntaxParser.cpp:157:30: error: no declaration matches ‘CAsyntaxParser::RuleContext* CAsyntaxParser::ProgContext::rule_()’
  157 | CAsyntaxParser::RuleContext* CAsyntaxParser::ProgContext::rule_() {
      |                              ^~~~~~~~~~~~~~
In file included from grammar/CAsyntaxListener.h:8,
                 from grammar/CAsyntaxParser.cpp:5:
grammar/CAsyntaxParser.h:59:18: note: candidate is: ‘antlr4::RuleContext* CAsyntaxParser::ProgContext::rule_()’
   59 |     RuleContext *rule_();
      |                  ^~~~~
grammar/CAsyntaxParser.h:54:10: note: ‘class CAsyntaxParser::ProgContext’ defined here
   54 |   class  ProgContext : public antlr4::ParserRuleContext {
      |          ^~~~~~~~~~~
grammar/CAsyntaxParser.cpp:1000:6: error: no declaration matches ‘bool CAsyntaxParser::sempred(RuleContext*, size_t, size_t)’
 1000 | bool CAsyntaxParser::sempred(RuleContext *context, size_t ruleIndex, size_t predicateIndex) {
      |      ^~~~~~~~~~~~~~
grammar/CAsyntaxParser.h:202:8: note: candidate is: ‘virtual bool CAsyntaxParser::sempred(antlr4::RuleContext*, size_t, size_t)’
  202 |   bool sempred(antlr4::RuleContext *_localctx, size_t ruleIndex, size_t predicateIndex) override;
      |        ^~~~~~~
grammar/CAsyntaxParser.h:12:8: note: ‘class CAsyntaxParser’ defined here
   12 | class  CAsyntaxParser : public antlr4::Parser {
      |        ^~~~~~~~~~~~~~
make: *** [makefile:8: build] Error 1

Solution

  • in your syntax "rule" is a defined keyword, replace it to law and it will work