Home Page
Title Page
JJ II
J I
Page 1 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
CS432F/CSL 728:Compiler Design
July 2004
Department of Computer Science and EngineeringI. I. T. Delhi, Hauz Khas, New Delhi 110 016.
July 30, 2004
Home Page
Title Page
JJ II
J I
Page 2 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Contents• first The Bigpicture last
• first Lexical Analysis last
• first Syntax Analysis last
– first Context-free Grammars last
– first Ambiguity last
– first Shift-Reduce Parsing last
– first Parse Trees last
• first Semantic Analysis last
• first Synthesized Attributes last
• first Inherited Attributes last
Contd . . .
Home Page
Title Page
JJ II
J I
Page 3 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Contents. . . Contd
• first Abstract Syntax Trees last
• first Symbol Tables last
• first Intermediate Representation last
– IR: Properties
– Typical Instruction Set
– IR: Generation
• first Runtime Structure last
Home Page
Title Page
JJ II
J I
Page 4 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
Title Page
JJ II
J I
Page 5 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
The Big Picture: 1
SCANNER
stream ofcharacters
stream oftokens
Home Page
Title Page
JJ II
J I
Page 6 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
The Big Picture: 2
SCANNER
PARSER
stream ofcharacters
stream oftokens
parse tree
Home Page
Title Page
JJ II
J I
Page 7 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
The Big Picture: 3
SCANNER
PARSER
SEMANTIC ANALYZER
stream ofcharacters
stream oftokens
parse tree
abstractsyntax tree
Home Page
Title Page
JJ II
J I
Page 8 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
The Big Picture: 4
SCANNER
PARSER
SEMANTIC ANALYZER
I.R. CODE GENERATOR
stream ofcharacters
stream oftokens
parse tree
intermediaterepresentation
abstractsyntax tree
Home Page
Title Page
JJ II
J I
Page 9 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
The Big Picture: 5
SCANNER
PARSER
SEMANTIC ANALYZER
I.R. CODE GENERATOR
OPTIMIZER
stream ofcharacters
stream oftokens
parse tree
intermediaterepresentation
optimizedintermediaterepresentation
abstractsyntax tree
Home Page
Title Page
JJ II
J I
Page 10 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
The Big Picture: 6
SCANNER
PARSER
SEMANTIC ANALYZER
I.R. CODE GENERATOR
OPTIMIZER
CODE GENERATOR
stream ofcharacters
stream oftokens
parse tree
intermediaterepresentation
optimizedintermediaterepresentation
target code
abstractsyntax tree
Home Page
Title Page
JJ II
J I
Page 11 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
The Big Picture: 7
SCANNER
PARSER
SEMANTIC ANALYZER
I.R. CODE GENERATOR
OPTIMIZER
CODE GENERATOR
ERROR−
HANDLER
SYMBOL
TABLE
MANAGER
stream ofcharacters
stream oftokens
parse tree
intermediaterepresentation
optimizedintermediaterepresentation
target code
abstractsyntax tree
Home Page
Title Page
JJ II
J I
Page 12 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
The Big Picture: 7
SCANNER
PARSER
SEMANTIC ANALYZER
I.R. CODE GENERATOR
OPTIMIZER
CODE GENERATOR
ERROR−
HANDLER
SYMBOL
TABLE
MANAGER
stream ofcharacters
stream oftokens
parse tree
intermediaterepresentation
optimizedintermediaterepresentation
target code
abstractsyntax tree
Scanner Parser Semantic AnalysisSymbol TableIR Optimization Contents
Home Page
Title Page
JJ II
J I
Page 13 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
Title Page
JJ II
J I
Page 14 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Scanning: 1
• Takes a stream of characters and identifies tokens fromthe lexemes.
• Eliminates comments and redundant whitepace.
• Keeps track of line numbers and column numbers andpasses them as parameters to the other phases to en-able error-reporting to the user.
Home Page
Title Page
JJ II
J I
Page 15 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Scanning: 2• Whitespace: A sequence of space, tab, newline,
carriage-return, form-feed characters etc.
• Lexeme: A sequence of non-whitespace characters de-limited by whitespace or special characters (e.g. oper-ators like +, -, *).
• Examples of lexemes.
– reserved words, keywords, identifiers etc.
– Each comment is usually a single lexeme
– preprocessor directives
Home Page
Title Page
JJ II
J I
Page 16 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Scanning: 3• Token: A sequence of characters to be treated as a
single unit.
• Examples of tokens.
– Reserved words (e.g. begin, end, struct, if etc.)
– Keywords (integer, true etc.)
– Operators (+, &&, ++ etc)
– Identifiers (variable names, procedure names, pa-rameter names)
– Literal constants (numeric, string, character con-stants etc.)
– Punctuation marks (:, , etc.)
Home Page
Title Page
JJ II
J I
Page 17 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Scanning: 4• Identification of tokens is usually done by a Determinis-
tic Finite-state automaton (DFA).
• The set of tokens of a language is represented by alarge regular expression.
• This regular expression is fed to a lexical-analyser gen-erator such as Lex, Flex or ML-Lex.
• A giant DFA is created by the Lexical analyser genera-tor.
Home Page
Title Page
JJ II
J I
Page 18 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Scanning: 5
1
2 3 4
5 6
7 8
913
i
f
a−h,j−
z
a−e,g−z
0−9,a−z 0−9,a−z
0−9
0−9
0−9
0−9 0−9
(
* 10 11 12
14
*
if identifier
real
real
error comment
whitespace
error
int
identifierot
her
char
s
any char
)
The Big Picture
Home Page
Title Page
JJ II
J I
Page 19 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Syntax AnalysisConsider the following two languages over an alphabetA = {a, b}.
R = {anbn|n < 100}P = {anbn|n > 0}
• R may be finitely represented by a regular expression(even though the actual expression is very long).
• However, P cannot actually be represented by a regularexpression
• A regular expression is not powerful enough to repre-sent languages which require parenthesis matching toarbitrary depths.
• All high level programming languages require an under-lying language of expressions which require parenthe-ses to be nested and matched to arbitrary depth.
Home Page
Title Page
JJ II
J I
Page 20 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
CF-Grammars:Definition
A context-free grammar (CFG) G = 〈N, T , P, S〉 consistsof
• a set N of nonterminal symbols,
• a set T of terminal symbols or the alphabet,
• a set P of productions or rewrite rules,
• each production is of the form X −→ α, where
– X ∈ N is a nonterminal and
– α ∈ (N ∪ T )∗ is a string of terminals and nontermi-nals
• a start symbol S ∈ N .
Home Page
Title Page
JJ II
J I
Page 21 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
CFG: ExampleG = 〈{S}, {a, b}, P, S〉, where S −→ ab and S −→ aSbare the only productions in P .Derivations look like this:
S ⇒ ab
S ⇒ aSb ⇒ aabb
S ⇒ aSb ⇒ aaSbb ⇒ aaabbb
L(G), the languagegenerated by G is {anbn|n > 0}.
Actually can be proved by induction on the length and struc-ture of derivations.
Home Page
Title Page
JJ II
J I
Page 22 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
CFG: Empty wordG = 〈{S}, {a, b}, P, S〉, where S −→ SS | aSb | εgenerates all sequences of matching nested parentheses,including the empty word ε.
A leftmost derivation might look like this:
S ⇒ SS ⇒ SSS ⇒ SS ⇒ aSbS ⇒ abS ⇒ abaSb . . .
A rightmost derivation might look like this:
S ⇒ SS ⇒ SSS ⇒ SS ⇒ SaSb ⇒ Sab ⇒ aSbab . . .
Other derivations might look like God alone knows what!
S ⇒ SS ⇒ SSS ⇒ SS ⇒ . . .
Could be quite confusing!
Home Page
Title Page
JJ II
J I
Page 23 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
CFG: Derivation trees1
Derivation sequences
• put an artificial order in which productions are fired.
• instead look at treesof derivations in which we may thinkof productions as being fired in parallel.
• There is then no highlighting in red to determine whichcopy of a nonterminal was used to get the next memberof the sequence.
• Of course, generation of the empty word ε must beshown explicitly in the tree.
Home Page
Title Page
JJ II
J I
Page 24 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
CFG: Derivation trees2
S
S
S
S
S S
S
S
a b a b
a bε
ε
ε
Derivation tree of
abaabb
ε
Home Page
Title Page
JJ II
J I
Page 25 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
CFG: Derivation trees3
S
S
S
S
S
S
S
a b
a b
ε
ε
S
a b
εAnother
Derivation tree of
abaabb
Home Page
Title Page
JJ II
J I
Page 26 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
CFG: Derivation trees4
S
S
S
S
S
S
a b
a b
ε
Sa b
ε
S
ε
Yet another
Derivation tree ofabaabb
Home Page
Title Page
JJ II
J I
Page 27 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
Title Page
JJ II
J I
Page 28 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Ambiguity: 1E → I | C | E+E | E∗EI → y | zC → 4
Consider the sentence y + 4 ∗ z.
EE
Home Page
Title Page
JJ II
J I
Page 29 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Ambiguity: 2E → I | C | E+E | E∗EI → y | zC → 4
Consider the sentence y + 4 ∗ z.
E
E E*
E
EE +
Home Page
Title Page
JJ II
J I
Page 30 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Ambiguity: 3E → I | C | E+E | E∗EI → y | zC → 4
Consider the sentence y + 4 ∗ z.
E
E
E
E*
E+
I
E
E
E
E +
EI
*
Home Page
Title Page
JJ II
J I
Page 31 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Ambiguity: 4E → I | C | E+E | E∗EI → y | zC → 4
Consider the sentence y + 4 ∗ z.
E
E
E
E*
E+
I C
I
z
E
E
E
E +
E
C
I
y
I
*
Home Page
Title Page
JJ II
J I
Page 32 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Ambiguity: 5E → I | C | E+E | E∗EI → y | zC → 4
Consider the sentence y + 4 ∗ z.
E
E
E
E*
E+
I
y
C
4
I
z
E
E
E
E +
E
C
I
y
4
I
z
*
Home Page
Title Page
JJ II
J I
Page 33 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
Title Page
JJ II
J I
Page 34 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 0−
/
b ( )
a −
a
a / b
r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
Home Page
Title Page
JJ II
J I
Page 35 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 1−
/
b ( )a
− a / b
r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
Shifta
Principle:Reduce whenever possible.Shift only whenreduce isimpossible
Home Page
Title Page
JJ II
J I
Page 36 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 2−
/
b ( )a
− a / b
r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
D Reduce by r5
Home Page
Title Page
JJ II
J I
Page 37 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 3−
/
b ( )a
− a / b
r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
T Reduce by r4
Home Page
Title Page
JJ II
J I
Page 38 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 4−
/
b ( )a
− a / b
r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
E Reduce by r2
Home Page
Title Page
JJ II
J I
Page 39 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 5−
/
b ( )a
−
a / b
r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
E
Shift
Home Page
Title Page
JJ II
J I
Page 40 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 6−
/
b ( )a
−
/ b
r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
E
Shifta
Home Page
Title Page
JJ II
J I
Page 41 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 7−
/
b ( )a
−
/ b
r1. E E T
r2 E T
r3 T T D
r4 T D
r5 D | | E
E
D
E
Reduce by r5
Home Page
Title Page
JJ II
J I
Page 42 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 8−
/
b ( )a
−
/ b
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
T Reduce by r4
Home Page
Title Page
JJ II
J I
Page 43 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 8a−
/
b ( )a
/ b
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
T Reduce by r4−
Home Page
Title Page
JJ II
J I
Page 44 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 9a−
/
b ( )a
/ b
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
Reduce by r1
Home Page
Title Page
JJ II
J I
Page 45 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 10a−
/
b ( )a
b
/
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
Shift
Home Page
Title Page
JJ II
J I
Page 46 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 11a−
/
b ( )a
/
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
Shiftb
Home Page
Title Page
JJ II
J I
Page 47 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 12a−
/
b ( )a
/
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
D Reduce by r5
Home Page
Title Page
JJ II
J I
Page 48 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 13a−
/
b ( )a
/
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
T Reduce by r4
Home Page
Title Page
JJ II
J I
Page 49 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 14a−
/
b ( )a
/
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
E Reduce by r2Stu
ck!
Get back!
Home Page
Title Page
JJ II
J I
Page 50 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 14b−
/
b ( )a
/
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
E Reduce by r2Get back!
Home Page
Title Page
JJ II
J I
Page 51 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 13b−
/
b ( )a
/
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
T Reduce by r4Get back!
Home Page
Title Page
JJ II
J I
Page 52 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 12b−
/
b ( )a
/
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
D Reduce by r5Get back!
Home Page
Title Page
JJ II
J I
Page 53 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 11b−
/
b ( )a
/
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
ShiftbGet back!
Home Page
Title Page
JJ II
J I
Page 54 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 10b−
/
b ( )a
b
/
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
ShiftGet back!
Home Page
Title Page
JJ II
J I
Page 55 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 9b−
/
b ( )a
/ b
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
Reduce by r1
Get back towhere youonce belonged!
Home Page
Title Page
JJ II
J I
Page 56 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 8b−
/
b ( )a
/ b
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
T Reduce by r4−
Shift insteadof reduce here!
Principle:modified
Reduce whenever possible, butbut depending upon
lookahead
Shift−reduce
conflict
Home Page
Title Page
JJ II
J I
Page 57 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 8−
/
b ( )a
−
/ b
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
T Reduce by r4
Home Page
Title Page
JJ II
J I
Page 58 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 9−
/
b ( )a
−
/
b
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
T
Shift
Home Page
Title Page
JJ II
J I
Page 59 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 10−
/
( )a
−
/
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
T
Shift
b
b
Home Page
Title Page
JJ II
J I
Page 60 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 11−
/
b ( )a
−
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
T
/
D Reduce by r5
Home Page
Title Page
JJ II
J I
Page 61 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 12−
/
b ( )a
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
T Reduce by r3−
Home Page
Title Page
JJ II
J I
Page 62 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: 13−
/
b ( )a
r1. E E T
r2 E T
r3 T T D
r4 D
r5 D | | E
EE
T
Reduce by r1
Home Page
Title Page
JJ II
J I
Page 63 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parse Trees: 0−r1. E E T
r2 E T
/r3 T T D b ( )aD | | Er5
r4 T D
a− / ba
Home Page
Title Page
JJ II
J I
Page 64 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parse Trees: 1
a − a / b
−r1. E E T
r2 E T
/r3 T T D b ( )aD | | Er5
r4 T D
D
Home Page
Title Page
JJ II
J I
Page 65 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parse Trees: 2
a − a / b
T
−r1. E E T
r2 E T
/r3 T T D b ( )aD | | Er5
r4 T D
D
Home Page
Title Page
JJ II
J I
Page 66 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parse Trees: 3
a − a / b
T
−r1. E E T
r2 E T
/r3 T T D b ( )aD | | Er5
r4 T D
E
D
Home Page
Title Page
JJ II
J I
Page 67 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parse Trees: 3a
a − a / b
T
−r1. E E T
r2 E T
/r3 T T D b ( )aD | | Er5
r4 T D
E
D
Home Page
Title Page
JJ II
J I
Page 68 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parse Trees: 3b
a − a / b
T
−r1. E E T
r2 E T
/r3 T T D b ( )aD | | Er5
r4 T D
E
D
Home Page
Title Page
JJ II
J I
Page 69 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parse Trees: 4
a − a
D
/ b
T
−r1. E E T
r2 E T
/r3 T T D b ( )aD | | Er5
r4 T D
E
D
Home Page
Title Page
JJ II
J I
Page 70 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parse Trees: 5
a − a
D
/ b
TT
−r1. E E T
r2 E T
/r3 T T D b ( )aD | | Er5
r4 T D
E
D
Home Page
Title Page
JJ II
J I
Page 71 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parse Trees: 5a
a − a
D
/ b
TT
−r1. E E T
r2 E T
/r3 T T D b ( )aD | | Er5
r4 T D
E
D
Home Page
Title Page
JJ II
J I
Page 72 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parse Trees: 5b
a − a
D
/ b
TT
−r1. E E T
r2 E T
/r3 T T D b ( )aD | | Er5
r4 T D
E
D
Home Page
Title Page
JJ II
J I
Page 73 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parse Trees: 6
a − a
D
/ b
D
TT
−r1. E E T
r2 E T
/r3 T T D b ( )aD | | Er5
r4 T D
E
D
Home Page
Title Page
JJ II
J I
Page 74 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parse Trees: 7
a − a
D
/ b
D
T
T
T
−r1. E E T
r2 E T
/r3 T T D b ( )aD | | Er5
r4 T D
E
D
Home Page
Title Page
JJ II
J I
Page 75 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parse Trees: 8
a − a
D
/ b
D
T
T
T
−r1. E E T
r2 E T
/r3 T T D b ( )aD | | Er5
r4 T D
E
E
D
Home Page
Title Page
JJ II
J I
Page 76 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: Summary: 1• All high-level languages are designed so that they may
be parsed in this fashion with only a single token look-ahead.
• Parsers for a language can be automatically con-structed by parger-generators such as Yacc, Bison, ML-Yacc.
• Shift-reduce conflicts if any, are automatically detectedand reported by the parser-generator.
• Shift-reduce conflicts may be avoided by suitablyredesigning the context-free grammar.
Home Page
Title Page
JJ II
J I
Page 77 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Parsing: Summary: 2• Very often shift-reduce conflicts may occur because
of the prefix problem. In such cases many parser-generators resolve the conflict in favour of shifting.
• There is also a possiblility of reduce-reduce conflicts.This usually happens when there is more than one non-terminal symbol to which the contents of the stack mayreduce.
• A minor reworking of the grammar to avoid redundantnon-terminal symbols will get rid of reduce-reduce con-flicts.
The Big Picture
Home Page
Title Page
JJ II
J I
Page 78 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Semantic Analysis: 1• Every Programming langauge can be used to program
any computable function, assuming of course, it has
– unbounded memory, and
– unbounded time
• The parser of a programming language provides theframeworkwithin which the target code is to be gener-ated.
• The parser also provides a structuringmechanism thatdivides the task of code generation into bits and piecesdetermined by the individual nonterminals and produc-tion rules.
• However, contex-free grammars are not powerfulenough to represent all computable functions. Exam-ple, the language {anbncn|n > 0}.
Home Page
Title Page
JJ II
J I
Page 79 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Semantic Analysis: 2• There are context-sensitive aspects of a program that
cannot be represented/enforced by a context-free gram-mar definition. Examples include
– correspondence between formal and actual param-eters
– type consistency between declaration and use.
– scope and visibility issues with respect to identifiersin a program.
Home Page
Title Page
JJ II
J I
Page 80 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
Title Page
JJ II
J I
Page 81 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Synthesized Attributes:0
ET F
T
T F
/
/
(
nF
( )E
E
) n
E T
−
−
TF
Fn
n
Home Page
Title Page
JJ II
J I
Page 82 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Synthesized Attributes:1
ET F
T
T F
/
/
(
nF
( )E
E
) n
E T
−
−
TF
Fn
n
4
4
3 2 1
Synthesized Attributes
Home Page
Title Page
JJ II
J I
Page 83 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Synthesized Attributes:2
ET F
T
T F
/
/
(
nF
( )E
E
) n
E T
−
−
TF
Fn
n
4
4
3 2 1
4
Synthesized Attributes
Home Page
Title Page
JJ II
J I
Page 84 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Synthesized Attributes:3
ET F
T
T F
/
/
(
nF
( )E
E
) n
E T
−
−
TF
Fn
n
4
4
4
4
3 2 1
Synthesized Attributes
Home Page
Title Page
JJ II
J I
Page 85 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Synthesized Attributes:4
ET F
T
T F
/
/
(
nF
( )E
E
) n
E T
−
−
TF
Fn
n
4
4
4
4
4
3 2 1
Synthesized Attributes
Home Page
Title Page
JJ II
J I
Page 86 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Synthesized Attributes:5
ET F
T
T F
/
/
(
nF
( )E
E
) n
E T
−
−
TF
Fn
n
4
4
4
4
4
1
3 2 1
Synthesized Attributes
Home Page
Title Page
JJ II
J I
Page 87 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Synthesized Attributes:6
ET F
T
T F
/
/
(
nF
( )E
E
) n
E T
−
−
TF
Fn
n
4
4
4
4
4
1
1
3 2 1
Synthesized Attributes
Home Page
Title Page
JJ II
J I
Page 88 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Synthesized Attributes:7
ET F
T
T F
/
/
(
nF
( )E
E
) n
E T
−
−
TF
Fn
n
4
4
4
4
4
1
1
1
3 2 1
Synthesized Attributes
Home Page
Title Page
JJ II
J I
Page 89 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Synthesized Attributes:8
ET F
T
T F
/
/
(
nF
( )E
E
) n
E T
−
−
TF
Fn
n
4
4
4
4
4
1
1
1
3 2 1
3
Synthesized Attributes
Home Page
Title Page
JJ II
J I
Page 90 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Synthesized Attributes:9
ET F
T
T F
/
/
(
nF
( )E
E
) n
E T
−
−
TF
Fn
n
4
4
4
4
4
1
1
1
3
3 2 1
3
Synthesized Attributes
Home Page
Title Page
JJ II
J I
Page 91 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Synthesized Attributes:10
ET F
T
T F
/
/
(
nF
( )E
E
) n
E T
−
−
TF
Fn
n
4
4
4
4
4
1
1
1
3
3
3 2 1
3
Synthesized Attributes
Home Page
Title Page
JJ II
J I
Page 92 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Synthesized Attributes:11
ET F
T
T F
/
/
(
nF
( )E
E
) n
E T
−
−
TF
Fn
n
4
4
4
4
4
1
1
1
3 2
3
3 2 1
3
Synthesized Attributes
Home Page
Title Page
JJ II
J I
Page 93 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Synthesized Attributes:12
ET F
T
T F
/
/
(
nF
( )E
E
) n
E T
−
−
TF
Fn
n
4
4
4
4
4
1
1
1
3 2
23
3 2 1
3
Synthesized Attributes
Home Page
Title Page
JJ II
J I
Page 94 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Synthesized Attributes:13
ET F
T
T F
/
/
(
nF
( )E
E
) n
E T
−
−
TF
Fn
n
4
4
4
4
4
1
1
1
3 2
23
3 2 1
1
3
Synthesized Attributes
Home Page
Title Page
JJ II
J I
Page 95 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Synthesized Attributes:14
ET F
T
T F
/
/
(
nF
( )E
E
) n
E T
−
−
TF
Fn
n
4
4
4
4
4
1
1
1
3 2
23
3 2 1
1
1
3
Synthesized Attributes
Home Page
Title Page
JJ II
J I
Page 96 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
An Attribute Grammar
E
T
T F
/
nF
( )E
E T−
TF
Fn
n4
4
4
4
1
1
1
3 2
23
1
1
3
E0 → E1−T B E0.val := sub(E1.val, T.val)
E → T B E.val := T.val
T0 → T1/F B T0.val := div(T1.val, F.val)
T → F B T.val := F.val
F → (E) B F.val := E.val
F → n B F.val := n.val
Home Page
Title Page
JJ II
J I
Page 97 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Inherited Attributes: 0C-style declarations generating int x,y, z.
D → T L T → int | floatL → L,I | I I → x | y | z
T
int
L
z
L
L
x
I
y
I
D
I
,
,
x
D L T I
y z int
,
Home Page
Title Page
JJ II
J I
Page 98 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Inherited Attributes: 1C-style declarations generating int x,y, z.
D → T L T → int | floatL → L,I | I I → x | y | z
T
int
L
z
L
L
x
I
y
I
D
I
,
,
x
D L T I
y z int
int
int,
Home Page
Title Page
JJ II
J I
Page 99 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Inherited Attributes: 2C-style declarations generating int x,y, z.
D → T L T → int | floatL → L,I | I I → x | y | z
T
int
L
z
L
L
x
I
y
I
D
I
,
,
x
D L T I
y z int
int int
int
int,
Home Page
Title Page
JJ II
J I
Page 100 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Inherited Attributes: 3C-style declarations generating int x,y, z.
D → T L T → int | floatL → L,I | I I → x | y | z
T
int
L
z
L
L
x
I
y
I
D
I
,
,
x
D L T I
y z int
int int
int
int,
int
Home Page
Title Page
JJ II
J I
Page 101 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Inherited Attributes: 4C-style declarations generating int x,y, z.
D → T L T → int | floatL → L,I | I I → x | y | z
T
int
L
z
L
L
x
I
y
I
D
I
,
,
x
D L T I
y z int
int int
int
int,
int
int
int
Home Page
Title Page
JJ II
J I
Page 102 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Inherited Attributes: 5T
int
L
z
L
L
x
I
y
I
D
I
,
,
x
D L T I
y z int
int int
int
int,
int
int
int
int
int
int
Home Page
Title Page
JJ II
J I
Page 103 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Inherited Attributes: 6C-style declarations generating int x,y, z.
D → T L T → int | floatL → L,I | I I → x | y | z
T
int
L
z
L
L
x
I
y
I
D
I
,
,
x
D L T I
y z int
int int
int
int,
int
int
int
int
int
int
int
int
Home Page
Title Page
JJ II
J I
Page 104 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Inherited Attributes: 7C-style declarations generating int x,y, z.
D → T L T → int | floatL → L,I | I I → x | y | z
T
int
L
z
L
L
x
I
y
I
D
I
,
,
x
D L T I
y z int
int int
int
int,
int
int
int
int
int
int
int
int
int
Home Page
Title Page
JJ II
J I
Page 105 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
An Attribute Grammar
T
int
L
z
L
L
x
I
y
I
D
I
,
int
int,
int
int
int
int
int
int
int
int
int
D → TL B L.in := T.type
T → int B T.type := int.int
T → float B T.type := float.f loat
L0 → L1,I B L1 := L0.in
L → I B I.in := L.in
I → id B id.type := I.in
Home Page
Title Page
JJ II
J I
Page 106 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Home Page
Title Page
JJ II
J I
Page 107 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Abstract Syntax: 0E → E−T | TT → T/F | FF → n | (E)
Suppose we want to evaluate an expression (4− 1)/2.What we actually want is a tree that looks like this:
4 1
2−−
/
Home Page
Title Page
JJ II
J I
Page 108 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Evaluation: 0
4 1
2−−
/
Home Page
Title Page
JJ II
J I
Page 109 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Evaluation: 1
4 1
2−−
/
Home Page
Title Page
JJ II
J I
Page 110 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Evaluation: 2
2
/
3
Home Page
Title Page
JJ II
J I
Page 111 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Evaluation: 3
2
/
3
Home Page
Title Page
JJ II
J I
Page 112 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Evaluation: 4
1
But what we actually get during parsing is a tree that lookslike . . .
Home Page
Title Page
JJ II
J I
Page 113 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Abstract Syntax: 1. . . THIS! E
T
T F
/
nF
( )E
E T−
TF
Fn
n
n n
/
n−−
Home Page
Title Page
JJ II
J I
Page 114 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Abstract Syntax: 2We use attribute grammar rules to construct the abstractsyntax tree (AST)!.But in order to do that we first require two procedures fortree construction.
makeLeaf(literal ) : Creates a node with label literal and re-turns a pointer to it.
makeBinaryNode(opr, opd1, opd2): Creates a node with la-bel opr (with fields which point to opd1 and opd2) andreturns a pointer to the newly created node.
Now we may associate a synthesized attribute called ptrwith each terminal and nonterminal symbol which points tothe root of the subtree created for it.
Home Page
Title Page
JJ II
J I
Page 115 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Abstract Syntax: 3E0 → E1−T B E0.ptr := makeBinaryNode(−, E1.ptr, T.ptr)
E → T B E.ptr := T.ptr
T0 → T1/F B T0.ptr := makeBinaryNode(/, T1.ptr, F.ptr)
T → F B T.ptr := F.ptr
F → (E) B F.ptr := E.ptr
F → n B F.ptr := makeLeaf (n.val)
The Big Picture
Home Page
Title Page
JJ II
J I
Page 116 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Symbol Table:1• The store house of context-sensitive and run-time infor-
mation about every identifier in the source program.
• All accesses relating to an identifier require to first findthe attributes of the identifier from the symbol table
• Usually organized as a hash table – provides fast ac-cess.
• Compiler-generated temporaries may also be stored inthe symbol table
Home Page
Title Page
JJ II
J I
Page 117 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Symbol Table:2Attributes stored in a symbol table for each identifier:
• type
• size
• scope/visibility information
• base address
• addresses to location of auxiliary symbol tables (in caseof records, procedures, classes)
• address of the location containing the string which ac-tually names the identifier and its length in the stringpool
Home Page
Title Page
JJ II
J I
Page 118 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Symbol Table:3• A symbol table exists through out the compilation and
run-time.
• Major operations required of a symbol table:
– insertion
– search
– deletions are purely logical (depending on scopeand visibility) and not physical
• Keywords are often stored in the symbol table beforethe compilation process begins.
Home Page
Title Page
JJ II
J I
Page 119 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Symbol Table:4Accesses to the symbol table at every stage of the compi-lation process,
Scanning: Insertion of new identifiers.
Parsing: Access to the symbol table to ensure that anoperand exists (declaration before use).
Semantic analysis:• Determination of types of identifiers from declara-
tions
• type checking to ensure that operands are used intype-valid contexts.
• Checking scope, visibility violations.
Home Page
Title Page
JJ II
J I
Page 120 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Symbol Table:5IR generation: . Memory allocation and relativea address
calculation.
Optimization: All memory accesses through symbol table
Target code: Translation of relative addresses to absoluteaddresses in terms of word length, word boundary etc.
The Big pictureai.e.relativeto a base address that is known only at run-time
Home Page
Title Page
JJ II
J I
Page 121 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
IntermediateRepresentation
Intermediate representations are important for reasons ofportability.
• (more or less)independent of specific features of thehigh-level language.
Example. Java byte-code for any high-level language.
• (more or less)independent of specific features of anyparticular target architecture (e.g. number of registers,memory size)
– number of registers
– memory size
– word length
Home Page
Title Page
JJ II
J I
Page 122 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
IR Properties: 11. It is fairly low-level containing instructions common to
all target architectures and assembly languages.
How low can you stoop? . . .
2. It contains some fairly high-level instructions that arecommon to most high-level programming languages.
How high can you rise?
3. To ensure portability
• an unbounded number of variables and memory lo-cations
• no commitment to Representational Issues
4. To ensure type-safety
• memory locations are also typed according to thedata they may contain,
• no commitment is made regarding word boundaries,and the structure of individual data items.
Next
Home Page
Title Page
JJ II
J I
Page 123 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
IR: Representation?• No commitment to word boundaries or byte boundaries
• No commitment to representation of
– int vs. float,
– float vs. double,
– packed vs. unpacked,
– strings – where and how?.
Back to IR Properties:1
Home Page
Title Page
JJ II
J I
Page 124 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
IR: How low can youstoop?
• most arithmetic and logical operations, load and storeinstructions etc.
• so as to be interpreted easily,
• the interpreter is fairly small,
• execution speeds are high,
• to have fixed length instructions (where each operandposition has a specific meaning).
Back to IR Properties:1
Home Page
Title Page
JJ II
J I
Page 125 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
IR: How high can yourise?
• typed variables,
• temporary variables instead of registers.
• array-indexing,
• random access to record fields,
• parameter-passing,
• pointers and pointer management
• no limits on memory addresses
Back to IR Properties:1
Home Page
Title Page
JJ II
J I
Page 126 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
A typical instructionset: 1
Three address code: A suite of instructions. Each instruc-tion has at most 3 operands.
• an opcode representing an operation with at most 2operands
• two operands on which the binary operation is per-formed
• a target operand, which accumulates the result of the(binary) operation.
If an operation requires less than 3 operands then one ormore of the operands is made null.
Home Page
Title Page
JJ II
J I
Page 127 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
A typical instructionset: 2
• Assignments (LOAD-STORE)
• Jumps (conditional and unconditional)
• Procedures and parameters
• Arrays and array-indexing
• Pointer Referencing and Dereferencing
Home Page
Title Page
JJ II
J I
Page 128 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
A typical instructionset: 2
• Assignments (LOAD-STORE)
– x := y bop z , where bop is a binary operation
– x := uop y , where uop is a unary operation
– x := y , load, store, copy or register transfer
• Jumps (conditional and unconditional)
• Procedures and parameters
• Arrays and array-indexing
• Pointer Referencing and Dereferencing
Home Page
Title Page
JJ II
J I
Page 129 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
A typical instructionset: 2
• Assignments (LOAD-STORE)
• Jumps (conditional and unconditional)
– goto L – Unconditional jump,
– x relop y goto L – Conditional jump, whererelop is a relational operator
• Procedures and parameters
• Arrays and array-indexing
• Pointer Referencing and Dereferencing
Home Page
Title Page
JJ II
J I
Page 130 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
A typical instructionset: 2
• Assignments (LOAD-STORE)
• Jumps (conditional and unconditional)
• Procedures and parameters
– call p n , where n is the number of parameters
– return y , return value from a procedures call
– param x , parameter declaration
• Arrays and array-indexing
• Pointer Referencing and Dereferencing
Home Page
Title Page
JJ II
J I
Page 131 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
A typical instructionset: 2
• Assignments (LOAD-STORE)
• Jumps (conditional and unconditional)
• Procedures and parameters
• Arrays and array-indexing
– x := a[i] – array indexing for r-value
– a[j] := y – array indexing for l-value
Note: The two opcodes are different depending onwhether l-valueor r-value is desired. x and y are alwayssimple variables
• Pointer Referencing and Dereferencing
Home Page
Title Page
JJ II
J I
Page 132 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
A typical instructionset: 2
• Assignments (LOAD-STORE)
• Jumps (conditional and unconditional)
• Procedures and parameters
• Arrays and array-indexing
• Pointer Referencing and Dereferencing
– x := ˆy – referencing: set x to point to y
– x := *y – dereferencing: copy contents of locationpointed to by y into x
– *x := y – dereferencing: copy r-valueof y into thelocation pointed to by x
Picture
Home Page
Title Page
JJ II
J I
Page 133 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Pointersx
x
*y
y
@z
*x
*x @z
*zz
*zz
x := ^y
x := *y
*x := y
*y@y
@z
*z
*z
x y
yx
x y
z
x
yy
*y
*y*y
@zz
Home Page
Title Page
JJ II
J I
Page 134 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
IR: Generation• Can be generated by recursive traversal of the abstract
syntax tree.
• Can be generated by syntax-directed translation as fol-lows:
For every non-terminal symbol N in the grammar of thesource language there exist two attributes
N.place , which denotes the address of a temporaryvariable where the result of the execution of the gen-erated code is stored
N.code, which is the actual code segment generated.
• In addition a global counter for the instructions gener-ated is maintained as part of the generation process.
• It is independent of the source language but can ex-press target machine operations without committing totoo much detail.
Home Page
Title Page
JJ II
J I
Page 135 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
IR: InfrastructureGiven an abstract syntax tree T, with T also denoting itsroot node.
T.place address of temporary variable where result of exe-cution of the T is stored.
newtempreturns a freshvariable name and also installs it inthe symbol table along with relevant information
T.code the actual sequence of instructions generated forthe tree T.
newlabelreturns a label to mark an instruction in the gener-ated code which may be the target of a jump.
emit emits an instructions (regarded as a string).
Home Page
Title Page
JJ II
J I
Page 136 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
IR: InfrastructureColour and font coding of IR code generation
• Green: Nodes of the Abstract Syntax Tree
• Brown : Characters and strings of the IntermediateRepresentation
• Red: Variables and data structures of the languageinwhich the IR code generator is written
• blue: Names of relevant proceduresused in IR code gen-eration.
• Black: All other stuff.
Home Page
Title Page
JJ II
J I
Page 137 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
IR: Example
E → id B
E.place := id.place;E.code := emit()
E0 → E1 − E2 B
E0.place := newtemp;E0.code := E1.code
|| E2.code|| emit(E0.place := E1.place − E2.place)
Home Page
Title Page
JJ II
J I
Page 138 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
IR: Example
S → id := E B
S.code := E.code|| emit(id.place:=E.place)
S0 → while E do S1 B
S0.begin := newlabel;S0.after := newlabel;S0.code := emit(S0.begin:)
|| E.code|| emit(if E.place= 0 goto S0.after)|| S1.code|| emit(gotoS0.begin)|| emit(S0.after:)
Home Page
Title Page
JJ II
J I
Page 139 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
IR: Example
S → id := E B
S.code := E.code|| emit(id.place:=E.place)
S0 → while E do S1 B
S0.begin := newlabel;S0.after := newlabel;S0.code := emit(S0.begin:)
|| E.code|| emit(if E.place= 0 goto S0.after)|| S1.code|| emit(gotoS0.begin)|| emit(S0.after:)
Home Page
Title Page
JJ II
J I
Page 140 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
IR: GenerationWhile generating the intermediate representation, it issometimes necessary to generate jumps into code that hasnot been generated as yet (hence the address of the labelis unknown). This usually happens while processing
• forward jumps
• short-circuit evaluation of boolean expressions
It is usual in such circumstances to either fill up the emptylabel entries in a second pass over the the code or througha process of backpatching (which is the maintenance oflists of jumps to the same instruction number), wherein theblank entries are filled in once the sequence number of thetarget instruction becomes known.
Home Page
Title Page
JJ II
J I
Page 141 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
A Calling ChainMain program
Globals
Procedure P2Locals of P2
Procedure P21
Locals of P21
Body of P21
Call P21
Body of P2
Call P21
Locals of P1
Procedure P1
Body of P1
Call P2
Main body
Call P1
Home Page
Title Page
JJ II
J I
Page 142 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Run-time Structure: 1Main program
Globals
Main body
Procedure P2
Locals of P2
Procedure P21
Locals of P21
Body of P2
Procedure P1
Locals of P1
Body of P1
Globals
Body of P21
Home Page
Title Page
JJ II
J I
Page 143 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Run-time Structure: 2Main program
Globals
Main body
Procedure P2
Locals of P2
Procedure P21
Locals of P21
Body of P2
Procedure P1
Locals of P1
Body of P1
Globals
Formal par of P1
Locals of P1
Return address to MainDynamic link to Main
Static link to Main
Body of P21
Home Page
Title Page
JJ II
J I
Page 144 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Run-time Structure: 3Main program
Globals
Main body
Procedure P2
Locals of P2
Procedure P21
Locals of P21
Body of P2
Procedure P1
Locals of P1
Body of P1
Globals
Formal par of P1
Locals of P1
Return address to Main
Formal par P2
Locals of P2
Return address to last of P1
Dynamic link to Main
Dynamic link to last P1
Static link to Main
Static link to last P1
Body of P21
Home Page
Title Page
JJ II
J I
Page 145 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Run-time Structure: 4Main program
Globals
Main body
Procedure P2
Locals of P2
Procedure P21
Locals of P21
Body of P2
Procedure P1
Locals of P1
Body of P1
Globals
Formal par of P1
Locals of P1
Return address to Main
Formal par P2
Locals of P2
Return address to last of P1
Formal par P21
Locals of P21
Return address to last of P2
Dynamic link to Main
Dynamic link to last P1
Dynamic link to last P2
Static link to Main
Static link to last P1
Static link last P2
Body of P21
Home Page
Title Page
JJ II
J I
Page 146 of 100
Go Back
Full Screen
Close
Quit
•First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
Run-time Structure: 5Main program
Globals
Main body
Procedure P2
Locals of P2
Procedure P21
Locals of P21
Body of P2
Procedure P1
Locals of P1
Body of P1
Globals
Formal par of P1
Locals of P1
Return address to Main
Formal par P2
Locals of P2
Return address to last of P1
Formal par P21
Locals of P21
Return address to last of P2
Formal par P21
Locals of P21
Return address to last of P21
Dynamic link to Main
Dynamic link to last P1
Dynamic link to last P2
Dynamic link to last P21
Static link to Main
Static link to last P1
Static link last P2
Static link to last P2
Body of P21