You are on page 1of 23

[KAWEESA ALEX]

[COMPILER CONSTRUCTION]
[KAMPALA UNIVERSITY]
[BSCIT]

COMPILER
CONSTRUCTION

1st LECTURE
LECTURE PROGRAMS

LECTURE 1 LECTURE 2 LECTURE 3 LECTURE 4 LECTURE 5 LECTURE 6

22rd /09/2022 29th /09/ 2022 06th /10/2022 13th /10/2022 20th /10/2022 27th /10/2022

Introduction To Major Data Description of Description of COURSE WORK Description of


Compile Structure in a Event Event Event
Construction Compiler

LECTURE 7 LECTURE 8 LECTURE 9 LECTURE 10 LECTURE 11 EXAMS

3rd /NOV/2022 10th /NOV/ 2022 17th /NOV/2022 24th /NOV/2022 1st /DEC/2022 8th /DEC2022

Introduction To Description of Description of Description of RECAP END OF


Compile Event Event Event SEMESTER
Construction EXAMS
What you'll learn

• Compiler construction and compiler design complete


course in a very easy and simple approach
• Different Phases of Compiler Construction:
• 1. Lexical Analysis (scanner) Phase
• 2. Syntax Analysis (parser) Phase
• 3. Semantics Analysis (mapper) Phase
• 4. Code Optimization Phase
• 5. Code Generation Phase
• 6. Compiler Projects
• 7. Compiler Quizzes
REQUIREMENTS

• If you have zero knowledge of compiler, I claim, you will


learn this course without any problem
• No programming experience needed, I will teach you
everything you need to know
• You have need a little knowledge about computer related
terms like input, output, cpu, computer language etc.
• This compiler construction course will teach you the
lessons that have taken years to learn
• A Mac or PC computer or any Smart phone.
Description
Welcome to the course of Compiler Construction from scratch!!!!
What is Compiler Construction course?

•Course Description: The course is intended to


teach the students the basic techniques that
underlie the practice of Compiler
Construction.
•The course will introduce the theory and tools
that can be employed in order to perform
syntax-directed translation of a high-level
programming language into an executable code.
What is a compiler?

• A computer program translates one language


to another
Source Compiler Target
Program Program
• A compiler is a complex program
• From 10,000 to 1,000,000 lines of codes
• Compilers are used in many forms of
computing
• Command interpreters, interface programs
WHAT IS A COMPILER????????????
• In computing, a compiler is a computer program
that transforms source code written in a
programming language or computer language, into
another computer language.
NOTE:
The most common reason for transforming
source code is to create an executable
program.
PHASES OF COMPILER:

In this course, every concept of Compiler Construction


and Compiler Design is taught in an easy way. you will be
able to go through all phase of Compiler design in an easy
and understandable way. You will learn all the phases of
compiler:
 Lexical analysis phase. (The Scanner)
 Syntax analysis phase. (The parser)
 Semantic analysis phase.
 Source code optimizer.
 Code generation phase.
 Target Code Optimizer.
The Phases of a Compiler
Lexical analysis phase.
(The Scanner)
This collects sequences of characters into meaningful
units called “Tokens” .
An example: a[index]=4+2
 a identifier
 [ left bracket
 Index identifier
 ] right brakect
 = assignment
 4 number
 + plus sign
 2 number
Other operations: it may enter literals into the literal table
Syntax analysis phase.
(The parser)
Syntax analysis: it determines the structure of the program
The result of syntax analysis are parser tree or syntax tree

An example: a[index]=4+2

Bellow is the description of


 Parser tree and
 Syntax tree (abstract syntax tree)
The parse tree.
The syntax tree
Semantic analysis phase.

The Semantic Analyzer: The semantics of a program


are its “meaning” , as opposed to its syntax, or structure,
that – determines some of its running time behaviors prior
to execution. • Static semantics: declarations and type
checking • Attributes: The extra pieces of information
computed by semantic analyzer

An example: a[index]=4=2 – The syntax tree annotated


with attribute
Annotated Syntax Tree
The Source Code Optimizer
• The earliest point of most optimization steps is just
after semantic analysis.
• The code improvement depends only on the
source code, and as a separate phase.
• Individual compilers exhibit a wide variation in
optimization kinds as well as placement.

• An example: a[index]=4+2
– Constant folding performed directly on annotated tree.

– Using intermediate code: three-address code, p-code.


Optimizations on Annotated Tree

Assign-expression

subscript-expression additive-expression
integer integer

identifier identifier number number


a index 4 2
array of integer integer integer integer
Optimization on Intermediate
Code

t = 4 + 2
a[index]=t

t= 6
a[index]=t

a[index]=6
Code generation phase
(The Code Generate)

• It takes the intermediate code or IR and generates code


for target machine
• The properties of the target machine become the major
factor:
– Using instructions and representation of data

• An example: a[index]=4+2
– Code sequence in a hypothetical assembly
language
A possible code sequence

MOV R0, index


MUL R0,2
a[index]=6 MOV R1,&a
ADD R1,R0
MOV *R1,6
The Target Code Optimizer

• It improves the target code generated by the


code generator:
– Address modes choosing
– Instructions replacing
– As well as redundant eliminating
MOV R0, index
MUL R0,2 MOV R0, index
MOV R1,&a SHL R0
ADD R1,R0 MOV &a[R1],6
MOV *R1,6
Conclusion
• Can we have assignment today or we have it next
week??????????????

NEXT LECTURE 29TH SEPT, 2022

Major Data Structure in a


Compiler
Resources
Course Web : http://nmlab.zju.edu.cn/~compiler
Email: alexkaweesa1986@gmail.com

END

You might also like