Showing posts with label lex and yacc. Show all posts
Showing posts with label lex and yacc. Show all posts

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

Friday, October 15, 2010

lex and yacc for Ubuntu

Ubuntu by default does not have lex and yacc installed. Here is the simplest way to install lex and yacc in Ubuntu. This method uses the following packages

flex (for lex)
bison (for yacc)

Open a terminal and type the following command

sudo apt-get install flex bison

After the installation type the following two commands on the console.

which lex
which yacc

They should return /usr/bin/lex and /usr/bin/yacc respectively if lex and yacc
installed properly