C 即使未执行写入操作,内容也会写入文件
我是lex的新手。下面是一个简单的lex程序,它计算给定输入C文件中printf和scanf语句的数量,并将它们替换为readf和writef,后者写入一个单独的文件C 即使未执行写入操作,内容也会写入文件,c,lex,C,Lex,我是lex的新手。下面是一个简单的lex程序,它计算给定输入C文件中printf和scanf语句的数量,并将它们替换为readf和writef,后者写入一个单独的文件 %{ #include<stdio.h> #include<stdlib.h> int pc=0 ,sc=0; %} %% "printf" {fprintf(yyout,"writef");pc++;}
%{
#include<stdio.h>
#include<stdlib.h>
int pc=0 ,sc=0;
%}
%%
"printf" {fprintf(yyout,"writef");pc++;}
"scanf" {fprintf(yyout,"readf");sc++;}
. {fprintf(yyout,"%c",yytext[0]);}
%%
main()
{
yyin=fopen("file5","r");
yyout=fopen("fileout5","w");
yylex();
printf("no.of printf statements=%d \n no.of scanf statements=%d \n",pc,sc);
fclose(yyin);
fclose(yyout);
}
文件5:
#include<stdio.h>
int main()
{
int a=1,b=1;
printf("%d",a);
scanf("%d",&a);
//this is a comment
fprintf(stdout,"type it to console");
printf("hlh");
return (0);
}
文件输出5:
#include<stdio.h>
int main()
{int a=1,b=1;
writef("%d",a);
readf("%d",&a);
//this is a comment
fwritef(stdout,"type it to console");
writef("hlh");
return (0);
}
我的问题是,当我删除第三条规则时,生成的输出是相同的。我想知道文件的剩余内容来自何处?根据我的说法,当我删除第三条规则时,fileout5中只应存在readf和writef 默认的lex行为是将输入复制到输出,这正是第三条语句所做的。如果你想吃掉输入,你需要匹配。并且有一个无操作规则{} 你能提供我的链接,它是提到,这将是伟大的help@Light