gpt4 book ai didi

c - 解析 - 导致移位/归约冲突的运算符

转载 作者:太空宇宙 更新时间:2023-11-04 08:01:39 26 4
gpt4 key购买 nike

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

Bison :

%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冲突都来 self 的这部分代码)

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)

最佳答案

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

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

  2. 您需要根据语法以正确的顺序列出优先关系。 (例如,& 几乎没有 * 绑定(bind)得那么紧密。)另外,+- 被保留联想。

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

关于c - 解析 - 导致移位/归约冲突的运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46267343/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com