Default lex behavior is to copy the input to the output, which is exactly what your third statement does. If you want to eat the input, you match "." and have a no op rule {}.
Even though write operation not performed content is written to file
Pergunta
I am a newbie to lex.Below is a simple lex program which counts the number of printf and scanf statements in a give input C file and replaces them with readf and writef which is written to a separate file.
%{
#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);
}
file5 :
#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);
}
fileout5:
#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);
}
My question is when I remove the third rule the output produced is same.I want to know from where does the remaining content of the file come from?According to me when I remove the third rule only readf's and writef's should be present in fileout5.
Solução
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow