Reputation: 173
Hi I m trying to make a very simple grammar and failing miserably when I try to combine 2 rules:
The flex file:
%option never-interactive
%option yylineno
D [0-9]
L [a-zA-Z_]
A [a-zA-Z_0-9]
WS [ \t\v\n\f]
%%
"keyword" { printf("KEYWORD: %s\n", yytext); return KEYWORD; }
[+-]?{D}+ { yylval = atoi(yytext); return L_SINT32; }
{L}{A}* { printf("ID: %s\n", yytext); return IDENTIFIER; }
";" { return EOS; }
{WS} { }
. { printf("OTHER\n"); return 0; }
%%
and the corresponding bison file:
%token L_SINT32
%token EOS
%token IDENTIFIER KEYWORD
%start stmt_list
%%
idlist : IDENTIFIER { printf("IDENT\n"); }
| idlist IDENTIFIER { printf("IDENT, IDENT\n"); }
;
stmt : KEYWORD IDENTIFIER ';'
| idlist ';' { printf("EXP\n"); }
;
stmt_list
: stmt
| stmt_list stmt
;
%%
When I try to use that on the input
id0 id1 id2;
keyword id1;
I get:
ID: id0
IDENT
ID: id1
IDENT, IDENT
ID: id2
IDENT, IDENT
error -> syntax error
Can someone points me out why is that ?
Upvotes: 0
Views: 42
Reputation: 241691
When your lexer sees a semicolon:
";" { return EOS; }
What your parser is looking for:
| idlist ';' { printf("EXP\n"); }
EOS
will have some value created by bison and put in the generated header file; it might be 258. ';'
, on the other hand, has the value ';'
(i.e. 0x3B
or 59
).
I'd get rid of EOS and just return ';'
from the lexer.
Upvotes: 1