Wednesday, September 9, 2020

Write a Lex program which adds line numbers to the given file and display the same onto the standard output.

LEX program to add line numbers to a given file


EXAMPLE :-

Create first file line.l or line.lex , and Create Second file line.java

line.l

%{ int line_number = 1; %} line .*\n %% {line} { printf("%10d %s", line_number++, yytext); } %% int yywrap(){} int main(int argc, char*argv[]) { extern FILE *yyin; yyin = fopen("line.java","r"); yylex(); return 0; }


INPUT :-

line.java


class hello{ public static void main(String args[]){ System.out.println("Hello World !"); } }


OUTPUT


sunny@sunny-PC:~/Desktop$ flex line.l sunny@sunny-PC:~/Desktop$ gcc lex.yy.c sunny@sunny-PC:~/Desktop$ ./a.out 1 class hello 2 { 3 public static void main(String args[]) 4 { 5 System.out.println("Hello World !"); 6 }          7 }

No comments:

Post a Comment

Please do not any spam link in Comment Box