检测 flex 中的评论
detecting comments in flex
我正在学习 flex.I 已经编写了一个简单的程序来检查给定文本文件中的单词是否为动词并打印它们。我想检测输入文件中是否有任何单行或多行注释(如 c 和 c++ 样式注释)并打印整个注释以输出。有没有办法做到这一点?我的示例代码如下:
%%
[\t]+
is |
am |
are |
was |
were {printf("%s: is a verb",yytext);}
[a-zA-Z]+ {printf("%s: is a verb",yytext);}
. |\n
%%
int main(int argc, char *argv[]){
yyin = fopen(argv[1], "r");
yylex();
fclose(yyin);
}
有点复杂。我建议使用 start conditions 来处理评论。这是我为此快速组合的词法分析器:
%option noyywrap
%x COMMENT_SINGLE
%x COMMENT_MULTI
%top{
/* for strndup */
#include <string.h>
}
%{
char* commentStart;
%}
%%
[\n\t\r ]+ {
/* ignore whitespace */ }
<INITIAL>"//" {
/* begin of single-line comment */
commentStart = yytext;
BEGIN(COMMENT_SINGLE);
}
<COMMENT_SINGLE>\n {
/* end of single-line comment */
char* comment = strndup(commentStart, yytext - commentStart);
printf("'%s': was a single-line comment\n", comment);
free(comment);
BEGIN(INITIAL);
}
<COMMENT_SINGLE>[^\n]+ {
/* suppress whatever is in the comment */
}
<INITIAL>"/*" {
/* begin of multi-line comment */
commentStart = yytext;
BEGIN(COMMENT_MULTI);
}
<COMMENT_MULTI>"*/" {
/* end of multi-line comment */
char* comment = strndup(commentStart, yytext + 2 - commentStart);
printf("'%s': was a multi-line comment\n", comment);
free(comment);
BEGIN(INITIAL);
}
<COMMENT_MULTI>. {
/* suppress whatever is in the comment */
}
<COMMENT_MULTI>\n {
/* don't print newlines */
}
is |
am |
are |
was |
were {
printf("'%s': is a verb\n", yytext);
}
[a-zA-Z]+ {
printf("'%s': is not a verb\n", yytext);
}
. {
/* don't print everything else */
}
%%
int main(int argc, char *argv[]){
yyin = fopen(argv[1], "r");
yylex();
fclose(yyin);
}
注意:词法分析器代码已经足够长,所以我省略了任何错误检查。
单行注释
\/\/.*\n {}
我正在学习 flex.I 已经编写了一个简单的程序来检查给定文本文件中的单词是否为动词并打印它们。我想检测输入文件中是否有任何单行或多行注释(如 c 和 c++ 样式注释)并打印整个注释以输出。有没有办法做到这一点?我的示例代码如下:
%%
[\t]+
is |
am |
are |
was |
were {printf("%s: is a verb",yytext);}
[a-zA-Z]+ {printf("%s: is a verb",yytext);}
. |\n
%%
int main(int argc, char *argv[]){
yyin = fopen(argv[1], "r");
yylex();
fclose(yyin);
}
有点复杂。我建议使用 start conditions 来处理评论。这是我为此快速组合的词法分析器:
%option noyywrap
%x COMMENT_SINGLE
%x COMMENT_MULTI
%top{
/* for strndup */
#include <string.h>
}
%{
char* commentStart;
%}
%%
[\n\t\r ]+ {
/* ignore whitespace */ }
<INITIAL>"//" {
/* begin of single-line comment */
commentStart = yytext;
BEGIN(COMMENT_SINGLE);
}
<COMMENT_SINGLE>\n {
/* end of single-line comment */
char* comment = strndup(commentStart, yytext - commentStart);
printf("'%s': was a single-line comment\n", comment);
free(comment);
BEGIN(INITIAL);
}
<COMMENT_SINGLE>[^\n]+ {
/* suppress whatever is in the comment */
}
<INITIAL>"/*" {
/* begin of multi-line comment */
commentStart = yytext;
BEGIN(COMMENT_MULTI);
}
<COMMENT_MULTI>"*/" {
/* end of multi-line comment */
char* comment = strndup(commentStart, yytext + 2 - commentStart);
printf("'%s': was a multi-line comment\n", comment);
free(comment);
BEGIN(INITIAL);
}
<COMMENT_MULTI>. {
/* suppress whatever is in the comment */
}
<COMMENT_MULTI>\n {
/* don't print newlines */
}
is |
am |
are |
was |
were {
printf("'%s': is a verb\n", yytext);
}
[a-zA-Z]+ {
printf("'%s': is not a verb\n", yytext);
}
. {
/* don't print everything else */
}
%%
int main(int argc, char *argv[]){
yyin = fopen(argv[1], "r");
yylex();
fclose(yyin);
}
注意:词法分析器代码已经足够长,所以我省略了任何错误检查。
单行注释
\/\/.*\n {}