0% found this document useful (0 votes)
15 views

Types of Compiler

This document discusses the two-pass compiler model. It explains the front end and back end of a compiler and their roles in mapping source code to an intermediate representation and then to machine code. The front end recognizes syntax and reports errors while producing IR. The back end maps IR to target machine code.

Uploaded by

Kashmala Alam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Types of Compiler

This document discusses the two-pass compiler model. It explains the front end and back end of a compiler and their roles in mapping source code to an intermediate representation and then to machine code. The front end recognizes syntax and reports errors while producing IR. The back end maps IR to target machine code.

Uploaded by

Kashmala Alam
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 21

COMPILER CONSTRUCTION

Instructor:
Mr. Sheraz Babar

1
Lecture 03

2
Two-pass Compiler

source Front IR Back machine


code End End code

errors

3
Two-pass Compiler
 Use an intermediate
representation (IR)
 Front end maps legal source
code into IR

4
Two-pass Compiler
 Back end maps IR into target
machine code
 Admits multiple front ends &
multiple passes

5
Two-pass Compiler
 Front end is O(n) or
O(n log n)
 Back end is NP-Complete
(NPC)

6
Front End
 Recognizes legal (& illegal)
programs
 Report errors in a useful
way
 Produce IR & preliminary
storage map
7
The Front-End
source tokens IR
scanner parser
code

errors
Modules
 Scanner
 Parser
8
Scanner
 Maps character stream into
words – basic unit of syntax
 Produces pairs –
• a word and
• its part of speech

9
Scanner
 Example
x = x + y
becomes
<id,x>
<id,x>
<assign,=> word
<id,x> token type
<op,+>
<id,y>
10
Scanner
 we call the pair
“<token type, word>” a
“token”
 typical tokens: number,
identifier, +, -, new, while, if

11
Parser

source tokens IR
scanner parser
code

errors

12
Parser
 Recognizes context-free
syntax and reports errors
 Guides context-sensitive
(“semantic”) analysis
 Builds IR for source
program
13
Context-Free Grammars
 Context-free syntax is specified
with a grammar G=(S,N,T,P)
 S is the start symbol
 N is a set of non-terminal symbols
 T is set of terminal symbols or words
 P is a set of productions or rewrite
rules

14
Context-Free Grammars
Grammar for expressions
1. goal → expr
2. expr → expr op term
3. | term
4. term → number
5. | id
6. op → +
7. | - 15
The Front End
 For this CFG
S = goal
T = { number, id, +, -}
N = { goal, expr, term, op}
P = { 1, 2, 3, 4, 5, 6, 7}

16
Context-Free Grammars
 Given a CFG, we can derive
sentences by repeated
substitution
 Consider the sentence
(expression)
x+2–y
17
Derivation
Production Result
goal
1 expr
2 expr op term
5 expr op y
7 expr – y
2 expr op term – y
4 expr op 2 – y
6 expr + 2 – y
3 term + 2 – y
5 x+2–y
18
The Front End
 To recognize a valid
sentence in some CFG, we
reverse this process and
build up a parse
 A parse can be represented
by a tree: parse tree or
syntax tree
19
Parse
Production Result
goal
1 expr
2 expr op term
5 expr op y
7 expr – y
2 expr op term – y
4 expr op 2 – y
6 expr + 2 – y
3 term + 2 – y
5 x+2–y
20
The End

21

You might also like