c++ - 处理位置信息 bison c++ 解析器错误

标签 c++ bison

我正在开发 bison c++ 解析器。 大多数示例在 .y 文件中都有带有参数 location& 的错误方法,但我不确定如何获取 location_type 来调用此方法。

typedef location location_type;
void
yy::c_parser::error (const location_type& l,
                          const std::string& m)
{
  driver.error (l, m);
}

这是摘自 http://panthema.net/2007/flex-bison-cpp-example/ 的示例,

if (!driver.calc.existsVariable(*$1)) {
           error(yyloc, std::string("Unknown variable \"") + *$1 + "\"");

但是,我在编译时遇到了一个错误:parser.yy:109: error: ‘yyloc’ was not returned in this scope

最佳答案

您的问题有点不清楚:您想从哪里调用yyerror

如果你想从解析器调用它,那么只需使用@n伪变量:

exp: exp "/" exp
  {
    if (!$3)
      {
         yyerror(@3, "division by zero");
         YYERROR;
      }
    else
      {
         $$ = $1 / $3;
      }
  }

如果您想从扫描仪调用它,请使用那里使用的变量来表示当前位置,可能类似于 yylloc

如果你想从其他地方调用它(例如,从 AST 遍历,但这会很奇怪),那么找到那里的位置。

但更重要的是(抱歉,我可能会说一些您已经知道的事情):请注意,您通常不需要调用 yyerror:您必须提供它,以便解析器可以引发错误。对 yyerror 的典型调用是在生成的代码中,而不是在您期望编写的代码中。

关于c++ - 处理位置信息 bison c++ 解析器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20940473/

相关文章:

c++ - 如何更改 C++ 引用所引用的变量?

无法在 bison/flex 中进行多项作业

parsing - 警告 : nonterminal useless in grammar: const_declaration [-Wother]

c++ - 如何在 DirectX 上沿 XYZ 轴旋转对象?

visual-studio-2010 - bison.exe 找不到 VS2010 中的 MSBuild 所需的文件 m4sugar.m4

parsing - Lex/Flex 中的开始状态

bison - 如何解决Bison警告 "... has no declared type"

c++ - 在条件中使用提取运算符的返回值?

c++ - 哪个头文件包含 DirectX 12 中的 ThrowIfFailed()

c++ - Tensorflow load graph c++ 示例中包含哪些 header ?