See below.
On Feb 17, 2008, at 8:22 AM, Tong King wrote:
Dear All,
I write a .l file and a .y file to parse the struct:
somebody=age, and the
result is to print somebody's age is .... But the parser can work
at the
first time, at the second time it reports wrong although input is
right. I
don't know why, and here is the code:
3.l
%{
#include "3.tab.h"
#include<string.h>
extern void yyerror( char*);
%}
%%
[a-zA-Z]+ {yylval.str=strdup(yytext); return NAME;}
[0-9]+ {yylval.num = atoi(yytext); return AGE;}
[ \t\n] {;}
[=;] {return yytext[0];}
. {ECHO; yyerror ("unexpected character");}
%%
int yywrap (void) {return 1;}
###############################
3.y
%{
#include<stdio.h>
extern int yylex();
void yyerror(const char*);
%}
%union{
char* str;
int num;
}
%token <str> NAME
//%token '='
%token <num> AGE
%%
exps: exp /* You need to include something to parse multiple
statements */
| exps exp
;
exp:
| NAME '=' AGE {printf("the man %s's age is%d\n",$1,$3 );}
;
%%
void yyerror(const char*s)
{
fprintf(stderr, "%s\n", s);
}
int main()
{
return yyparse();
}
Your parser works as written, but you wrote it to parse only *one*
statement. You need to include an additional parser rule to parse
multiple statements see above.
_______________________________________________
help-bison@gnu.org http://lists.gnu.org/mailman/listinfo/help-bison