Project Report ON Simple Compiler Design
Project Report ON Simple Compiler Design
ON
SIMPLE COMPILER DESIGN
Submitted in partial fulfilment of the Requirements for the award of THE Degree of Bachelor
of Technology In Computer science and Engineering Under the esteemed guidance of
Mrs C Radhika Rani
By
190031023 M Maneeshwar
190031225 P Swarnalekha
K L EDUCATION FOUNDATION
Green Fields, Vaddeswaram, Guntur District-522 502 2020-2021
K L EDUCATION FOUNDATION DEPARTMENT OF COMPUTER SCIENCE AND
ENGINEERING
CERTIFICATE
This is to certify that this project based lab report entitled “ Simple Compiler Design “ is a
bonafide work done by 190031023 M Maneeshwar 190031225 P Swarnalekha in the course
19CS3214P AUTOMATA THEORY AND COMPILER DESIGN in partial fulfilment of the
requirements for the award of Degree in Bachelor of Technology in COMPUTER SCIENCE
& ENGNEERING during the Odd Semester of Academic year 2020-2021.
DECLARATION
We hereby declare that this project based lab report entitled “ Simple Compiler Design “ has
been prepared by us in the course 19CS3214P AUTOMATA THEORY AND COMPILER
DESIGN in partial fulfilment of the requirement for the award of degree bachelor of
technology in COMPUTER SCIENCE & ENGINEERING during the Odd Semester of the
academic year 2020-2021. We also declare that this project-based lab report is of our own
effort and it has not been submitted to any other university for the award of any degree.
Date :
Place :
ACKNOWDEMENT
We express our gratitude to Mrs C Radhika Rani Course Co-coordinator for 19CS3214P ,
AUTOMATA THEORY AND COMPILER DESIGN course in the Computer Science
and Engineering Department for providing us with adequate planning and support and means
by which we can complete this project
We express our gratitude to Mr. V. HARIKIRAN, Head of the Department for computer
science and Engineering for providing us with adequate facilities, ways and means by which
we can complete this project-based Lab.
We would like to place on record the deep sense of gratitude to the honourable Vice
Chancellor, K L University for providing the necessary facilities to carry the project-based
Lab.
Last but not the least, we thank all Teaching and Non-Teaching Staff of our department and
especially our classmates and our friends for their support in the completion of our project-
based Lab.
CHAPTERS PAGE NO
......................18 CONTENTS
Chapter 1: Introduction...........................................................................................1
Chapter 4:Screenshots...........................................................................................13
Chapter 6: Conclusion...........................................................................................17
Chapter 7: References...........................................................................................18
Chapter 1
Introduction
A compiler translates the code written in one language to some other language
without changing the meaning of the program. It is also expected that a
compiler should make the target code efficient and optimized in terms of time
and space.
The compilation process is a sequence of various phases. Each phase takes input
from its previous stage, has its own representation of source program, and feeds
its output to the next phase of the compiler. The analysis phase of the compiler
reads the source program, divides it into core parts and then checks for lexical,
grammar and syntax errors.
The analysis phase generates an intermediate code which is also referred as
Assembly Language Code
Our project is to take a language and convert it to intermediate code. This part
of compiler design is front end. This Intermediate code generated is independent
of machine. This code is later optimized and converted to machine code.
Chapter 2
Language Description
2.1 Language
Our language will have the following specifications.
Identifier Rules
I. Identifier can be of maximum length 6.
II. Identifiers are not case sensitive.
III. An Indentifier can only have alphanumeric characters( a-z , A-Z , 0-9 ) and
underscore(_).
IV. The first character of an identifier can only contain alphabet( a-z , A-Z ).
V. Keywords are not allowed to be used as Identifiers.
VI. No special characters, such as semicolon, period, whitespaces, slash or
comma are permitted to be used in or as Identifier.
Data Types:
Our language supports only 3 datatypes
1. Integer 2. String 3. Character
Expressions:
a. Arithmetic operators (+, -, *, /, %)
b. Uniray operator
c. Paranthesis
d. Only Integer supported
e. Relational expression to be supported (>, <, >=, <=, ==, !=)
f. Character string and integer constants
a. Input ;
b. Output ;
Program Structure:
Decleration:
Start
End
2.2 Sample Program I:
#mode 10
declaration
int r
int c
int in
int flg
Start
r=0
flg = 1
while( flg == 1 )
if( c == 0) then
flg = 0
endif
c = c-1
endwhile
end
#mode 10
declaration
int a ; b
int i
int k
string mes1
start
k=k*1
if(i<9 )then
i=i+9
k=k*1
endif
i=i-45
repeat
i=i+9*k+b
k=k*1
output "Hello World"
input k
until(i<2 )
while(k>3 )
i=i+9
k=k*1
endwhile
end
2.5 Code Generated
START:
MOV AX, @DATA
MOV DS, AX
MOV AX, k
MUL 1
MOV k, AX
MOV AX, i
CMP AX, 9
JGE LB01
MOV AX, i
ADD AX, 9
MOV i, AX
MOV AX, k
MUL 1
MOV k, AX
LB01:
MOV AX, i
SUB AX, 45
MOV i, AX
LB01:
MOV AX, i
ADD AX, 9
MUL k
ADD AX, b
MOV i, AX
MOV AX, k
MUL 1
MOV k, AX
LEA DX, "Hello World"
CALL MESSAGE
CALL INDEC
MOV k, AX
MOV AX, i
CMP AX, 2
JGE LB01
LB01:
MOV AX,
CMP AX, 3
JLE LB01
MOV AX, i
ADD AX, 9
MOV i, AX
MOV AX, k
MUL 1
MOV k, AX
JMP LB01
LB01:
MOV AX, 4C00H
INT 21H
END START
Chapter 3
Phases of compiler
1.1 Overview
Analysis part of compiler breaks the source program into constituent pieces and imposes a
grammatical structure on them which further uses this structure to create an intermediate
representation of the source program. It is also termed as front end of compiler.
Usually, a lexical error is caused by the appearance of some illegal character, mostly at the
beginning of a token.
Top down parsers Top down parsers construct parse tree from root to leaves.
Bottom up parsers Bottom up parsers construct parse tree from leaves to root.
Role of Parser
• Once a token is generated by the lexical analyzer, it is passed to the parser.
• On receiving a token, the parser verifies the string of token names that can be generated by
the grammar of source language.
• It calls the function getNextToken(), to notify the lexical analyzer to yield another token.
• It scans the token one at a time from left to right to construct the parse tree.
• It also checks the syntactic constructs of the grammar.
1.4 Semantic analyser
• Semantic analysis is the third phase of compiler.
• It checks for the semantic consistency.
• Type information is gathered and stored in symbol table or in syntax tree.
• Performs type checking.
11
2.parser
3.
4.