我怎样才能修复野牛代码中的这个错误?
how can i fix this error in a bison code?
我在编译 bison 代码时遇到问题,正在尝试编译此代码:
%{
#include <stdio.h>
#include <stdlib.h>
#include <stdstring.h>
//#include "list.h"
int yylex();
int errore = 0;
void yyerror(char const *s);
%}
%union{
char *stringa;
double reale;
int intero;
}
%start Input
%token <stringa> MATR
%token <reale> CFU COD_M
%token <intero> ANNO_C VOTO
%token AA DATA MATERIA SEP1 SEP2 VIR LANC PEV PAR_I PAR_F TRAT NOME
%define parse.error verbose
%%
Input : AA DATA SEP1 Corsi SEP2 Studenti
;
Corsi : El_Materie
;
El_Materie : Materie
El_Materie Materie
;
Materie : PAR_I COD_M VIR MATERIA VIR ANNO_C VIR CFU PAR_F
;
Studenti : El_Studenti
;
El_Studenti : Studente Lista_M
El_Studenti Studente Lista_M
;
Studente : MATR LANC NOME PEV ANNO_C PEV
;
Lista_M : Esame
Lista_M Esame
;
Esame : COD_M VIR VOTO TRAT
;
%%
void yyerror(char const *s){
fprintf(stderr, "Errore: %s\n", s);
}
int main(void){
if(yyparse() == 0){
print();
}
return 0;
}
它返回这个错误:
Carriera1.y: warning: 10 nonterminals useless in grammar [-Wother]
Carriera1.y: warning: 10 rules useless in grammar [-Wother]
Carriera1.y:16.8-13: fatal error: no phrase is derived from the initial symbol Start
16 | %start Input
我该如何解决这个错误?
windows CMD 和 Linux Bash 中的错误相同。
我尝试使用其他初始符号,但错误是相同的。
提前致谢
更新:
我有一个新的错误
使用“-Wall -Werror”进行编译
错误是:
Carriera.fl: In function ‘yylex’:
Carriera.fl:38:17: error: assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Werror=int-conversion]
38 | {Matr} {yylval.stringa = strdup(yytext); return(MATR);}
| ^
At top level:
lex.yy.c:1251:16: error: ‘input’ defined but not used [-Werror=unused-function]
1251 | static int input (void)
| ^~~~~
lex.yy.c:1208:17: error: ‘yyunput’ defined but not used [-Werror=unused-function]
1208 | static void yyunput (int c, char * yy_bp )
| ^~~~~~~
谢谢
要定义具有多个备选方案的规则,请使用 |
运算符分隔备选方案,而不是换行符。您当然可以保留换行符以提高可读性,但 |
运算符实际上是分隔备选方案的。
Lista_M : Esame
Lista_M Esame
这与 Lista_M : Esame Lista_M Esame
相同,因此无限递归,这就是规则无法匹配任何内容的原因。
这同样适用于您尝试使用多个备选方案的其他规则。
我在编译 bison 代码时遇到问题,正在尝试编译此代码:
%{
#include <stdio.h>
#include <stdlib.h>
#include <stdstring.h>
//#include "list.h"
int yylex();
int errore = 0;
void yyerror(char const *s);
%}
%union{
char *stringa;
double reale;
int intero;
}
%start Input
%token <stringa> MATR
%token <reale> CFU COD_M
%token <intero> ANNO_C VOTO
%token AA DATA MATERIA SEP1 SEP2 VIR LANC PEV PAR_I PAR_F TRAT NOME
%define parse.error verbose
%%
Input : AA DATA SEP1 Corsi SEP2 Studenti
;
Corsi : El_Materie
;
El_Materie : Materie
El_Materie Materie
;
Materie : PAR_I COD_M VIR MATERIA VIR ANNO_C VIR CFU PAR_F
;
Studenti : El_Studenti
;
El_Studenti : Studente Lista_M
El_Studenti Studente Lista_M
;
Studente : MATR LANC NOME PEV ANNO_C PEV
;
Lista_M : Esame
Lista_M Esame
;
Esame : COD_M VIR VOTO TRAT
;
%%
void yyerror(char const *s){
fprintf(stderr, "Errore: %s\n", s);
}
int main(void){
if(yyparse() == 0){
print();
}
return 0;
}
它返回这个错误:
Carriera1.y: warning: 10 nonterminals useless in grammar [-Wother]
Carriera1.y: warning: 10 rules useless in grammar [-Wother]
Carriera1.y:16.8-13: fatal error: no phrase is derived from the initial symbol Start
16 | %start Input
我该如何解决这个错误? windows CMD 和 Linux Bash 中的错误相同。 我尝试使用其他初始符号,但错误是相同的。
提前致谢
更新:
我有一个新的错误 使用“-Wall -Werror”进行编译
错误是:
Carriera.fl: In function ‘yylex’:
Carriera.fl:38:17: error: assignment to ‘char’ from ‘char *’ makes integer from pointer without a cast [-Werror=int-conversion]
38 | {Matr} {yylval.stringa = strdup(yytext); return(MATR);}
| ^
At top level:
lex.yy.c:1251:16: error: ‘input’ defined but not used [-Werror=unused-function]
1251 | static int input (void)
| ^~~~~
lex.yy.c:1208:17: error: ‘yyunput’ defined but not used [-Werror=unused-function]
1208 | static void yyunput (int c, char * yy_bp )
| ^~~~~~~
谢谢
要定义具有多个备选方案的规则,请使用 |
运算符分隔备选方案,而不是换行符。您当然可以保留换行符以提高可读性,但 |
运算符实际上是分隔备选方案的。
Lista_M : Esame
Lista_M Esame
这与 Lista_M : Esame Lista_M Esame
相同,因此无限递归,这就是规则无法匹配任何内容的原因。
这同样适用于您尝试使用多个备选方案的其他规则。