Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
99 views22 pages

Compiler Design Lab Manual

complete book of compiler design

Uploaded by

Ajay Jha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
99 views22 pages

Compiler Design Lab Manual

complete book of compiler design

Uploaded by

Ajay Jha
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/378696696

Compiler Design Lab manual

Presentation · March 2024


DOI: 10.13140/RG.2.2.13919.02722

CITATIONS READS
0 514

1 author:

Gebreziher Gebrelibanos
gebreziher.gebrelibanos.adu.edu.et
2 PUBLICATIONS 0 CITATIONS

SEE PROFILE

All content following this page was uploaded by Gebreziher Gebrelibanos on 04 March 2024.

The user has requested enhancement of the downloaded file.


Compiler Design Lab manual
Adigrat University
Department of Computer Science

Prepared by Mr. G/her G/libanos

November 23, 2023


Contents

1 Lexical analyzer Phase 2


1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.1.1 Lexical Analysis . . . . . . . . . . . . . . . . . . . . . . . 2
1.1.2 Installation of required software . . . . . . . . . . . . . . . 2
1.1.3 Structure of the Flex Program . . . . . . . . . . . . . . . 6
1.1.4 lab one . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.1.5 lab exercise . . . . . . . . . . . . . . . . . . . . . . . . . . 11

2 Syntax analyzer phase 13


2.1 installation YACC . . . . . . . . . . . . . . . . . . . . . . . . . . 13
2.2 structure of the yacc . . . . . . . . . . . . . . . . . . . . . . . . . 14
2.3 How it execute the yacc . . . . . . . . . . . . . . . . . . . . . . . 14
2.4 sample yacc program . . . . . . . . . . . . . . . . . . . . . . . . . 15
2.5 Practice questions . . . . . . . . . . . . . . . . . . . . . . . . . . 19

1
Chapter 1

Lexical analyzer Phase

1.1 Introduction
This is lab manual for computer science students of Adigrat University taking
the course compiler design.
compiler is a software that converts high-level language into a low-level language.
This process is known as compilation. And compilation takes place in multiple
steps. And the first step in the compilation is the lexical analysis. Lex on
Ubuntu or flex on window is a program that creates a lexical analyzer and helps
you to perform the task of lexical analysis.

1.1.1 Lexical Analysis


Compilation happens in multiple phases, and lexical analysis is the starting
Phases of Compiler. It gathers preprocessed source code, written in high level
language, that comes as the preprocessor’s output. The lexical analyzer gener-
ates a stream of tokens from the preprocessed source code by removing white
space and comments. It generates an error if it gets any invalid token. The
stream of character is read by it, and it seeks the legal tokens, and then the
data is passed to the syntax analyzer when asked for it.

1.1.2 Installation of required software


inorder to preform the compiler design lab the following software are required.
ˆ lex for Ubuntu OS User or Flex for Window user

ˆ Any c compiler that consist gcc

install both software and find the path for flex and gcc in program file as shown
bellow in the image.

2
ˆ flex path on my computer
C:\Program Files (x86)\GnuWin32\bin\

ˆ gcc path on my computer


C:\Program Files (x86)\Embarcadero\Dev-Cpp\TDM-GCC-64\bin

3
after installing both softwares the next step is to change the path of both file
by type the following on start button
edit environment variable
edit path on the user variable.

4
click on edit and new and past the path

5
do the same for gcc too.

1.1.3 Structure of the Flex Program


A lex program consists of three sections:

1. section containing definitions


2. translation rules which write regular expression
3. auxiliary functions
The above list can be shown as follows:

6
1.1.4 lab one
1. write a flex program to check whether a given input is identifier or number?

7
ˆ save the file with .l extension (lab1.l)
ˆ check the file type on your folder as L type
ˆ run the program using flex compiler as follow
ˆ flex file name.l
flex lab1.l
ˆ flex generate lex.yy.c file
ˆ run the c code using gcc compiler as follow
ˆ gcc lex.yy.c -o out put
ˆ execute the out put .exe file
out put.exe
ˆ accepting input stream
The over all execution can shown as follows:

The execution preforms on cmd and looks like as follow

When the above program execute the following file would be add to lab1.l

8
as show in the below image
lex.yy.c
out put1.exe

2. For the question number 1 accept input from file rather than accepting
from user. What to add here the yyin before yylex only.
yyin=fopen(”file name”,”r”);

On the above code the input is save on input.c

9
3. write a flex program for the bellow c++ code and use regular definition
and count total number of tokens and count total number of each cate-
gories.

The possible source code for the above sample input would be design as
follows:

The output looks like as shown below

10
1.1.5 lab exercise
1. Write a flex program to display valid file name that consists at least the
following file extensions .doc, .exe .txt .c, .cpp, .pdf, .ppt and etc.
2. Write a flex program to display valid person full name that consists until
grand father name and at least 3 characters and at most 15 character.

11
3. Write a flex program to display valid Ethiopian office phone number and
valid mobile phone number.

12
Chapter 2

Syntax analyzer phase

2.1 installation YACC


This chapter deals about the second phase of the compiler design.it uses to
generate the parse tree of the lexical analyzer(tokens).
YACC is abbreviation for yet another compiler compiler

ˆ generate parse for the tokens generated from lexical analyzer

ˆ it uses grammar (CFG)

13
2.2 structure of the yacc

2.3 How it execute the yacc


The yacc file must include the following steps to execute.

ˆ Save with file name.y extension (lab1.y)

ˆ Save the token need to parse using file name.l extension (lab1.l)

ˆ execute using the following commend

– yacc -d lab1.y
– The above code generates two file lab1.tab.c which is c code uses to
parse and lab1.tab.h which consists header file to include in flex code
used to specify tokens.
– flex lab1.l
– This also generate lex.yy.c which we cover on chapter one
– gcc lex.yy.c lab1.tab.c -o out put
ˆ The above process can be summarized by the following image.

14
2.4 sample yacc program
1. Write a flex and yacc program to display valid variable declaration.
ˆ yacc source code

15
ˆ flex source code

16
ˆ execution of the program

2. Write flex and yacc program to assign a value for a viable.


ˆ yacc souce code

17
ˆ flex source code

3. Write flex and yacc program for sample c++ code that can be consist
header files, main function, variable declaration, output, input conditional
statements, and etc.
The following code accept for the input shown below

18
ˆ flex representation

ˆ yacc souce code

19
2.5 Practice questions
write a flex and yacc program for the following questions.

1. Write a flex and yacc program to design for loop.


2. Write a flex and yacc program to display cout and cin form of c++ program
3.

20

View publication stats

You might also like