Showing posts with label lex. Show all posts
Showing posts with label lex. 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

Sunday, October 17, 2010

Simple lex program

Lex acts as a lexical analyzer which reads the input and converts strings in the source to tokens according to the regular expressions defined in the source.

Sample lex code - lexcode.l

%{
//definition section
#include <stdio.h>
%}

%%
//rules section
[0123456789]+ printf("NUMBER\n");
[a-zA-Z][a-zA-Z0-9]* printf("WORD\n");
%%

//user subroutings


Compilation

lex lexcode1.l
Creates lex.yy.c

cc lex.yy.c -o lexcode1 -lfl

Execution

./lexcode1

Sample inputs and their outputs

foo
WORD

bar
WORD

123
NUMBER

bar123
WORD

123bar
NUMBER
WORD

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