【问题标题】:How to present error when #define is incomplete in Flex?Flex中#define不完整时如何显示错误?
【发布时间】:2012-11-02 01:36:20
【问题描述】:

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

#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 ; 
} 

我哪里做错了?

这是输出:

a@ubuntu:~/Desktop$ ./a.out
#define A 4
error
A 4
#define A
error
A

【问题讨论】:

  • 我重新标记了这个;因为我很确定它与 Adob​​e/Apache Flex(UI 框架)无关。 gnu-flex 或 lex 通常用于词法解析器。

标签: parsing lex flex-lexer lexical-analysis


【解决方案1】:

规则“#define”+。即使输入正确,它也更长并且优先于早期的#define。你可以这么说:-

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

考虑使用带有 flex 的 -dvT 选项来获得详细的调试输出。此外,我不确定您是否需要对除了 cmets 之外的任何东西如此广泛地使用状态。但你会知道的更好。

【讨论】:

    猜你喜欢
    • 2021-07-04
    • 2013-02-05
    • 2022-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    相关资源
    最近更新 更多