yacc的语法如下:
Successfully fetched weather data!
现在,当我在.y文件上运行bison时,我收到以下错误:
18. Stmt_list : Stmt | Stmt_list ';' Stmt;
19. Stmt : Variable assignop Expression;
20. Variable : id | id'['Expression']';
21. Expression : Simple_expression | Simple_expression relop Simple_expression;
22. Simple_expression : Term | Simple_expression addop Term;
23. Term : Factor | Term mulop Factor;
24. Factor : id | num | '('Expression')' | id'['Expression']' | not Factor;
我是野牛的完整菜鸟。我不知道如何在这里解决问题。
/ **输入和输出文件** /
输入:
subc.y: conflicts: 8 shift/reduce, 26 reduce/reduce
subc.y:21.21-45: warning: rule useless in parser due to conflicts: expr: simp_ex
pr relop simp_expr
subc.y:22.20-39: warning: rule useless in parser due to conflicts: simp_expr: si
mp_expr addop term
subc.y:23.17-33: warning: rule useless in parser due to conflicts: term: term mu
lop factor
subc.y:24.40-49: warning: rule useless in parser due to conflicts: factor: not f
actor
输出:
%{
#include<stdio.h>
#include "lex.yy.c"
void yyerror (char *s);
%}
/* Yacc definitions */
%start stmt_list
%token id
%token num
%token relop
%token mulop
%token addop
%token assignop
%token not
%%
/* descriptions of expected inputs corresponding actions (in C) */
stmt_list : stmt| stmt_list ';' stmt { printf("SUCCESS"); };
stmt : var assignop expr ;
var : id| id'['expr']' ;
expr : simp_expr | simp_expr relop simp_expr;
simp_expr : term| simp_expr addop term;
term : factor| term mulop factor;
factor : id| num| expr| id'['expr']'| not factor;
%%
void yyerror (char *s) {printf (stderr, "%s\n", s);}
答案 0 :(得分:1)
这很简单,构造:
id '[' expr ']' | id
可以是var
和factor
。
语法含糊不清,这就是野牛告诉你的......
解决方法是将其从factor
中删除,然后在其位置插入var
。
语法中也有一个循环:
expr -> simp_exp -> term -> factor -> expr
从expr
移除factor
以消除该循环,或反映原始语法中的内容,或许应该是:
'(' expr ')'
(你忘记了表达式的括号)