生成的解析器中出现错误“错误:没有声明匹配”

问题描述 投票:0回答:1

我的 C++ 文件中的这个函数正在与 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;
}

语法定义为:

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;

C++ 文件是用

antlr4 -Dlanguage=Cpp CAsyntax.g4

生成的

我尝试了很多方法,包括使用antlr建议的cmake方法。

我正在编译我的代码:

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

出于某种原因,执行

-lantlr4-runtime
而不是
-I/usr/include/antlr4-runtime
会导致无法找到“antlr4-runtime.h”的致命错误。

我使用的是antlr版本4.13.1-1

我希望它能给我一个以字符串形式输出到标准输出的简单树,就像我编译的演示示例一样,并且它有效(使用 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
c++ g++ antlr antlr4
1个回答
0
投票

在你的语法中“rule”是一个定义的关键字,将其替换为 law 就可以了

© www.soinside.com 2019 - 2024. All rights reserved.