Compiler Design Case Study 2
Compiler Design Case Study 2
Lex is a program generator designed for lexical processing of character input streams. It accepts a
high-level, problem oriented specification for character string matching, and produces a program
in a general purpose language which recognizes regular expressions. The regular expressions are
specified by the user in the source specifications given to Lex. The Lex written code recognizes
these expressions in an input stream and partitions the input stream into strings matching the
expressions. At the boundaries between strings program sections provided by the user are
executed. The Lex source file associates the regular expressions and the program fragments. As
each expression appears in the input to the program written by Lex, the corresponding fragment is
executed.
Defination :-
• Lex is a tool in lexical analysis phase to recognize tokens using regular expression.
• Lex tool itself is a lex compiler.
lex Routines :-
One way to ignore all characters between two special characters, such as between a pair of double
quotation marks, is to use input() like this:
\" while (input() != '"');
After the first double quotation mark, the scanner reads all subsequent characters, and does not
look for a match, until it reads the second double quotation mark. (See the further examples
of input() and unput(c) usage in the "User Routines " section.)
For special I/O needs that are not covered by these default macros, such as writing to several files,
use standard I/O routines in C to rewrite the macro functions.
These routines, however, must be modified consistently. In particular, the character set used must
be consistent in all routines, and a value of 0 returned by input() must mean end of file. The
relationship between input() and unput(c) must be maintained or the lex lookahead will not work.
page 11
parag dhing
Acropolis Institute of Technology & Research
INSTALLATION :-
First of all connect your Windows/LinuxMint to the Internet and Open terminal (you can use the
shortcut Ctrl+Alt+t to open the terminal)
Now in terminal type the following command. (You can check the above image for the screenshot)
sudo apt-get update
Now, it will ask for your password – Please type the password for your Linux Account and hit
Enter. (Note : While typing the password nothing will be visible on terminal –it’s normal simply
continue typing your password and hit enter when you are done with it)
It may also ask for your permissions to update simply type “y” and hit enter , it will update its
repositories through the internet, wait till the complete index update gets completed and it presents
you with a terminal screen to enter command .
Now , first we will install the Lex package which is called as flex by typing the following
command in terminal –
sudo apt-get install flex
Again it will , ask for confirmation , simply respond with “y” and hit enter. Lex will be
successfully installed , and you will be presented with the terminal screen .
Now, Let’s move on to the installation of Yacc for which we will be using the bison package .
Type the following command in the terminal to install yacc –
sudo apt-get install bison
That’s it the Yacc file too will be downloaded and installed , sometimes the Repository servers of
bison are down and give errors in such cases you can use any of the following alternatives to
install yacc.-
1. Open Command prompt and switch to your working directory where you have
stored your lex file (".l") and yacc file (".y")
2. Let your lex and yacc files be "hello.l" and "hello.y". Now, follow the preceding
steps to compile and run your program.
1. For Compiling Lex file only:
1. flex hello.l
2. gcc lex.yy.c
2. For Compiling Lex & Yacc file both:
1. flex hello.l
2. bison -dy hello.y
3. gcc lex.yy.c y.tab.c
3. For Executing the Program
1. a.exe
B. linax :-
Flex is the Fast Lexical Analyser Generator. It's the same as lex, so don't worry about it.
Simply put, it helps a computer look for something. So, if you want to find all instances of
"the" in a paragraph, you'll use lex.
lex example.l
gcc lex.yy.c -ll -o example
./example
This compiles the program, links the lex libraries with it, and renames the output file.
Notice that we're using "gcc" - lex is basically a C program!
7. To compile the lex and the yacc programs together (assuming that lex tokenizes the input
and passes the tokens to yacc)
lex example.l
yacc -d example.y
gcc lex.yy.c y.tab.c -ll -ly -o example
./example
And you're done!
Page |1 4
parag dhing
Acropolis Institute of Technology & Research
Examples :-
/*lex program to count number of words*/
%{
#include<stdio.h>
#include<string.h>
int i = 0;
%}
/* Rules Section*/
%%
([a-zA-Z0-9])* {i++;} /* Rule for counting
number of words*/
int yywrap(void){}
int main()
{
// The function that starts the analysis
yylex();
return 0;
}
Page |1 5
parag dhing
Acropolis Institute of Technology & Research
Page |1 6
parag dhing