Parsing 当Flex中的#define不完整时,如何显示错误?

Parsing 当Flex中的#define不完整时,如何显示错误?,parsing,lex,flex-lexer,lexical-analysis,Parsing,Lex,Flex Lexer,Lexical Analysis,当用户输入不完整的错误时,我试图向屏幕显示错误输出 定义,例如: #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 FUL

当用户输入不完整的错误时,我试图向屏幕显示错误输出 定义,例如:

#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 ; 
} 
规则“#define”+。更长,并且优先于较早的#define,即使输入正确。你可以这样说:-

“#define”[:space:*${printf(“error\n”);}


考虑将-dvT选项与flex一起使用,以获得详细的调试输出。此外,我不确定你是否需要如此广泛地使用状态,除了可能的评论。但是你会更清楚的。

我重新标记了这个;我很确定它与w/Adobe/apacheflex(UI框架)无关。GNUflex或lex通常用于词法分析器。
a@ubuntu:~/Desktop$ ./a.out
#define A 4
error
A 4
#define A
error
A