help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: A problem of a name parser


From: Aaron Jackson
Subject: Re: A problem of a name parser
Date: Sun, 17 Feb 2008 14:48:40 -0500

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.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]