检查中未完成的弯曲评论 [英] checking unfinished comments in flex

查看:212
本文介绍了检查中未完成的弯曲评论的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一个新的弯曲。我刚写了一个样本code检测多线使用柔性程序的意见。现在我想改善code。我想检测在code未完成和虐待形成意见。例如:注释开头没有一个结局* /是一个未完成的注释并形成不良评论我的意思是注释不正确形成/ *,也就是说,一个EOF出现里面的评论等等。我有我的$添加C $ C到检查这些东西呢?我的示例code是如下:

 %×COMMENT_MULTI_LINE
%{
字符* commentStart;
%}%%[\\ n \\ t \\ r] + {
  / *忽略空白* /}
<初始>/ *{
  commentStart = yytext中;
  BEGIN(COMMENT_MULTI_LINE);
}&所述; COMMENT_MULTI_LINE>中* /{
  字符*注释= strndup(commentStart,yytext中+ 2 - commentStart);
  的printf(%s'的:是一个多行注释\\ n,评论);
  免费(注解);
  BEGIN(INITIAL);
}&所述; COMMENT_MULTI_LINE取代。 {}< COMMENT_MULTI_LINE> \\ N {}
%%INT主(INT ARGC,CHAR *的argv []){
  函数yylex();
}


解决方案

柔性手册章节关于使用<< EOF>> 是相当有帮助的,因为它具有完全相同的情况下,作为一个例子,他们的code也可以复制逐字到Flex程序。

,因为它解释,使用时<< EOF>> 你不能将其放置在一个正常的常规EX pression 的模式。它只能通过的状态的名称来进行。在您的code你正在使用的状态,表明你是一个注释中。这种状态称为 COMMENT_MULTI 。所有你需要做的就是把这些在&LT面前;< EOF>> 标记,并给它一个动作做的:

 < COMMENT_MULTI><<&EOF GT;> {printf的(未终止的点评:%S \\ n,yytext中);
                        yyterminate();}

专项行动功能 yyterminate()告诉弯曲你已经认识到<< EOF>> ,它标志着你的程序结束时的输入。

我已经测试过这一点,它在你的code工作。 (和多线串也)。

I am a new to flex. I have just written a sample code to detect multi line comments using a flex program. Now I want to improve the code. I want to detect unfinished and ill formed comments in the code. for example: a comment beginning with /* without an ending */ is an unfinished comment and by ill formed comment I mean the comment is not properly formed, say, an EOF appears inside the comment etc. What I have to add in my code to check these things? My sample code is as follows:

%x COMMENT_MULTI_LINE
%{
char* commentStart;
%}

%%

[\n\t\r ]+ { 
  /* ignore whitespace */ }


<INITIAL>"/*" { 
  commentStart = yytext; 
  BEGIN(COMMENT_MULTI_LINE); 
}

<COMMENT_MULTI_LINE>"*/" { 
  char* comment = strndup(commentStart, yytext + 2 - commentStart);
  printf("'%s': was a multi-line comment\n", comment);
  free(comment); 
  BEGIN(INITIAL); 
}

<COMMENT_MULTI_LINE>. { 

} 

<COMMENT_MULTI_LINE>\n { 

} 


%%

int main(int argc, char *argv[]){    
  yylex();         
}

解决方案

The flex manual section on using <<EOF>> is quite helpful as it has exactly your case as an example, and their code can also be copied verbatim into your flex program.

As it explains, when using <<EOF>> you cannot place it in a normal regular expression pattern. It can only be proceeded by a the name of a state. In your code you are using a state to indicate you are inside a comment. This state is called COMMENT_MULTI. All you have to do is put that in front of the <<EOF>> marker and give it an action to do:

<COMMENT_MULTI><<EOF>> {printf("Unterminated Comment: %s\n", yytext); 
                        yyterminate();}

The special action function yyterminate() tells flex that you have recognised the <<EOF>> and that it marks the end-of-input for your program.

I have tested this, and it works in your code. (And with multi-line strings also).

这篇关于检查中未完成的弯曲评论的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆