help-bison
[Top][All Lists]
Advanced

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

A problem of a name parser


From: Tong King
Subject: A problem of a name parser
Date: Sun, 17 Feb 2008 21:22:28 +0800

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

%%
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();
}


reply via email to

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