2016-01-29 152 views
0

这是我的野牛解析器代码。野牛表达式解析失败C++

statement 
    : compound_statement 
    | expression_statement 
    ; 



expression_statement 
    : ';' 
    | expression ';'      {cout<<"expr found\n";} 
    ; 

expression 
    : number_expression     {cout << "number expression\n";} 
    ; 

number_expression 
    : number_arith_expression    {cout << "arith expression\n";} 
    ; 

number_arith_expression 

    : number_idenfiers 
    | number_constants 
    | number_arith_expression number_arith_expression '+' { cout<<"found\n";} 
    | number_arith_expression number_arith_expression '-' {} 

    ; 

number_idenfiers 
    : PPNUMVAR             {} 
    | PPSYSNUMVAR            {} 
    ; 

number_constants 
    : PPFLOAT             {} 
    ; 

我给表达

23 23 +;

我的结果是:

发现

ARITH表达

数量表达

行号:1 ..解析错误

为什么它不解析expression_statment?它不解析SEMICOLON。我应该更改以使用分号?

+0

最有可能的是您的词法扫描仪不正确地返回分号令牌,但很难知道没有看到扫描仪。为什么不使用bison的内置调试工具,它会给你更多的信息,包括它实际看到的令牌。请参阅https://www.gnu.org/software/bison/manual/bison.html#Enabling-Traces – rici

+0

谢谢rici。 –

回答

0

根据你的语法,一个有效的加法表达式将是两个表达式,后跟一个+,但是你的输入的中间是+

+0

哎呀!抱歉。我误入歧途。它是一个后缀表达式。现在我纠正了这个问题。 –