2014-01-05 24 views
1

我正在研究bison C++分析器。 大多數示例都在.y文件中有參數location&的錯誤方法,但我不知道如何讓location_type調用此方法。處理位置信息bison C++分析器錯誤

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 declared in this scope

+0

可能與[SO/how-does-flex-support-bison-location-exactly]有關(http://stackoverflow.com/questions/656703/how-does-flex-support-bison-location-exactly ) – Jarod42

回答

1

你的問題有點不清楚:你想從哪裏撥打yyerror

如果你想從解析器調用它,那麼就用@n僞變量:

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

如果你想從掃描儀調用它,然後用用有表示當前變量位置,可能類似於yylloc

如果你想從其他地方調用它(例如,從AST遍歷,但那會很奇怪),然後找到那裏的位置。

但更重要的(對不起,我可以指出你已經知道的東西):要知道,你通常不需要調用yyerror:你必須提供它,這樣解析器可以拋出錯誤。對yyerror的典型調用位於生成的代碼中,而不是您希望編寫的代碼中。

+0

這是@n。謝謝! – prosseek