2012-11-02 20 views
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 
+0

我重申了這一點;因爲我很確定它沒有任何功能w/Adob​​e/Apache Flex(UI框架)。 gnu-flex或lex通常用於詞法分析器。 – JeffryHouser

回答

1

規則 「的#define」 +。比較早的#define優先於正確的輸入。你可以這樣說: -

「#define」[:space:] * $ {printf(「error \ n」); }

考慮在flex中使用-dvT選項以獲取詳細的調試輸出。此外,我不確定您是否需要對狀態進行如此廣泛的使用,除了可能的評論。但你會更清楚。

相關問題