[Lex/Yacc] Example5.l:8: error: 'yylval' undeclared (first use in this function)
Bonjour,
je suis en train de lire ce tutoriel sur Lex/Yacc
http://ds9a.nl/lex-yacc/cvs/lex-yacc-howto.html
J'arrive jusqu'à l'exemple 4 à comprendre et compiler...
Par contre il y a un soucis avec l'exemple 5.
Voici le fichier example5.l
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| %{
#include <stdio.h>
#include "y.tab.h"
%}
%%
[0-9]+ yylval=atoi(yytext); return NUMBER;
heat return TOKHEAT;
on|off yylval=!strcmp(yytext,"on"); return STATE;
target return TOKTARGET;
temperature return TOKTEMPERATURE;
\n /* ignore end of line */;
[ \t]+ /* ignore whitespace */;
%% |
Le fichier example5.y
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
| %{
#include <stdio.h>
#include <string.h>
void yyerror(const char *str)
{
fprintf(stderr,"error: %s\n",str);
}
int yywrap()
{
return 1;
}
main()
{
yyparse();
}
%}
%token NUMBER TOKHEAT STATE TOKTARGET TOKTEMPERATURE
%%
commands:
| commands command
;
command:
heat_switch
|
target_set
;
heat_switch:
TOKHEAT STATE
{
if($2)
printf("\tHeat turned on\n");
else
printf("\tHeat turned off\n");
}
;
target_set:
TOKTARGET TOKTEMPERATURE NUMBER
{
printf("\tTemperature set to %d\n",$3);
}
; |
et pour compiler
Code:
1 2 3
| lex example5.l
yacc -d example5.y
cc lex.yy.c y.tab.c -o example5 |
J'obtiens le message d'erreur suivant
Code:
1 2 3 4 5
| $ sh example5.compile
example5.l: In function 'yylex':
example5.l:8: error: 'yylval' undeclared (first use in this function)
example5.l:8: error: (Each undeclared identifier is reported only once
example5.l:8: error: for each function it appears in.) |
Avez-vous une idée de la manière de résoudre le problème.
Merci d'avance
PS: les exemples sont dispos dans un .tar.gz sur
http://ds9a.nl/lex-yacc/
http://ds9a.nl/lex-yacc/cvs/lex-yacc-examples.tar.gz
PS2: j'ai googler un peu sur le sujet il semble qu'il faille mettre un
Code:
extern YYSTYPE yylval;
mais je n'ai pas compris où
je dois le mettre... et pourquoi !