开发者

What's wrong with this lex file?

开发者 https://www.devze.com 2022-12-11 07:04 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号