如何避免yacc C语法乘法和指针之间的冲突?

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

我是yacc初学者

C语法乘法和指针

a = 10 ***** b

如何区分乘法和指针?

我认为只能根据b的类型推论?

实际上,我看到了一些例子:http://www.quut.com/c/ANSI-C-grammar-l-2011.htmlhttps://github.com/JavinYang/go-1/blob/release-branch.go1/src/cmd/gc/go.y

我不明白为什么这两个示例不会引起减少/减少冲突

我模仿上面的代码将导致冲突

logical_and_expression
    : equality_expression
    | logical_and_expression LOGICAL_AND equality_expression
    {
        $$ = 0;
    }
    ;
equality_expression
    : relational_expression
    | equality_expression EQ relational_expression
    {
        $$ = 0;
    }
    | equality_expression NE relational_expression
    {
        $$ = 0;
    }
    ;
relational_expression
    : additive_expression
    | relational_expression GT additive_expression
    {
        $$ = 0;
    }
    | relational_expression GE additive_expression
    {
        $$ = 0;
    }
    | relational_expression LT additive_expression
    {
        $$ = 0;
    }
    | relational_expression LE additive_expression
    {
        $$ = 0;
    }
    ;
additive_expression
    : multiplicative_expression 
    | additive_expression ADD multiplicative_expression
    {
    }
    | additive_expression SUB multiplicative_expression
    {
    };
multiplicative_expression
    : unary_expression
    | multiplicative_expression MUL unary_expression
    {
    }
    | multiplicative_expression DIV unary_expression
    {
    }
unary_expression
    : primary_expression
    | MUL unary_expression // cause reduce/reduce conflicts
    {
    }
    | SUB unary_expression
    {
    }
    | EXCLAMATION unary_expression
    {
    }
    | BIT_XOR unary_expression
    {
    }
    ;

y.output冲突行:

state 85

   79 multiplicative_expression: unary_expression .
   83 unary_expression: MUL unary_expression .

    LOGICAL_AND  reduce using rule 79 (multiplicative_expression)
    LOGICAL_AND  [reduce using rule 83 (unary_expression)]
    LOGICAL_OR   reduce using rule 79 (multiplicative_expression)
    LOGICAL_OR   [reduce using rule 83 (unary_expression)]
    EQ           reduce using rule 79 (multiplicative_expression)
    EQ           [reduce using rule 83 (unary_expression)]
    NE           reduce using rule 79 (multiplicative_expression)
    NE           [reduce using rule 83 (unary_expression)]
    GT           reduce using rule 79 (multiplicative_expression)
    GT           [reduce using rule 83 (unary_expression)]
    GE           reduce using rule 79 (multiplicative_expression)
    GE           [reduce using rule 83 (unary_expression)]
    LT           reduce using rule 79 (multiplicative_expression)
    LT           [reduce using rule 83 (unary_expression)]
    LE           reduce using rule 79 (multiplicative_expression)
    LE           [reduce using rule 83 (unary_expression)]
    ADD          reduce using rule 79 (multiplicative_expression)
    ADD          [reduce using rule 83 (unary_expression)]
    SUB          reduce using rule 79 (multiplicative_expression)
    SUB          [reduce using rule 83 (unary_expression)]
    MUL          reduce using rule 79 (multiplicative_expression)
    MUL          [reduce using rule 83 (unary_expression)]
    DIV          reduce using rule 79 (multiplicative_expression)
    DIV          [reduce using rule 83 (unary_expression)]
    NEW_LINE     reduce using rule 79 (multiplicative_expression)
    NEW_LINE     [reduce using rule 83 (unary_expression)]
    $default     reduce using rule 79 (multiplicative_expression)
yacc
1个回答
2
投票

没有冲突,因为乘法是中缀,而指针解除引用是前缀,因此可以在LALR(1)解析器中轻松地将它们与上下文区分开。您在上面发布的片段没有冲突,因此您显示的冲突实际上是来自其他规则。

[最有可能来自一个规则,该规则允许两个连续的表达式之间没有标记来分隔它们,例如(function_application: expression expression ;)。如果您将这样的规则与运算符结合使用,这些运算符可以是前缀或前缀(例如-*),则结果不明确,因为a - b可以解析为(a) (- b)(函数应用程序+一元)或二进制运算。要消除歧义(以及对函数应用程序的结果应用一元运算的相关歧义),可以使函数应用程序的操作数为primary:function_application: primary_expression | primary_expression function_application ;

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