lex program to prepend line number to each line

 

%{
#include<stdio.h>
#include<string.h>
int ln=0;
%}
%%
“\n” {}
.* {ln++;fprintf(yyout,”\n%d:%s”,ln,yytext);}
%%
main()
{
yyin=fopen(“try1.txt”,”r”);
yyout=fopen(“try2.txt”,”w”);
yylex();
return 0;
}
int yywrap()
{
}

 

</pre>

Sample output

try1.txt

hai
hello world
test file
language processor

try2.txt

1:hai
2:hello world
3:test file
4:language processor

 

Leave a Reply

Your email address will not be published. Required fields are marked *