gpt4 book ai didi

c - 如何修复 yacc 警告属性未类型化并出现文件意外结束的 fatal error

转载 作者:太空宇宙 更新时间:2023-11-04 06:14:56 25 4
gpt4 key购买 nike

我想在 Yacc 中编写 moy 的计算代码,所以我做了这段代码

%{
#include <ctype.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
// Fonction qui permet de claculer la moyenne
double moy(int*ls,int size )
{
int som=0;
for(int j=0; j<size;j++)
som+=ls[j];
return (som/size);
}

/* La structure de la table*/
%}

%union {
int value;
struct s{int *ls; int size;} str;
}
%token nb
%type <value> E
%type <str> L
%left '+','-'
%left '*','/'
//%right '^'
//%right moins_unaire
%%
Ligne : E '\n' {printf("%d \n",$1);}
;

E : E '-' T {$$=$1-$3;}
| E '+' T {$$=$1+$3;}
| T {$$=$1;}
;
T : T '*' F
| T '/' F
| F
;
F : '(' E ')' {$$=$2;}
| nb {$$=$1;}
| fct {$$=$1;}
;
fct: nf '('L ')' {if ($1==1) {
$$= moy($3.ls,$3.size);
}
;
nf : 'moy' {$$=1;}
;
L : L ',' E {$$.ls=$1; $$.ls[$$.size++]=$3;}
| E {$$.size=1; $$.ls=malloc(50); $$.ls[0]=$1;}
;
%%
main ()
{
yyparse();
}

但是一旦我编译了它,我就收到了这些警告:

编译...月

C:\Users\LE\Desktop\exos\moy\mo.y(35) : warning Y4003: '$3' : attribute is untyped
C:\Users\LE\Desktop\exos\moy\mo.y(36) : warning Y4003: '$3' : attribute is untyped
C:\Users\LE\Desktop\exos\moy\mo.y(37) : warning Y4003: '$1' : attribute is untyped
C:\Users\LE\Desktop\exos\moy\mo.y(43) : warning Y4003: '$$' : attribute is untyped
C:\Users\LE\Desktop\exos\moy\mo.y(44) : warning Y4003: '$$' : attribute is untyped
C:\Users\LE\Desktop\exos\moy\mo.y(44) : warning Y4003: '$1' : attribute is untyped
C:\Users\LE\Desktop\exos\moy\mo.y(45) : warning Y4003: '$$' : attribute is untyped
C:\Users\LE\Desktop\exos\moy\mo.y(45) : warning Y4003: '$1' : attribute is untyped
C:\Users\LE\Desktop\exos\moy\mo.y(47) : warning Y4003: '$1' : attribute is untyped
C:\Users\LE\Desktop\exos\moy\mo.y(48) : warning Y4003: '$$' : attribute is untyped
C:\Users\LE\Desktop\exos\moy\mo.y(51) : warning Y4003: '$$' : attribute is untyped
C:\Users\LE\Desktop\exos\moy\mo.y(53) : warning Y4003: '$$' : attribute is untyped
C:\Users\LE\Desktop\exos\moy\mo.y(53) : warning Y4003: '$1' : attribute is untyped
C:\Users\LE\Desktop\exos\moy\mo.y(53) : warning Y4003: '$$' : attribute is untyped
C:\Users\LE\Desktop\exos\moy\mo.y(53) : warning Y4003: '$$' : attribute is untyped
C:\Users\LE\Desktop\exos\moy\mo.y(54) : warning Y4003: '$$' : attribute is untyped
C:\Users\LE\Desktop\exos\moy\mo.y(54) : warning Y4003: '$$' : attribute is untyped
C:\Users\LE\Desktop\exos\moy\mo.y(54) : warning Y4003: '$$' : attribute is untyped
C:\Users\LE\Desktop\exos\moy\mo.y(54) : warning Y4003: '$1' : attribute is untyped
C:\Users\LE\Desktop\exos\moy\mo.y(62) : fatal error Y1015: unexpected end of file found in action

g - 1 error(s), 19 warning(s)

我注意到一旦我删除了这两行:

%type <value> E
%type <str> L

未类型化错误消失。我进行了很多搜索,但未能解决问题。

我该怎么办?

最佳答案

你需要

%type <value> E T F

修复类型错误。

正如@JeffMercado 所指出的,过早的 EOF 是由过多的第一个大括号引起的

fct: nf '('L ')' {if ($1==1) {

%left '+','-' 
%left '*','/'

这是不对的。逗号应该是空格。

关于c - 如何修复 yacc 警告属性未类型化并出现文件意外结束的 fatal error ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47897150/

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