0% found this document useful (0 votes)
281 views

Compiler Design Case Study 2

Lex is a tool used in the lexical analysis phase of compilation to recognize tokens using regular expressions. It accepts a specification of rules written in a lex source file that associates regular expressions with code fragments. When it encounters an expression in the input, it executes the corresponding code. Lex generates a C program that implements the lexical analysis. The lex source file is compiled to create the scanner which is then linked with the rest of the compiler. Examples demonstrate using lex to count words in input and print the number of words.

Uploaded by

UTKARSH ARYA
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
281 views

Compiler Design Case Study 2

Lex is a tool used in the lexical analysis phase of compilation to recognize tokens using regular expressions. It accepts a specification of rules written in a lex source file that associates regular expressions with code fragments. When it encounters an expression in the input, it executes the corresponding code. Lex generates a C program that implements the lexical analysis. The lex source file is compiled to create the scanner which is then linked with the rest of the compiler. Examples demonstrate using lex to count words in input and print the number of words.

Uploaded by

UTKARSH ARYA
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Acropolis Institute of Technology & Research

STUDY OF LEX TOOLS :-


Introduction of lex tools :-

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.

Structure of lex program :-

Defination :-

• Lex is a tool in lexical analysis phase to recognize tokens using regular expression.
• Lex tool itself is a lex compiler.

lex Routines :-

The following macros enable you to perform special actions.

 input() reads another character

 unput() puts a character back to be read again a moment later

 output() writes a character on an output device

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

Lex predefine variables :-

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.-

Procedure to run lex :-


Page |1 2
parag dhing
Acropolis Institute of Technology & Research

Compilation & Execution of your Program:


A. Windows :-

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 :-

 1. Open the terminal.


Either Ctrl+Alt+T or find "Terminal" in the Dashboard (Spotlight on Mac: ⌘+Spacebar)
1a. Install homebrew by running this:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

2. Type: which lex


If /usr/bin/lex (or something to that effect) shows up, lex is installed on your system. If the
prompt returns with no output, it isn't.
Install lex by

Linux: sudo apt-get install flex


Mac: brew install flex

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.

3. Type: which yacc


Follow the same procedure as above, except you have to install
Page |1 3
parag dhing
Acropolis Institute of Technology & Research

Linux: sudo apt-get install bison


Mac: brew install bison

4. To start writing the program, use: vi example.l


This opens the vi editor. Here are some basic vi commands: Rookie's guide!
You can replace "example.l" with your own file name, of course. Don't forget the ".l"
extension though!

5. Compile your lex program as:

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!

6. You can start your yacc program similarly


vi example.y

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*/

"\n" {printf("%d\n", i); i = 0;}


%%

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

You might also like