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: Claudio Saavedra
Subject: Re: A problem of a name parser
Date: Sun, 17 Feb 2008 16:32:28 -0300

El dom, 17-02-2008 a las 21:22 +0800, Tong King escribió:
> 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:

If I understood right, your grammar accepts only one instance of the
struct you mention. You need to tweak it a little, so that accepts one
or more occurrences. Adding a new rule

exp_list:
   exp | exp exp_list
;

could help. Note that you may need to specify a separator token as well.

Claudio

> 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
> 
> %%
> 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();
> }
> _______________________________________________
> address@hidden http://lists.gnu.org/mailman/listinfo/help-bison
> 
-- 
Claudio Saavedra <address@hidden>





reply via email to

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