【发布时间】:2020-10-17 15:24:39
【问题描述】:
我有以下代码:
%{
#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:警告,无法匹配规则
有人可以帮助我吗?我哪里错了?我该怎么办?
【问题讨论】:
-
除非识别字符串文字,否则无法准确删除 cmets。
标签: parsing haskell comments flex-lexer