1.Introducion to lex/flex

lex (lexical analyzer) flex(fast lexical analyzer) is a tool for generating scanners: programs which recognized lexical patterns in text. Lex can generate analyzers in either C or Ratfor, a language which can be translated automatically to portable Fortran. It is available on the PDP-11 UNIX, Honeywell GCOS, and IBM OS systems. However, will only discuss generating analyzers in C on the UNIX based system. 
flex reads the given input files(.lex), or its standard input if no file names are given, for a description of a scanner to generate. The description is in the form of pairs of regular expressions and C code, called rules. flex generates as output a C source file, lex.yy.c, which defines a routine yylex(). This file is compiled and linked with the -lfl library to produce an executable. When the executable is run, it analyzes its input for occurrences of the regular expressions. Whenever it finds one, it executes the corresponding C code. Lets create a sample lex file log.lex
%%
user printf( "%s", getlogin() );

This scanner program replace the string user with the login name. By default, any text not matched by a flex scanner is copied to the output, so the net effect of this scanner is to copy its input file to its output with each occurrence of "user" expanded with login name. 
In this input, there is just one rule. "user" is the pattern and the "printf" is the action. The "%%" marks the beginning of the rules. 
Now we have to give this file to flex scanner generator which will generate the corresponding C program lex.yy.c.You can open lex.yy.c and study the structure of the scanner program. 
$flex log.lex

The next step is to compile the C program lex.yy.c using the C compiler ( cc or gcc) by linking the flex libraries(-lfl).This will produce an executable program a.out by default. You can also specify a different output file name with -o option. 
$cc lex.yy.c -lfl 

Run the file a.out.Type different words.If the word 'user' is given as input, it will display the login name(cek).All other words are simply echoed to the output because there is no matching pattern. 
$./a.out 
hi 
hi 
hello 
hello 
user 
cek 

Note:
Programming in Lex can be divided into three steps:
1. Specify the pattern-associated actions in a form that Lex can understand.
2. Run Lex over this file to generate C code for the scanner.
3. Compile and link the C code to produce the executable scanner.

Comments

Popular posts from this blog

KTU Compiler Lab CSL411

13.Precedence and conflict resolution in yacc