我正在学习与YACC一起工作,我真的无法理解一些错误。我想知道为什么当我尝试使用.y文件生成解析器时出现此错误。这是我的.y文件,代表Pascal语言的上下文无关语法:
%%
Program : program ident ';' declaration_opc compound_stmt '.'
;
declaration_opc : var declaration_list
|
;
declaration_list : declaration ';' declaration_list
| declaration ';'
;
declaration : id_list ':' type
;
id_list : ident
| ident ',' id_list
;
type : integer
| BOOLEAN
;
proc_dec : proc_header forward ';'
| proc_header declaration_list compound_stmt
| func_header forward ';'
| func_header declaration_list compound_stmt
;
proc_header : procedure ident parametros ';'
;
func_header : function ident parametros : type
;
parametros : '(' param_list ')'
|
;
param_list : arg
| arg ';' param_list
;
arg : id_list ':' type
| var id_list ':' type
;
compound_stmt : begin statement_list end
;
statement_list : statement ';' statement_list
| statement
;
statement : ident attrib expression
| IF expression then statement
| IF expression then statement ELSE statement
| WHILE expression DO statement
| compound_stmt
| readln ident
| writeln print_list
|
;
print_list : literal print_list2
| expression print_list2
;
print_list2 : ',' print_list
|
;
expression : add_expression relop add_expression
| add_expression
;
relop : lessequal
| '<'
| '>'
| moreequal
| '='
| notequal
;
add_expression : add_expression addop term
;
addop : '+'
| '-'
| or
;
term : term mulop unary_exp
| unary_exp
;
mulop : '*'
| div
| mod
| and
;
unary_exp : not unary_exp
| factor
;
factor : '(' expression ')'
| ident
| num
| TRUE
| FALSE
;
%%
而且我总是这样:
byaccj: 19 rules never reduced
byaccj: 1 shift/reduce conflict.
有什么可能的解决方案?我发现其他人也有同样的错误,但是我找不到对我的问题有用的东西。如果需要更多信息,请提供。我还读到“规则从不减少”错误,这意味着从不使用语法中的某些规则,但是我的规则无法看到这一点。
在您的情况下,该规则proc_dec
永远不会出现在任何其他规则的右侧,因此永远不会从您开始的符号(Program
)到达。Yacc只是告诉您该规则(以及它使用的所有规则以及其他所有规则)均无法达到。
通常,您希望使用该-v
选项来使yacc生成包含y.output
有关语法的详细信息的文件。该文件将具体告诉您所有冲突和未使用的规则-它们是什么以及如何发生-而消息仅是对问题的摘要。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句