Week 3 Lec 6 CC

You might also like

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 19

1

Lahore Garrison University


CSC373-Compiler Construction
Week-3 Lecture-6
Semester-#5 Fall 2019
Prepared by:

Eisha Tir Razia


2
Overview of previous lecture

 Symbol Table
 How to construct Symbol Table?

Lahore Garrison University


3
Preamble of each lecture

 Two-Pass Compiler

Lahore Garrison University


4
Lecture Outcomes

 Understanding related to
 Two-Pass Compiler

Lahore Garrison University


5
The Grouping of Phases into Passes

 The discussion of phases deals with the logical organization of a


compiler. In an implementation, activities from several phases may be
grouped together into a pass that reads an input file and writes an
output file.
 For example, the front-end phases of lexical analysis, syntax analysis,
semantic analysis, and intermediate code generation might be grouped
together into one pass. Code optimization might be an optional pass.
Then there could be a back-end pass consisting of code generation for a
particular target machine.

Lahore Garrison University


6
Cont..

 Some compiler collections have been created around carefully,


designed intermediate representations that allow the front end for a
particular language to interface with the back end for a certain target
machine. With these collections, we can produce compilers for different
source languages, for one target machine by combining different front
ends with the back end for that target machine.
 Similarly, we can produce compilers for different target machines, by
combining a front end with back ends for different target machines.

Lahore Garrison University


Two-pass Compiler 7

source Front IR Back machine


code End End code

errors
8

Two-pass Compiler

 Use an intermediate representation (IR)


 Front end maps legal source code into IR
9

Two-pass Compiler

 Back end maps IR into target machine code


 Admits multiple front ends & multiple passes
10
Two-pass Compiler

 Front end is O(n) or O(n log n)


 Back end is NP-Complete (NPC)
11
Front End

 Recognizes legal (& illegal) programs


 Report errors in a useful way
 Produce IR & preliminary storage map
The Front-End
12

source tokens IR
scanner parser
code

errors
Modules
 Scanner
 Parser
13
Scanner

source tokens IR
scanner parser
code

errors
Scanner 14

 Maps character stream into words – basic unit of syntax


 Produces pairs –
a word and
 its part of speech
15
Scanner

 Example
x = x + y
becomes
<id,x>
<id,x>
<assign,=>
<id,x> word
<op,+>
<id,y> token type
Scanner 16

 we call the pair “<token type, word>” a “token”


 typical tokens: number, identifier, +, -, new, while, if
Parser
17

 Recognizes context-free syntax and reports errors


 Guides context-sensitive (“semantic”) analysis
 Builds IR for source program
18

Q&A

Lahore Garrison University


19
References

 These lecture notes were taken from following source:


 Compilers: Principles, Techniques, and Tools By Alfred V. Aho, Ravi
Sethi, Jeffrey D. Ullman, Contributor Jeffrey D. Ullman, Addison-
Wesley Pub. Co., 2nd edition, 2006

Lahore Garrison University

You might also like