gpt4 book ai didi

compiler-errors - 如何在 Bison 码中解决此错误?

转载 作者:行者123 更新时间:2023-12-02 10:56:26 25 4
gpt4 key购买 nike

我在编译野牛代码时遇到问题,尝试编译此代码:

 %{
#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相同,因此是无限递归的,这就是规则无法匹配任何内容的原因。

这同样适用于您尝试使用多个替代方法的其他规则。

关于compiler-errors - 如何在 Bison 码中解决此错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62478843/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com