我正在为其编写解析器的语言具有三个与此处相关的结构:由ord
表示的运算符,TOK_ORD
它将字符表达式转换为整数表达式,以及[
]
和.
,分别用于索引和字段访问,就像在 C 中一样。
这是我的优先规则的摘录:
%right TOK_ORD
%left PREC_INDEX PREC_MEMBER
我的语法有一个非终结符expr
,它代表表达式。这是语法中的一些相关片段(TOK_IDENT
是我.l
文件中定义的标识符的正则表达式):
expr : TOK_ORD expr { /* semantic actions */ }
| variable { /* semantic actions */ }
;
variable : expr '[' expr ']' %prec PREC_INDEX { /* semantic actions */ }
| expr '.' TOK_IDENT %prec PREC_MEMBER { /* semantic actions */ }
;
以下是野牛输出文件中有关移位/减少冲突的信息:
state 61
42 expr: expr . '=' expr
43 | expr . TOK_EQ expr
44 | expr . TOK_NE expr
45 | expr . TOK_LT expr
46 | expr . TOK_LE expr
47 | expr . TOK_GT expr
48 | expr . TOK_GE expr
49 | expr . '+' expr
50 | expr . '-' expr
51 | expr . '*' expr
52 | expr . '/' expr
53 | expr . '%' expr
57 | TOK_ORD expr .
72 variable: expr . '[' expr ']'
73 | expr . '.' TOK_IDENT
'[' shift, and go to state 92
'.' shift, and go to state 93
'[' [reduce using rule 57 (expr)]
'.' [reduce using rule 57 (expr)]
$default reduce using rule 57 (expr)
州 92 和 93,供参考:
state 92
72 variable: expr '[' . expr ']'
TOK_FALSE shift, and go to state 14
TOK_TRUE shift, and go to state 15
TOK_NULL shift, and go to state 16
TOK_NEW shift, and go to state 17
TOK_IDENT shift, and go to state 53
TOK_INTCON shift, and go to state 19
TOK_CHARCON shift, and go to state 20
TOK_STRINGCON shift, and go to state 21
TOK_ORD shift, and go to state 22
TOK_CHR shift, and go to state 23
'+' shift, and go to state 24
'-' shift, and go to state 25
'!' shift, and go to state 26
'(' shift, and go to state 29
expr go to state 125
allocator go to state 44
call go to state 45
callargs go to state 46
variable go to state 47
constant go to state 48
state 93
73 variable: expr '.' . TOK_IDENT
我不明白为什么会有移位/减少冲突。我的语法清楚地定义了索引和字段访问的优先级高于ord
,但这似乎还不够。
如果您想知道,是的,这是家庭作业,但作业已经上交并评分。我要回过头来尝试修复班次/减少冲突,以便更好地了解发生了什么。