使用Flex从pascal文本文件中删除注释

时间:2016-04-08 13:19:21

标签: comments pascal flex-lexer lex

我试过这个,但它没有用。我认为只需要稍微纠正一下。我想我没有在一行中正确使用符号的优先级。救命?

%{
   #include<stdio.h>
   #include<stdlib.h>
   int nest = 0;
%}
%x COMMENT
%%
"/*"            {BEGIN(COMMENT); ++nest;}
"//".*          

<COMMENT>[^*)]* {}
<COMMENT>"(*"   {++nest;}
<COMMENT>"*)"   {if (--nest== 0) BEGIN(INITIAL);}
<COMMENT>[*)]   {}


([^/]*([/][^(*])*)* {fprintf(yyout, yytext);}  
%%
int main(int argc, char* argv[])
{
    yyin = fopen("input.txt","r");
    yyout = fopen("output.txt", "w");
    yylex();
    fclose(yyin);
    fclose(yyout);
    return 0;
}

我还需要消除{...}类型的评论,如果有人可以提供帮助,请多多欣赏!

 For example:
 1.a.input:   show (* commenting  *)show
     output:  show show
   b.input:   show { commenting  }show
     output:  show show

 2.input:   show \\  afqqe
   output:  show 

 3.a.input:   show (* commenting .. comment..
              comment....*) show
     output:  show show
   b.input:   show { commenting .. comment..
              comment....} show
     output:  show show

 4.a.input:   show (* commenting {.. }comment..
              comment....*) comment show
     output:  show comment show
   b.input:   show { commenting (* .. comment..
              comment..*)..} show
     output:  show show

0 个答案:

没有答案