Flex:词法分析器,用于删除Haskell中的多注释行

问题描述

我有以下代码:

%{
    #include<stdio.h>
%}

%x multicomment

%option noyywrap
%% 

--(.*) ; 
  
"{-"      BEGIN(multicomment);
<multicomment>[^*\n]+    
<multicomment>"*"        
<multicomment>\n         
<multicomment>"-}"    BEGIN(INITIAL);
%% 
  
int main(int argc,char **argv) 
{ 
    yyin=fopen("Code.txt","r"); 
    yyout=fopen("out.c","w");

    yylex(); 
    return 0; 
} 

完成任务非常简单...从haskell代码中删除单行/多行注释。

-用于单行; {--}多行;

如果我使用“ / *” “ * /” (用于C注释)而不是“ {-” “-}” 。当我使用后两个字符时,我不知道为什么flex删除{-之后的所有其他字符。

示例,假设要清除以下输入文本:

some text

{- some other text
    in multiline
    with haskel comment
-}

/* another text
    always in multiline
    but with C comment
*/

some text without comment

如果上述代码设置如下:

    "/*"      BEGIN(multicomment);
    <multicomment>[^*\n]+    
    <multicomment>"*"        
    <multicomment>\n         
    <multicomment>"*/"    BEGIN(INITIAL);
具有 / *“ ” * /“ 输出的

正确:

some text

{- some other text
    in multiline
    with haskel comment initiator
-}

some text without comment

相反,如果我使用原始代码

    "{-"      BEGIN(multicomment);
    <multicomment>[^*\n]+    
    <multicomment>"*"        
    <multicomment>\n         
    <multicomment>"-}"    BEGIN(INITIAL);

带有“ {-” “-}” ,它不起作用,输出为:

一些文字

它会删除“ {-” 中的所有字符,直到文件结尾,我还尝试了其他论坛推荐的其他设置,例如:

<multicomment>"-\}"    BEGIN(INITIAL);
<multicomment>"-"+"}"    BEGIN(INITIAL);
<multicomment>"-" + "}"    BEGIN(INITIAL);
<multicomment>[-}]    BEGIN(INITIAL);

但是在这些情况下,当我尝试使用 flex CommentClean.l 进行编译时,结果如下:

CommentClean.l:16:警告,规则无法匹配

有人可以帮助我吗?我哪里错了?我该怎么办?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)