Rui Maciel
2008-08-10 20:15:36 UTC
I've just started looking into flex+bison and unfortunately I've stumbled
on a problem that I'm not able to figure out. I have a grammar definition
which I believe is sane and a lexer which works fine but when I try to
feed the parser a valid entry, the parser complains about a syntax error.
Could anyone please point out to me what is wrong with this code? Thanks
in advance.
The code is as follows:
the flex source code:
%{
#include <stdio.h>
#include <string.h>
#include "definition.h"
extern char* yylval;
%}
char [A-Za-z]
num [0-9]
eq [=]
name {char}+
age {num}+
%%
{name} { yylval = strdup(yytext); return NAME; }
{eq} { return EQ; }
{age} { yylval = strdup(yytext); return AGE; }
[ \t] /* ignore */
. {yyerror("invalid character\n");}
%%
int yywrap()
{
return 1;
}
The bison source code:
%{
#include <stdio.h>
typedef char* string;
#define YYSTYPE string
%}
%token NAME EQ AGE
%start file
%%
file: record file
| record
;
record: '\n'
| NAME EQ AGE { printf("%s is %s years old!!!\n", $1, $3); }
;
%%
int main(void)
{
yyparse();
return 0;
}
int yyerror(char *msg)
{
yydebug=1;
printf("Error encountered: %s \n", msg);
}
on a problem that I'm not able to figure out. I have a grammar definition
which I believe is sane and a lexer which works fine but when I try to
feed the parser a valid entry, the parser complains about a syntax error.
Could anyone please point out to me what is wrong with this code? Thanks
in advance.
The code is as follows:
the flex source code:
%{
#include <stdio.h>
#include <string.h>
#include "definition.h"
extern char* yylval;
%}
char [A-Za-z]
num [0-9]
eq [=]
name {char}+
age {num}+
%%
{name} { yylval = strdup(yytext); return NAME; }
{eq} { return EQ; }
{age} { yylval = strdup(yytext); return AGE; }
[ \t] /* ignore */
. {yyerror("invalid character\n");}
%%
int yywrap()
{
return 1;
}
The bison source code:
%{
#include <stdio.h>
typedef char* string;
#define YYSTYPE string
%}
%token NAME EQ AGE
%start file
%%
file: record file
| record
;
record: '\n'
| NAME EQ AGE { printf("%s is %s years old!!!\n", $1, $3); }
;
%%
int main(void)
{
yyparse();
return 0;
}
int yyerror(char *msg)
{
yydebug=1;
printf("Error encountered: %s \n", msg);
}