我用flex編寫的詞法分析器出現問題。當我嘗試編譯它時,沒有創建exe文件,並且出現很多錯誤。以下是Flex文件:Flex沒有編譯詞法分析器 - 宏的錯誤
%{
#ifdef PRINT
#define TOKEN(t) printf("Token: " #t "\n");
#else
#define TOKEN(t) return(t);
#endif
%}
delim [ \t\n]
ws {delim}+
digit [0-9]
id {character}({character}|{digit})*
number {digit}+
character [A-Za-z]
%%
{ws} ; /* Do Nothing */
":" TOKEN(COLON);
";" TOKEN(SEMICOLON);
"," TOKEN(COMMA);
"(" TOKEN(BRA);
")" TOKEN(CKET);
"." TOKEN(DOT);
"'" TOKEN(APOS);
"=" TOKEN(EQUALS);
"<" TOKEN(LESSTHAN);
">" TOKEN(GREATERTHAN);
"+" TOKEN(PLUS);
"-" TOKEN(SUBTRACT);
"*" TOKEN(MULTIPLY);
"/" TOKEN(DIVIDE);
{id} TOKEN(ID);
{number} TOKEN(NUMBER);
'{character}' TOKEN(CHARACTER_CONSTANT);
%%
這是我收到的錯誤:
spl.l: In function 'yylex':
spl.l:19:7: error: 'COLON' undeclared (first use in this function)
":" TOKEN(COLON);
^
spl.l:5:25: note: in definition of macro 'TOKEN'
#define TOKEN(t) return(t);
^
spl.l:19:7: note: each undeclared identifier is reported only once for each function it appears in
":" TOKEN(COLON);
^
spl.l:5:25: note: in definition of macro 'TOKEN'
#define TOKEN(t) return(t);
^
spl.l:20:7: error: 'SEMICOLON' undeclared (first use in this function)
";" TOKEN(SEMICOLON);
^
spl.l:5:25: note: in definition of macro 'TOKEN'
#define TOKEN(t) return(t);
,我使用編譯命令:
flex a.l
gcc -o newlex.exe lex.yy.c -lfl
任何人都可以看到我的可能會出錯?
順便說一句,你不應該需要一個「;」在你的宏中,你已經調用它了 –