Fatal error: start symbol does not derive any sentence
Fatal error: start symbol does not derive any sentence
我正在尝试使用 lex 和 yacc 为计算器开发程序。我不断收到以下错误:
calc.y: warning: 5 nonterminals useless in grammar [-Wother]
calc.y: warning: 8 rules useless in grammar [-Wother]
calc.y:8.1: fatal error: start symbol I does not derive any sentence
I : E '\n' {printf("%d\n",);}
我看过类似的问题,但它们有无限递归,但这个没有。
calc.l
%{
#include"y.tab.h"
%}
digits [0-9]*
%%
{digits} {return DIGITS}
%%
int yywrap()
{
}
calc.y
%{
#include<stdio.h>
%}
%token DIGITS
%%
I : E '\n' {printf("%d\n",);}
;
E : E '+' F {$$ = + ;}
| E '-' F {$$ = - ;}
;
F : F '*' G {$$ = * ;}
| F '/' G {$$ = / ;}
G :'('E')'
| DIGITS
;
%%
int main()
{
yyparse();
}
int yyerror()
{
}
我不知道 yacc,但是:
要构建一个 I
,您需要一个 E
:
I : E '\n'
要构建一个 E
,您需要一个 E
:
E : E '+' F
| E '-' F
因为如果你还没有 E
就没有办法建造(一开始你什么都没有),所以没有办法建造 I
要么。
或者换个角度看:E
是无限递归的,因为它总是指向自身。
如果我们从词法分析器开始,我们首先得到 DIGITS
。
DIGITS
可用于构建G
.
但是我们对 G
无能为力,因为使用它的唯一规则(F '*' G
和 F '/' G
)也需要 F
才能继续,并且我们没有 F
。所以我们被困住了。
我正在尝试使用 lex 和 yacc 为计算器开发程序。我不断收到以下错误:
calc.y: warning: 5 nonterminals useless in grammar [-Wother]
calc.y: warning: 8 rules useless in grammar [-Wother]
calc.y:8.1: fatal error: start symbol I does not derive any sentence
I : E '\n' {printf("%d\n",);}
我看过类似的问题,但它们有无限递归,但这个没有。
calc.l
%{
#include"y.tab.h"
%}
digits [0-9]*
%%
{digits} {return DIGITS}
%%
int yywrap()
{
}
calc.y
%{
#include<stdio.h>
%}
%token DIGITS
%%
I : E '\n' {printf("%d\n",);}
;
E : E '+' F {$$ = + ;}
| E '-' F {$$ = - ;}
;
F : F '*' G {$$ = * ;}
| F '/' G {$$ = / ;}
G :'('E')'
| DIGITS
;
%%
int main()
{
yyparse();
}
int yyerror()
{
}
我不知道 yacc,但是:
要构建一个
I
,您需要一个E
:I : E '\n'
要构建一个
E
,您需要一个E
:E : E '+' F | E '-' F
因为如果你还没有 E
就没有办法建造(一开始你什么都没有),所以没有办法建造 I
要么。
或者换个角度看:E
是无限递归的,因为它总是指向自身。
如果我们从词法分析器开始,我们首先得到 DIGITS
。
DIGITS
可用于构建G
.
但是我们对 G
无能为力,因为使用它的唯一规则(F '*' G
和 F '/' G
)也需要 F
才能继续,并且我们没有 F
。所以我们被困住了。