如何修复yacc警告属性是无类型的,具有致命错误意外结束文件

问题描述 投票:0回答:1

我想在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();
}

但是一旦我编译它,我得到了那些警告:

编译...... mo.y

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

无类型错误消失。我搜索了很多,但我无法解决问题。

我该怎么办 ?

c yacc
1个回答
4
投票

你需要

%type <value> E T F

修复类型错误。

正如@JeffMercado所指出的那样,过早的EOF是由多余的第一支撑引起的

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

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

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

© www.soinside.com 2019 - 2024. All rights reserved.