lex程序上没有注释行数 [英] lex program on counting no of comment lines
本文介绍了lex程序上没有注释行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这里程序不计算注释行,单行注释和多行注释的数目,并给出以file.txt作为输入的总注释输出
here the program counts the no of comment lines, single line comments and multi line comments and gives a total comments output with a file.txt as input
file.txt
//hellow world
/*hello world1*/
/*hello world2
*/
/*hello world3
hello world3.1*/
#include<>
count.l
%{
#include<stdio.h>
#include<stdlib.h>
int a=0,b=0,c=0,d;
%}
%%
"//".* {a++;}
"/*" {b++;}
.*"*/" {b--;c++;}
%%
void main(int argc,char *argv[]){
yyin=fopen(argv[1],"r");
yylex();
printf("single line %d \nmultiline %d \n",a,c);
d=a+c;
printf("total %d \n",d);
}
我得到的输出是
./a.out file.txt
./a.out file.txt
hello world2
hello world3
#include<>
single line 1
multiline 3
total 4
我需要获得的输出仅仅是
the output that i need to get is just
#include<>
single line 1
multiline 3
total 4
我也这样尝试过,我在"/*"
的前面添加了.*
,就像这样"/*".*
,然后它将删除该行中的"*/"
,并给我多行注释计数为2.
我尝试了各种方法,但我有点卡住了.
I tried this way too i added .*
infront of "/*"
like this "/*".*
then it would delete the "*/"
in that line and give me the multi line comment count as 2.
i tried various ways but im kinda stuck.
推荐答案
添加了精确的逻辑以使其更好地工作.
added precise logic to make it work better.
%{
#include<stdio.h>
#include<stdlib.h>
int a=0,c=0,d,e=0;
%}
%%
"/*" {if(e==0)e++;}
"*/" {if(e==1)e=0;c++;}
"//".* {if(e==0)a++;}
. {if(e==0)ECHO;}
%%
void main(int argc,char *argv[]){
yyin=fopen(argv[1],"r");
yyout=fopen(argv[2],"w");
yylex();
printf("single line %d \nmultiline %d \n",a,c);
d=a+c;
printf("total %d \n",d);
}
这篇关于lex程序上没有注释行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文