0% found this document useful (0 votes)
108 views20 pages

Project Report ON Simple Compiler Design

The document is a project report on simple compiler design submitted by two students, M Maneeshwar and P Swarnalekha, to fulfill the requirements of their Bachelor of Technology degree. It includes a certificate signed by their faculty supervisor and declarations signed by the students. It also acknowledges the support provided by the institution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views20 pages

Project Report ON Simple Compiler Design

The document is a project report on simple compiler design submitted by two students, M Maneeshwar and P Swarnalekha, to fulfill the requirements of their Bachelor of Technology degree. It includes a certificate signed by their faculty supervisor and declarations signed by the students. It also acknowledges the support provided by the institution.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 20

PROJECT REPORT

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.

Faculty in Charge Head of the Department


Mrs.CH.RADHIKA RANI Mr.HARI KIRAN VEGE
K L EDUCATION FOUNDATION DEPARTMENT OF COMPUTER SCIENCE AND
ENGINEERING

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.

Name of the student


190031023 M Maneeshwar
190031225 P Swarnalekha
TABLE OF CONTENTS

CHAPTERS PAGE NO

......................18 CONTENTS

Chapter 1: Introduction...........................................................................................1

Chapter 2: Language description

2.1 Language ...........................................................................................2

2.2 Sample Program I...............................................................................4


2.3 Codegenerated....................................................................................5

2.4 Sample Program II..............................................................................7


2.5 CodeGenerated....................................................................................8

Chapter 3: Phases of compiler


3.1 Overview............................................................................................10
3.2 Lexicalanalyser.................................................................................10
3.3 Syntax analyser..................................................................................11
3.4 Semanticanalyser...............................................................................11
3.5 Intermediate code generator...............................................................12

Chapter 4:Screenshots...........................................................................................13

Chapter 5: Feasibility and future scope.................................................................16

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

e.g. int const 4


char const ‘4’
string const “4”
Statements:
a. Declaration statement : int a
b. Declaration and Initialisation : int a=5;
c. Assingment Statement : a=6;
d. Conditional statement
Simple if (nesting not allowed):
if then
Endif
Switch Statement (nesting not allowed):
Switch()
Cases
Value 1:
Break;
Value n:
Repetition Statement (nesting not allowed)
A. Repeat
Until ()
B. While (relational expression)
Endwhile
C. For = start value, end value, inc/dec………
Endfor
I/O Statement:

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

2.3 Code generated


START:
MOV AX, @DATA
MOV DS, AX
MOV AX,
MOV r, AX
MOV AX,
MOV flg, AX
LB01:
MOV AX,
CMP AX,
JNE LB01
MOV AX,
CMP AX,
JNE LB01
MOV AX,
MOV flg, AX
LB01:
MOV AX,
SUB AX,
MOV c,
AXJMP LB01
LB01:
MOV AX, 4C00
HINT 21H
END START
break;
Endcase

2.2 Sample Program II

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

1.2 Lexical analyser


Lexical analysis is the process of converting a sequence of characters from source program
into a sequence of tokens. A program which performs lexical analysis is termed as a lexical
analyzer (lexer), tokenizer or scanner.
Lexical analysis consists of two stages of processing which are as follows:
• Scanning
• Tokenization
Token is a valid sequence of characters which are given by lexeme. In a programming
language,
• keywords,
• constant,
• identifiers,
• numbers,
• operators and
• punctuations symbols
are possible tokens to be identified.
For example : c=a+b;
In this c,a and b are identifiers and ‘=’ and ‘*’ are mathematical operators.
Lexical Errors
• A character sequence that cannot be scanned into any valid token is a lexical error.
• Lexical errors are uncommon, but they still must be handled by a scanner.
10
• Misspelling of identifiers, keyword, or operators are considered as lexical errors.

Usually, a lexical error is caused by the appearance of some illegal character, mostly at the
beginning of a token.

1.3 Syntax analyser


Syntax analysis is the second phase of compiler. Syntax analysis is also known as parsing.
Parsing is the process of determining whether a string of tokens can be generated by a
grammar.
It is performed by syntax analyzer which can also be termed as parser.
In addition to construction of the parse tree, syntax analysis also checks and reports syntax
errors accurately. Parser is a program that obtains tokens from lexical analyzer and constructs
the parse tree which is passed to the next phase of compiler for further processing.
Parser implements context free grammar for performing error checks.
Types of Parser

 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

1.5Intermediate code generator


Intermediate code generation is the process by which a compiler's code generator converts
some intermediate representation of source code into a form (e.g., machine code) that can be
readily executed by a machine.
Intermediate code generation produces intermediate representations for the source program
which are of the following forms:
     o Postfix notation
     o Three address code
     o Syntax tree
Most commonly used form is the three address code.
        t1 = inttofloat (5)
        t2 = id3* tl
        t3 = id2 + t2
        id1 = t3

Properties of intermediate code


• It should be easy to produce.
• It should be easy to translate into target program.
After intermediate code generation the front end part of compiler finishes.The output to
intermediate code generated is fed as input to back end of compiler , which converts this
Intermediate code to machine code.
Chapter 4
Screenshots

2.parser
3.
4.

You might also like