I have a Makefile so that when I type make the following commands run:
yacc -d parser.y
gcc -c y.tab.c
flex calclexer.l
gcc -c lex.yy.c
But then after this I get the following error messages:
calclexer.l:10: error: parse error before '[' token
calclexer.l:10: error: stray '\' in program
calclexer.l:15: error: stray '\' in program
calclexer.l:24: error: stray '\' in program
make: *** [lex.yy.o] Error 1
This is what is inside calclexer. How can it be fixed?
%{
#include "y.tab.h"
#include "parser.h"
#include <math.h>
%}
%%
%%
([0-9]+|([0-9]*\.[0-9]+)([eE][-+]?[0-9]+)?) {
yylval.dval = atof(yytext);
return NUMBER;
}
[ \t] ; /* ignore white spac开发者_如何学Ce */
[A-Za-z][A-Za-z0-9]* { /* return symbol pointer */
yylval.symp = symlook(yytext);
return NAME;
}
"$" { return 0; /* end of input */ }
\n |. return yytext[0];
%%
You look to have an extra "%%" in "calclexer.l", where you have:
%%
%%
Remove one of those (and the blank line).
The format of a lexer file is (taken from the flex manpage):
definitions
%%
rules
%%
user code
The user code gets copied verbatim to the output file. With the extra "%%", your rules are being interpreted as user code.
精彩评论