Pages

Monday, November 21

Lex program ~ Read and copy file content


This lex program will read an input file and copy the content with line number to another file.






%{
#include<stdio.h>
#include<string.h>
char line[20];
int count=0,i=0;
FILE *out;
%}
%%
['\n'] { fprintf(out,"%d %s\n",++count,line);}
(.*) {
strcpy(line,yytext);
}
%%
main()
{
yyin=fopen("in.c","r");
out=fopen("out.c","w");
yylex();
getch();
}

int yywrap()
{
return 1;
}








input
main()
{
int i=9, j=900,k=115;
printf("%d,i+j+k);
}


output file


1 main()
2 {
3 int i=9, j=900,k=115;
4 printf("%d,i+j+k);
5 }
6 }

14 comments:

  1. I faced with error which provide me segmentation fault ( core dump ).

    ReplyDelete
  2. program is not running

    ReplyDelete
  3. program is not running

    ReplyDelete
  4. i am not understanding..

    ReplyDelete
  5. i am getting error in (.*) part..
    actually why we are using (.*)..

    ReplyDelete


  6. Very informative article.Thank you author for posting this kind of article .



    http://www.wikitechy.com/view-article/string-copy-program-in-c-example-with-explanation



    Both are really good,
    Cheers,
    Venkat


    ReplyDelete
  7. giving an error on 13th , 17th and 18th line as unrecognized rule

    ReplyDelete
  8. segmentation fault

    ReplyDelete
  9. Respected Sir,
    how to install lex programming software in windows 8
    and how to do programming well.
    please help!!!

    ReplyDelete
  10. lex :can't open this file
    this is the output

    ReplyDelete