Skip to content
This repository has been archived by the owner on Dec 11, 2024. It is now read-only.

语法问题 #82

Answered by LjwNautilus
Yawningx asked this question in Q&A
Mar 30, 2023 · 3 comments · 4 replies
Discussion options

You must be logged in to vote

确实是文法发生了 reduce-reduce conflict ,FuncType 和 BType 都可以直接推出 T_INT ,而且 T_INT 后面都可以接 T_IDENTIFIER ,bison 只看一个 symbol 不知道是规约成函数定义 FuncDef 还是变量声明 VarDecl 。报段错误是因为 bison 自动忽略了 FuncType 和 BType 中最靠后的规则,导致 parser 原来可以识别的很多结构都无法识别。

 // A wrong example
FuncDef: 
  FuncType T_IDENTIFIER T_L_PAREN T_R_PAREN Block {
    $$ = AddTreeNode($5, "FunctionDecl", $2->name);
    delete $2;
  }
  ;

VarDecl:
  BType VarDef T_SEMI {
    $$ = AddTreeNode($2, "VarDecl");
  }
  ;

VarDef:
  T_IDENTIFIER {
    delete $1;
  }
  | T_IDENTIFIER T_EQUAL InitVal {
    $$ = $3;
    delete $1;
  }
  ;

FuncType: T_VOID | T_INT;
BType: T_INT | T_CHAR | T_LONG T_LONG;

编译时遇到 conflict 会报 warning 但信息很少,可以直接用 bison 加上 -Wcountere…

Replies: 3 comments 4 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
3 replies
@Yawningx
Comment options

@feifeishenFFS
Comment options

@wufeng15226
Comment options

Comment options

You must be logged in to vote
1 reply
@Yawningx
Comment options

Answer selected by Yawningx
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants