Professional Documents
Culture Documents
Lecture 1
Course Information
Course Code:CS422
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
o Pre Req: Theory of Automata
o 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)
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
9
Source Code
id1 = id2 + id3*number
Lexical Analyzer
Syntax Analyzer
Code Optimizer
Code Generator
Object Code 10
Syntax Analyzer (Parser)
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
12
Syntax Analyzer (Parser)
=
id1 +
id2 Id3 * 60
13
Syntax Analyzer (Parser)
id1
+
position
id2 *
rate 60
14
Syntax Analyzer (Parser)
Learn by doing
Percentage = Marks_Obtained / Total *
100
15
=
Source Code
id1 +
Lexical Analyzer position
id2 *
initial id3 numbe
Syntax Analyzer
rate 60
Semantic Analyzer
Error
Handler
Intermediate Code Generator
Code Optimizer
Code Generator
Object Code 16
Lets Revise !
Source Code Position=initial + rate*60
Lexical Analyzer
Syntax Analyzer
Code Optimizer
Code Generator
Object Code 18
Source Code id1 = id2 + id3*number
Lexical Analyzer
Syntax Analyzer
Code Optimizer
Code Generator
Object Code 19
=
=
Source Code
id1
id1 +
+
Lexical Analyzer position
position id2
id2 **
initial
initial id3 numbe
numbe
id3
Syntax Analyzer
rate
rate 60
60
Semantic Analyzer
Error
Handler
Intermediate Code Generator
Code Optimizer
Code Generator
Object Code 20