yacc - "first use" error when change the code in lex file -
given .l
file this:
%{ #include "y.tab.h" %} %% [ \t\n] "if" return if_token ; "while" return else_token ; . yyerror("invalid character"); %% int yywrap(void){ return 1; }
and .y
file this:
%{ #include <stdio.h> void yyerror(char *); %} %token if_token else_token minus_token digit_token %% program :expr {printf("program accepted!!!");}; expr : if_token | digit_token ; %% void yyerror(char *s){ fprintf(stderr, "%s\n", s); } int main(){ yyparse(); return 0; }
i use these 3 commands compile these 2 files (my lex file named p.l , yacc file named p.y):
flex p.l yacc -d p.y gcc lex.yy.c y.tab.c
it compiled no error. when changed "return else_token" "return while_token", got error , got no output file:
p.l: in function ‘yylex’: p.l:10:8: error: ‘while_token’ undeclared (first use in function) "while" return while_token ; ^ p.l:10:8: note: each undeclared identifier reported once each function appears in
also when change "while" "else" , add new rule like:
"for" return for_token ;
i same error. how can correct code work correctly?
you didn't add:
%token while_token for_token
to grammar, header didn't contain definition while_token
or for_token
, compilation of lexical analyzer failed.
Comments
Post a Comment