Lecture 1
Course Information
Course Code: CS411
Course Title: Compiler Construction
Objective:
Acquire the practical skills to write a simple compiler for an
imperative programming language
Understand the concepts of scanning, parsing, name
management in nested scopes, and code generation
Book: Compilers Principles, Techniques, and
Tools by Alfred V.Aho, Ravi Sethi,
Jeffery D.Ullman
References:
Introduction to computer theory
Automata theory by Daniel I.A. Cohen
Pre Req: Theory of Automata
Marks Distribution
2
What is a compiler?
It is a language processor !!
Translator
It is a program that can read a program in one
language (Java, C, Lisp, C#, Pascal) etc. – the
source language – and translate it into an
equivalent program in another language –
target language.
3
Compilation Process
Something that
Something we
computer can
can understand
understand easily
easily
Error Messages
4
Phases of a Compiler
Analysis Synthesis
Source Code
Lexical Analyzer
Syntax Analyzer
Analysis
Code Optimizer
Synthesis
Code Generator
Object Code 6
Position=initial + rate*60
Source Code
Lexical Analyzer
Syntax Analyzer
Code Optimizer
Code Generator
Object Code 7
Lexical Analyzer (Scanner)
How humans understand English?
This is a sentence.
9
Learn by doing.
10
Lexical Analyzer (Scanner)
It reads a stream of characters and groups the
characters into tokens
Learn by Example
Position = initial + rate*60
Tokens Generated
1. Identifier#1 Position
2. Assignment Operator =
3. Identifier#2 initial
4. Addition Operator +
5. Identifier#3 rate
6. Multiplication Operator *
7. Number 60
Learn by doing
Percentage = Marks_Obtained / Total * 100
11
Source Code
id1 = id2 + id3*number
Lexical Analyzer
Syntax Analyzer
Code Optimizer
Code Generator
Object Code 12
Syntax Analyzer (Parser)
How human understand English?
Subject Object
Sentence
14
Syntax Analyzer (Parser)
Uses the tokens produced by the lexical analyzer
to create a tree-like intermediate representation.
Parse tree depicts the grammatical structure of
the token stream.
Example
Source Code --> Position = initial + rate*60
Lexical Analyzer --> id1= id2+ id3 * number
Parse Tree / Syntax Tree
=
15
Syntax Analyzer (Parser)
=
id1 +
id2 Id3 * 60
16
Syntax Analyzer (Parser)
id1
+
position
id2 *
rate 60
17
Syntax Analyzer (Parser)
Learn by doing
Percentage = Marks_Obtained / Total *
100
18
=
Source Code
id1 +
Lexical Analyzer position
id2 *
initial id3 numb
Syntax Analyzer r
rate 60
Semantic Analyzer
Error
Handler
Intermediate Code Generator
Code Optimizer
Code Generator
Object Code 19
Lets Revise !
Source Code Position=initial + rate*60
Lexical Analyzer
Syntax Analyzer
Code Optimizer
Code Generator
Object Code 21
Source Code id1 = id2 + id3*number
Lexical Analyzer
Syntax Analyzer
Code Optimizer
Code Generator
Object Code 22
=
=
Source Code
id1
id1 +
+
Lexical Analyzer position
position id2
id2 **
initial
initial id3 numb
numb
id3
Syntax Analyzer rr
rate
rate 60
60
Semantic Analyzer
Error
Handler
Intermediate Code Generator
Code Optimizer
Code Generator
Object Code 23