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 :

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

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

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

program is not running

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

program is not running

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

i am not understanding..

v!p!n said... Best Blogger Tips [Reply to comment] Best Blogger Templates

@Anonymous

Tell me your doubt!

Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates

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

Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates



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


Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates

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

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

segmentation fault

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

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

Unknown said... Best Blogger Tips [Reply to comment] Best Blogger Templates

lex :can't open this file
this is the output

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

asdfgth

Anonymous said... Best Blogger Tips [Reply to comment] Best Blogger Templates

lol

asdfghjk said... Best Blogger Tips [Reply to comment] Best Blogger Templates

sdfghjkl

Post a Comment