FLEX (Fast Lexical Analyzer Generator)
This post will show you how to run Lex and Yacc programs easily on Ubuntu 12.04 18.04 and 21.04
Lex
is a computer program that generates lexical analyzers and was written
by Mike Lesk and Eric Schmidt. Lex reads an input stream specifying the
lexical analyzer and outputs source code implementing the lex in the C
programming language. l, or if the file is named -, lex reads the description from standard input (standard input).
It produces a set of tables that, together with additional prototype
code from /etc/yylex. c, constitute a lexical analyzer to scan those
expressions.
I have assumed that you have a working Ubuntu 12.04 LTS version installed on your system and the computer is connected to the network i.e, internet.
Ubuntu does not come installed with a lex and yacc compiler to do so install it first by
1. Opening the terminal- Short cut Ctrl + Alt + T here T is Terminal
2. sudo apt-get install flex
3. enter your ubuntu password
After installation of flex is completed then
4. sudo apt-get install bison
5. Enter your password.
Successfully Lex and Yacc have been installed on your system.
Running a Lex and Yacc program
1. Write the lex program in a file and save it as file.l (where file is the name of the file).
2. Open the terminal and navigate to the directory where you have saved the file.l (e.x cd Desktop)
3. Type lex filename.l
4. Then type cc lex.yy.c -lfl
5. Finally type ./a.out
Your lex progam will be running now (comment should be correct).
or compiling lex and yacc together
1. write lex program in a file file.l and yacc in a file file.y
2. open the terminal and navigate to the directory where you have saved the files.
3. Type lex filename.l
4. Type yacc filename.y
5. Type cc lex.yy.c y.tab.h -lfl
6. Finally Type ./a.out
Simple Program
%%
"+" {printf("PLUS ");}
%%
int main(int argc, int argv)
{
yylex();
}
Step 1: Open Terminal Type cd Desktop
Start Program by typing gedit filename.l here it's gedit xyz.l
Write the program
Step 2: For compiling use flex filename.l here l is lex code its is understood by the machine-->compiler
Here it's $flex xyz.l
Step 3: After compiling use this $cc lex.yy.c -lfl
Here cc is C Compiler, lex.yy is The lex command stores the yylex function
in a file named lex. yy. c. You can use the yylex function alone to
recognize simple one-word input, or you can use it with other C language
programs to perform more difficult input analysis functions.
- Use the lex program to change the specification file into a C language program. The resulting program is in the lex.yy.c file.
- Use the cc command with the -ll flag to compile and link the program with a library of lex subroutines. The resulting executable program is in the a.out file.
- lfl: First, FLEX reads a specification of a scanner either from an input file *.lex, or from standard input, and it generates as output a C source file lex.yy.c. Then, lex.yy.c is compiled and linked with the "-lfl" library to produce an executable a.out. Finally, a.out analyzes its input stream and transforms it into a sequence of tokens.
- Background Process:
- After it is compiled through flex filename.l internally it generates a file called lex.yy.x the after we type $ cc lex.yy.c -lfl it generates a.out.