【问题标题】:parsing - operators causing shift/reduce conflict解析 - 导致移位/减少冲突的运算符
【发布时间】:2017-09-17 18:11:53
【问题描述】:

我正在尝试编写一个基本的 C 解析器,但我遇到了许多 shift/reduce 冲突。我已经尝试了其他问题的许多建议,但似乎没有任何效果。我试图添加一些基于 C 语言的优先级,但我不断得到转移/减少冲突。我错过了什么?任何帮助将不胜感激!

野牛:

%union {char* var; char* type} 

%token <type> INT  DOUBLE BOOL CHAR
%token FOR WHILE 
%token IF ELSE PRINTF 
%token BYREF BREAK CONTINUE RETURN DELETE FALSE TRUE NEW NULL VOID
%token NUM 
%token INCLUDE
%token DOT
%token <var> ID 
%nonassoc ELSE
%right '[' ']'
%right    '!'  '-' '+' '?' ':'
%left AND OR 
%right  DELETE
%left  '&' '*'  '/' '%'
%left '<' '>' LE GE EQ NE LT GT
%right MUL_AS DIV_AS ADD_AS MIN_AS MOD_AS INCR DCRS 
%error-verbose


%%


 expr
   : ID
   | TRUE
   | FALSE
   | NULL
   |"(" expr ")"
   | ID "(" expr_list ")"
   | expr "[" expr "]"
   | expr '&' expr
   | expr '*' expr 
   | expr '!' expr
   | expr INCR
   | expr DCRS
   | expr '=' expr                %prec '='
   | expr DIV_AS expr            %prec MOD_AS
   | expr MOD_AS expr
   | expr MUL_AS expr
   | expr ADD_AS expr
   | expr MIN_AS expr
   | expr LE expr
   | expr GE expr
   | expr NE expr
   | expr EQ expr
   | expr GT expr
   | expr LT expr
   | "(" data_type ")" expr
   | expr "?" expr ":" expr
   | NEW data_type
   | NEW data_type "[" expr "]" 
   | DELETE expr

   ;

部分报告:(几乎所有的 shift/reduce 冲突都来自我的这部分代码)

State 88

   49 expr: expr . "[" expr "]"
   50        | expr . '&' expr
   51        | expr . '*' expr
   52        | expr . '!' expr
   53        | expr . INCR
   54        | expr . DCRS
   55        | expr . '=' expr
   55        | expr '=' expr .  ['!', '&', '*', LE, GE, EQ, NE, LT, GT, MUL_AS, DIV_AS, ADD_AS, MIN_AS, MOD_AS, INCR, DCRS, ";", "[", "]", ")", ')', ';', '=', "?", ":", ',']
   56        | expr . DIV_AS expr
   57        | expr . MOD_AS expr
   58        | expr . MUL_AS expr
   59        | expr . ADD_AS expr
   60        | expr . MIN_AS expr
   61        | expr . LE expr
   62        | expr . GE expr
   63        | expr . NE expr
   64        | expr . EQ expr
   65        | expr . GT expr
   66        | expr . LT expr
   68        | expr . "?" expr ":" expr

    '!'     shift, and go to state 44
    '&'     shift, and go to state 45
    '*'     shift, and go to state 46
    LE      shift, and go to state 47
    GE      shift, and go to state 48
    EQ      shift, and go to state 49
    NE      shift, and go to state 50
    LT      shift, and go to state 51
    GT      shift, and go to state 52
    MUL_AS  shift, and go to state 53
    DIV_AS  shift, and go to state 54
    ADD_AS  shift, and go to state 55
    MIN_AS  shift, and go to state 56
    MOD_AS  shift, and go to state 57
    INCR    shift, and go to state 58
    DCRS    shift, and go to state 59
    "["     shift, and go to state 60
    '='     shift, and go to state 61
    "?"     shift, and go to state 62

    '!'       [reduce using rule 55 (expr)]
    '&'       [reduce using rule 55 (expr)]
    '*'       [reduce using rule 55 (expr)]
    LE        [reduce using rule 55 (expr)]
    GE        [reduce using rule 55 (expr)]
    EQ        [reduce using rule 55 (expr)]
    NE        [reduce using rule 55 (expr)]
    LT        [reduce using rule 55 (expr)]
    GT        [reduce using rule 55 (expr)]
    MUL_AS    [reduce using rule 55 (expr)]
    DIV_AS    [reduce using rule 55 (expr)]
    ADD_AS    [reduce using rule 55 (expr)]
    MIN_AS    [reduce using rule 55 (expr)]
    MOD_AS    [reduce using rule 55 (expr)]
    INCR      [reduce using rule 55 (expr)]
    DCRS      [reduce using rule 55 (expr)]
    "["       [reduce using rule 55 (expr)]
    '='       [reduce using rule 55 (expr)]
    "?"       [reduce using rule 55 (expr)]
    $default  reduce using rule 55 (expr)

【问题讨论】:

  • 替代| expr '=' expr        %prec '=' 可能是造成大部分麻烦的原因。作为一项临时措施,将其删除,看看有多少变化。我不确定您是如何组织运算符优先级的;我在列表中没有看到 '=',所以我不知道 Bison 是如何解释该优先级的。等号是否也是另一个运算符,例如EQ?你有'&lt;'LT 作为比较器规则——有什么区别?这可能无关紧要,但如果没有完整信息,外人会感到困惑。

标签: c parsing compiler-construction bison


【解决方案1】:

如果您使用优先声明来消除表达式解析的歧义:

  1. 您需要声明 every 运算符。 (在您引用的转换状态中,错误与 = 相关,您尚未声明其优先级。)

  2. 您需要根据语法以正确的顺序列出优先关系。 (例如,&amp; 的绑定不如* 紧密。)另外,+- 保持关联。

或者,您可以使用 C 标准中的 C 语法中的表达式语法作为基础。它是用明确的优先级编写的——即不需要优先级声明。在任何一种情况下,花时间去理解语法是如何工作的,无论是作为语言的描述还是驱动解析都是值得的。优先级声明通过强制解析器将某些歧义明确地解析为移位或减少来工作,因此它们在实践中与不使用优先级的明确语法非常相似。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-03
    • 1970-01-01
    • 2020-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多