Hi,
I'm not sure if this is the group to post a question about
flex/bison(lex/yacc) problems. I'm having trouble parsing multiple
line in a file with a grammar I wrote.
Note: flex is 2.5.4 and bison 1.25 this has been tried on linux and
solaris 2.5.2 and also using lex/yacc.
The lex/grammar must parse multiple lines of the sort that follows:
bx*123*ab*a 1b*;
bx**3*cd2;
bx*;
Now the ";" isn't important and will eventually be removed but the
lex file is:
%{
#include "y.tab.h" /* or the appropriate file bison generates */
#define token(x) x
%}
%%
^bx { return token(ID); }
\* { return token(SEPARATOR); }
[0-9]+ { return token(NUMBER); }
[A-Z ]+ { return token(ALPHA); }
[A-Za-z0-9 ]+ { return token{ALPHNUM); }
; { return token(TERMINATOR); }
\n /* ignore */
. { return token(ERROR); }
The bison/yacc file is:
%token ID
%token SEPARATOR
%token NUMBER
%token ALPHA
%token ALPHNUM
%token TERMINATOR
%token ERROR
%%
strt
: line TERMINAL
;
line
: ID SEPARATOR fieldexpression
;
fieldexpression
: fields
| fieldexpression SEPARATOR fields
;
fields
: /* null */
| NUMERIC
| ALPHA
| ALPHNUM
;
Of course the test.c containing the main() just defines a few
externs such as yyparse=1, yylen, yytext[], and yylineno and call yyparse
with a function for errors defined as yyerror().
main()
{
yyparse();
yyerror(s)Quote:}
char *s;
{
fprintf(stdout, " parse error at lineno=%d for token=%s\n",
yylineno, yytext);
Also not the lex code has been tested and will return or not return theQuote:}
proper tokens. It is only when the lex and parser are trying to parse
more than two lines.
--
Mike,