0
我想,當用戶輸入一個不完整的 定義,例如,呈現在屏幕上輸出一個錯誤:在Flex中#define不完整時如何顯示錯誤?
#define A // this is wrong , error message should appear
#define A 5 // this is the correct form , no error message would be presented
,但它不能正常工作,這裏的代碼:
%{
#include <stdio.h>
#include <string.h>
%}
%s FULLCOMMENT
%s LINECOMMENT
%s DEFINE
%s INCLUDE
%s PRINT
%s PSEUDO_C_CODE
STRING [^ \n]*
%%
<FULLCOMMENT>"*/" { BEGIN INITIAL; }
<FULLCOMMENT>. { /* Do Nothing */ }
<INCLUDE>"<stdio.h>"|"<stdlib.h>"|"<string.h>" { BEGIN INITIAL; }
<INCLUDE>. { printf("error\n"); return 0 ; }
<DEFINE>[ \t] { printf("eat a space within define\n"); }
<DEFINE>{STRING} { printf("eat string %s\n" , yytext);}
<DEFINE>\n { printf("eat a break line within define\n"); BEGIN INITIAL; }
"#include" { BEGIN INCLUDE; }
"#define" { printf("you gonna to define\n"); BEGIN DEFINE; }
"#define"+. { printf("error\n"); }
%%
int yywrap(void) { return 1; } // Callback at end of file
int main(void) {
yylex();
return 0 ;
}
我哪裏做錯了 ?
這裏是輸出:
[email protected]:~/Desktop$ ./a.out
#define A 4
error
A 4
#define A
error
A
我重申了這一點;因爲我很確定它沒有任何功能w/Adobe/Apache Flex(UI框架)。 gnu-flex或lex通常用於詞法分析器。 – JeffryHouser