Thursday, November 18, 2010

Bit further into lex

Consider the lex program given below (line.l) which adds line numbers in front of each line in a given text file and displays the output on standard output.

The use of lex variables and lex functions

yytext : The text of the matched pattern is stored in this variable. yytext is a char pointer.

yyin : Of the type FILE*. This points to the current file being parsed by the lexer.

yylex() : The function that starts the analysis. It is automatically generated by Lex.

What if there is no matching pattern to be found?
Any string matches and default action happens which means all the inputs are going to be printed in a way one at a time.

%{

int lineno=1;

%}

%%

.*\n { printf("%5d %s",lineno++,yytext); }

%%

main(int argc, char*argv[])
{

FILE *yyin;
yyin=fopen(argv[1],"r");
yylex();
return 0;

}



Compilation process

lex line.l
cc lex.yy.c -0 line -lfl

Executing

For this program it requires a a file as a command line argument. So a file should be provided in this stage.

./line filename

If it is needed to print the output in a file, just redirect the output to a text file like following.

./line > filename

No comments :

Post a Comment