Anda di halaman 1dari 23

Compiler Construction

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.

 A compiler needs to report any errors in the


source program that it detects during the
translation process. (e.g. a missing semicolon at
the end of a statement)

3
Compilation Process
Something that
Something we
computer can
can understand
understand easily
easily

Source Code Compilation Process Object Code

Error Messages

4
Phases of a Compiler

Analysis Synthesis
Source Code

Lexical Analyzer

Syntax Analyzer
Analysis

Symbol Semantic Analyzer


Error
Table
Handler
Manager
Intermediate Code Generator

Code Optimizer

Synthesis
Code Generator

Object Code 6
Position=initial + rate*60
Source Code

Lexical Analyzer

Syntax Analyzer

Symbol Semantic Analyzer


Error
Table
Handler
Manager
Intermediate Code Generator

Code Optimizer

Code Generator

Object Code 7
Lexical Analyzer (Scanner)
How humans understand English?

 First step recognize words.


 Smallest units above letters.

This is a sentence.

9
Learn by doing.

ist his ase nte nce

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

Symbol Semantic Analyzer


Error
Table
Handler
Manager
Intermediate Code Generator

Code Optimizer

Code Generator

Object Code 12
Syntax Analyzer (Parser)
How human understand English?

This line is a longer sentence.

Article Noun Verb Article Adjective Noun

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
=

id1 id2 + id3 * number

15
Syntax Analyzer (Parser)
=

id1 +

id2 Id3 * 60

16
Syntax Analyzer (Parser)

id1
+

position
id2 *

initial id3 number

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

Symbol Semantic Analyzer


Error
Table
Handler
Manager
Intermediate Code Generator

Code Optimizer

Code Generator

Object Code 21
Source Code id1 = id2 + id3*number

Lexical Analyzer

Syntax Analyzer

Symbol Semantic Analyzer


Error
Table
Handler
Manager
Intermediate Code Generator

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

Anda mungkin juga menyukai