DINA TAKLIT
DINA TAKLIT

Reputation: 8408

How to fix yacc warning attribute is untyped with fatal error unexpected end of file

I want to code the calculate of moy in Yacc so I do this code

%{
#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();
}

but once i compiled it i have gotten those warnings:

Compiling... 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)

I noticed that once I delete this two lines:

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

Untyped error disappear. I searched a lot but i did not reach to fix the problem.

What should I do ?

Upvotes: 0

Views: 541

Answers (1)

user207421
user207421

Reputation: 310980

You need

%type <value> E T F

to fix the type errors.

As noted by @JeffMercado, the premature EOF is caused by the excess first brace on

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

And

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

This is not right. The commas should be spaces.

Upvotes: 4

Related Questions