You are on page 1of 147

Home Page

Title Page

CS432F/CSL 728: JJ II

Compiler Design J I

July 2004
Page 1 of 100

S. Arun-Kumar Go Back

sak@cse.iitd.ernet.in
Department of Computer Science and Engineering Full Screen

I. I. T. Delhi, Hauz Khas, New Delhi 110 016.


Close

July 30, 2004


Quit

First Prev Next Last Go Back Full Screen Close Quit


Contents Home Page

first The Bigpicture last Title Page

first Lexical Analysis last JJ II

first Syntax Analysis last


J I
first Context-free Grammars last
first Ambiguity last
Page 2 of 100
first Shift-Reduce Parsing last
first Parse Trees last Go Back

first Semantic Analysis last


Full Screen
first Synthesized Attributes last
first Inherited Attributes last Close

Contd . . .
Quit

First Prev Next Last Go Back Full Screen Close Quit


Home Page

Contents Title Page

. . . Contd JJ II

first Abstract Syntax Trees last


J I
first Symbol Tables last
first Intermediate Representation last Page 3 of 100

IR: Properties
Go Back
Typical Instruction Set
IR: Generation
Full Screen

first Runtime Structure last


Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


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


The Big Picture: 1 Home Page

stream of Title Page


characters
SCANNER
stream of JJ II
tokens

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: 2 Home Page

stream of Title Page


characters
SCANNER
stream of JJ II
tokens
PARSER
parse tree 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: 3 Home Page

stream of Title Page


characters
SCANNER
stream of JJ II
tokens
PARSER
parse tree J I
SEMANTIC ANALYZER
abstract
syntax tree Page 7 of 100

Go Back

Full Screen

Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


The Big Picture: 4 Home Page

stream of Title Page


characters
SCANNER
stream of JJ II
tokens
PARSER
parse tree J I
SEMANTIC ANALYZER
abstract
syntax tree Page 8 of 100

I.R. CODE GENERATOR


intermediate Go Back
representation

Full Screen

Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


The Big Picture: 5 Home Page

stream of Title Page


characters
SCANNER
stream of JJ II
tokens
PARSER
parse tree J I
SEMANTIC ANALYZER
abstract
syntax tree Page 9 of 100

I.R. CODE GENERATOR


intermediate Go Back
representation
OPTIMIZER

optimized Full Screen


intermediate
representation
Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


The Big Picture: 6 Home Page

stream of Title Page


characters
SCANNER
stream of JJ II
tokens
PARSER
parse tree J I
SEMANTIC ANALYZER
abstract
syntax tree Page 10 of 100

I.R. CODE GENERATOR


intermediate Go Back
representation
OPTIMIZER

optimized Full Screen


intermediate
representation

CODE GENERATOR Close

target code

Quit

First Prev Next Last Go Back Full Screen Close Quit


The Big Picture: 7 Home Page

stream of Title Page


characters
SCANNER
stream of JJ II
tokens
PARSER
parse tree J I
ERROR SEMANTIC ANALYZER SYMBOL
abstract
HANDLER TABLE Page 11 of 100
syntax tree
MANAGER
I.R. CODE GENERATOR
intermediate Go Back
representation
OPTIMIZER

optimized Full Screen


intermediate
representation

CODE GENERATOR Close

target code

Quit

First Prev Next Last Go Back Full Screen Close Quit


The Big Picture: 7
Home Page

Title Page

stream of
characters
SCANNER JJ II
stream of
tokens
PARSER
parse tree J I
ERROR SEMANTIC ANALYZER SYMBOL
abstract
HANDLER TABLE
syntax tree
MANAGER
I.R. CODE GENERATOR Page 12 of 100
intermediate
representation
OPTIMIZER

optimized Go Back
intermediate
representation

CODE GENERATOR

target code Full Screen

Scanner Parser Semantic Analysis Symbol Table


Close
IR Optimization Contents

Quit

First Prev Next Last Go Back Full Screen Close Quit


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

Scanning: 1 Title Page

JJ II

Takes a stream of characters and identifies tokens from


the lexemes. J I

Page 14 of 100

Eliminates comments and redundant whitepace.


Go Back

Keeps track of line numbers and column numbers and Full Screen
passes them as parameters to the other phases to en-
able error-reporting to the user.
Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


Home Page

Scanning: 2 Title Page

Whitespace: A sequence of space, tab, newline, JJ II


carriage-return, form-feed characters etc.
Lexeme: A sequence of non-whitespace characters de- J I

limited by whitespace or special characters (e.g. oper-


ators like +, -, *). Page 15 of 100

Examples of lexemes.
Go Back

reserved words, keywords, identifiers etc.


Each comment is usually a single lexeme Full Screen

preprocessor directives
Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


Home Page

Scanning: 3 Title Page

Token: A sequence of characters to be treated as a


single unit. JJ II

Examples of tokens.
J I
Reserved words (e.g. begin, end, struct, if etc.)
Keywords (integer, true etc.) Page 16 of 100

Operators (+, &&, ++ etc)


Identifiers (variable names, procedure names, pa- Go Back

rameter names)
Literal constants (numeric, string, character con- Full Screen

stants etc.)
Punctuation marks (:, , etc.) Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


Home Page

Scanning: 4
Title Page

JJ II
Identification of tokens is usually done by a Determinis-
tic Finite-state automaton (DFA). J I
The set of tokens of a language is represented by a
large regular expression. Page 17 of 100

This regular expression is fed to a lexical-analyser gen-


erator such as Lex, Flex or ML-Lex. Go Back

A giant DFA is created by the Lexical analyser genera-


Full Screen
tor.
Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


Scanning: 5
Home Page

Title Page
ae,gz
identifier if identifier
f JJ II
09,az 09,az
2 3 4 09
J I
i z
j
09
h,

5 6
a

real Page 18 of 100


09 09
1
09
white Go Back
chars

space 7 8 real
( int
14
other

any char Full Screen

* 10 * 11 )
13 9 12 Close

error error comment

Quit
The Big Picture
First Prev Next Last Go Back Full Screen Close Quit
Syntax Analysis Home Page

Consider the following two languages over an alphabet


A = {a, b}. Title Page

R = {anbn|n < 100} JJ II


P = {anbn|n > 0}
R may be finitely represented by a regular expression J I

(even though the actual expression is very long).


Page 19 of 100
However, P cannot actually be represented by a regular
expression
Go Back

A regular expression is not powerful enough to repre-


sent languages which require parenthesis matching to Full Screen
arbitrary depths.
All high level programming languages require an under- Close

lying language of expressions which require parenthe-


ses to be nested and matched to arbitrary depth. Quit

First Prev Next Last Go Back Full Screen Close Quit


CF-Grammars: Home Page

Definition Title Page

A context-free grammar (CFG) G = hN, T , P, Si consists JJ II


of
a set N of nonterminal symbols, J I

a set T of terminal symbols or the alphabet,


Page 20 of 100
a set P of productions or rewrite rules,
each production is of the form X , where Go Back

X N is a nonterminal and
(N T ) is a string of terminals and nontermi-
Full Screen

nals
Close
a start symbol S N .
Quit

First Prev Next Last Go Back Full Screen Close Quit


CFG: Example Home Page

G = h{S}, {a, b}, P, Si, where S ab and S aSb Title Page

are the only productions in P .


Derivations look like this: JJ II

J I

S ab
Page 21 of 100

S aSb aabb
S aSb aaSbb aaabbb Go Back

L(G), the language generated by G is {anbn|n > 0}.


Full Screen

Actually can be proved by induction on the length and struc- Close

ture of derivations.
Quit

First Prev Next Last Go Back Full Screen Close Quit


CFG: Empty word Home Page

G = h{S}, {a, b}, P, Si, where S SS | aSb |


Title Page
generates all sequences of matching nested parentheses,
including the empty word .
JJ II

A leftmost derivation might look like this:


J I

S SS SSS SS aSbS abS abaSb . . .


Page 22 of 100

A rightmost derivation might look like this:


Go Back

S SS SSS SS SaSb Sab aSbab . . .


Other derivations might look like God alone knows what! Full Screen

S SS SSS SS . . . Close

Could be quite confusing!


Quit

First Prev Next Last Go Back Full Screen Close Quit


Home Page

CFG: Derivation trees Title Page

1 JJ II
Derivation sequences
put an artificial order in which productions are fired. J I

instead look at trees of derivations in which we may think


of productions as being fired in parallel. Page 23 of 100

There is then no highlighting in red to determine which


Go Back
copy of a nonterminal was used to get the next member
of the sequence.
Full Screen

Of course, generation of the empty word must be


shown explicitly in the tree. Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


CFG: Derivation trees
2 Home Page

Title Page

S
JJ II

S S
J I

S S Page 24 of 100

Go Back
a S b a S b

Full Screen
Derivation tree of
a S b
abaabb Close


Quit

First Prev Next Last Go Back Full Screen Close Quit


CFG: Derivation trees
3 Home Page

Title Page

S JJ II

S S
J I

S S a S b Page 25 of 100

Go Back
a S b a S b

Full Screen


Another
Close
Derivation tree of
abaabb
Quit

First Prev Next Last Go Back Full Screen Close Quit


CFG: Derivation trees
4 Home Page

Title Page

S
JJ II

S S
J I

S S a S b Page 26 of 100

a b
Go Back
a S b S

Full Screen


Yet another
Close
Derivation tree of
abaabb
Quit

First Prev Next Last Go Back Full Screen Close Quit


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


Ambiguity: 1
Home Page

E I | C | E+E | EE
I y | z Title Page
C 4
Consider the sentence y + 4 z. JJ II

J I

E E

Page 28 of 100

Go Back

Full Screen

Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


Ambiguity: 2
Home Page

E I | C | E+E | EE
I y | z Title Page
C 4
Consider the sentence y + 4 z. JJ II

J I

E E

Page 29 of 100

E + E E * E

Go Back

Full Screen

Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


Ambiguity: 3
Home Page

E I | C | E+E | EE
I y | z Title Page
C 4
Consider the sentence y + 4 z. JJ II

J I

E E

Page 30 of 100

E + E E * E

Go Back
* E E + E
I E I
Full Screen

Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


Ambiguity: 4
Home Page

E I | C | E+E | EE
I y | z Title Page
C 4
Consider the sentence y + 4 z. JJ II

J I

E E

Page 31 of 100

E + E E * E

Go Back
* E E + E
I E I
Full Screen

I I C
y C z
Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


Ambiguity: 5
Home Page

E I | C | E+E | EE
I y | z Title Page
C 4
Consider the sentence y + 4 z. JJ II

J I

E E

Page 32 of 100

E + E E * E

Go Back
* E E + E
I E I
Full Screen

I I C
y C z
Close

z y 4
Quit
4

First Prev Next Last Go Back Full Screen Close Quit


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


Parsing: 0
Home Page

r1. E E T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I

a a / b
Page 34 of 100

Go Back

Full Screen

Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


Parsing: 1
Home Page

r1. E E T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I

a / b
Page 35 of 100

Principle: Go Back
Reduce
whenever possible.
Full Screen
Shift only when
reduce is
impossible Close

a
Shift Quit

First Prev Next Last Go Back Full Screen Close Quit


Parsing: 2
Home Page

r1. E E T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I

a / b
Page 36 of 100

Go Back

Full Screen

Close

D Reduce by r5 Quit

First Prev Next Last Go Back Full Screen Close Quit


Parsing: 3
Home Page

r1. E E T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I

a / b
Page 37 of 100

Go Back

Full Screen

Close

T Reduce by r4 Quit

First Prev Next Last Go Back Full Screen Close Quit


Parsing: 4
Home Page

r1. E E T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I

a / b
Page 38 of 100

Go Back

Full Screen

Close

E Reduce by r2 Quit

First Prev Next Last Go Back Full Screen Close Quit


Parsing: 5
Home Page

r1. E E T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I

a / b
Page 39 of 100

Go Back

Full Screen

Close

Shift
E
Quit

First Prev Next Last Go Back Full Screen Close Quit


Parsing: 6
Home Page

r1. E E T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I

/ b
Page 40 of 100

Go Back

Full Screen

a
Shift Close


E
Quit

First Prev Next Last Go Back Full Screen Close Quit


Parsing: 7
Home Page

r1. E E T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I

/ b
Page 41 of 100

Go Back

Full Screen

D Reduce by r5 Close


E
Quit

First Prev Next Last Go Back Full Screen Close Quit


Parsing: 8
Home Page

r1. E E T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I

/ b
Page 42 of 100

Go Back

Full Screen

T Reduce by r4 Close


E
Quit

First Prev Next Last Go Back Full Screen Close Quit


Parsing: 8a
Home Page

r1. E E T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I

/ b
Page 43 of 100

Go Back

Full Screen

T Reduce by r4 Close

E
Quit

First Prev Next Last Go Back Full Screen Close Quit


Parsing: 9a
Home Page

r1. E E T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I

/ b
Page 44 of 100

Go Back

Full Screen

Close

E Reduce by r1 Quit

First Prev Next Last Go Back Full Screen Close Quit


Parsing: 10a
Home Page

r1. E E T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I

b
Page 45 of 100

Go Back

Full Screen

Close
/
Shift
E
Quit

First Prev Next Last Go Back Full Screen Close Quit


Parsing: 11a
Home Page

r1. E E T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I

Page 46 of 100

Go Back

Full Screen

b Shift Close
/

E
Quit

First Prev Next Last Go Back Full Screen Close Quit


Parsing: 12a
Home Page

r1. E E T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I

Page 47 of 100

Go Back

Full Screen

D Reduce by r5 Close
/

E
Quit

First Prev Next Last Go Back Full Screen Close Quit


Parsing: 13a
Home Page

r1. E E T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I

Page 48 of 100

Go Back

Full Screen

T
Reduce by r4 Close
/

E
Quit

First Prev Next Last Go Back Full Screen Close Quit


Parsing: 14a
Home Page

r1. E E T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I

Page 49 of 100

Go Back
k!

Full Screen
uc
St

E Reduce by r2 Close
/
Get back!
E
Quit

First Prev Next Last Go Back Full Screen Close Quit


Parsing: 14b
Home Page

r1. E E T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I

Page 50 of 100

Go Back

Full Screen

E Reduce by r2 Close
/
Get back!
E
Quit

First Prev Next Last Go Back Full Screen Close Quit


Parsing: 13b
Home Page

r1. E E T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I

Page 51 of 100

Go Back

Full Screen

T
Reduce by r4 Close

Get back! /

E
Quit

First Prev Next Last Go Back Full Screen Close Quit


Parsing: 12b
Home Page

r1. E E T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I

Page 52 of 100

Go Back

Full Screen

D Reduce by r5 Close

Get back! /

E
Quit

First Prev Next Last Go Back Full Screen Close Quit


Parsing: 11b
Home Page

r1. E E T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I

Page 53 of 100

Go Back

Full Screen

Get back! b Shift Close


/

E
Quit

First Prev Next Last Go Back Full Screen Close Quit


Parsing: 10b
Home Page

r1. E E T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I

b
Page 54 of 100

Go Back

Full Screen

Close
/
Get back! Shift
E
Quit

First Prev Next Last Go Back Full Screen Close Quit


Parsing: 9b
Home Page

r1. E E T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I

/ b
Page 55 of 100

Go Back

Full Screen

Get back to
where you Close

once belonged!
E Reduce by r1 Quit

First Prev Next Last Go Back Full Screen Close Quit


Parsing: 8b
fied
Home Page

o d i
m Principle:
r1. E E T
Title Page
r2 E T Reduce whenever possible, but
r3 T T / D but depending upon
JJ II
r4 T D
lookahead
r5 D a | b | ( E )
J I

/ b
Page 56 of 100
Shift instead
of reduce here!
Go Back

ce
i f t redu
Sh ict Full Screen
confl
T Reduce by r4 Close

E
Quit

First Prev Next Last Go Back Full Screen Close Quit


Parsing: 8
Home Page

r1. E E T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I

/ b
Page 57 of 100

Go Back

Full Screen

T Reduce by r4 Close


E
Quit

First Prev Next Last Go Back Full Screen Close Quit


Parsing: 9
Home Page

r1. E E T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I

b
Page 58 of 100

Go Back

Full Screen
/ Shift
T Close


E
Quit

First Prev Next Last Go Back Full Screen Close Quit


Parsing: 10
Home Page

r1. E E T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I

Page 59 of 100

Go Back

b
Shift Full Screen
/

T Close


E
Quit

First Prev Next Last Go Back Full Screen Close Quit


Parsing: 11
Home Page

r1. E E T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I

Page 60 of 100

Go Back

D Reduce by r5
Full Screen
/
T
Close


E
Quit

First Prev Next Last Go Back Full Screen Close Quit


Parsing: 12
Home Page

r1. E E T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I

Page 61 of 100

Go Back

Full Screen

T Reduce by r3 Close

E
Quit

First Prev Next Last Go Back Full Screen Close Quit


Parsing: 13
Home Page

r1. E E T
Title Page
r2 E T
r3 T T / D
JJ II
r4 T D
r5 D a | b | ( E )
J I

Page 62 of 100

Go Back

Full Screen

Close

E Reduce by r1 Quit

First Prev Next Last Go Back Full Screen Close Quit


Parse Trees: 0 Home Page

Title Page
r1. E E T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
JJ II

J I

Page 63 of 100

Go Back

Full Screen

Close

a a / b

Quit

First Prev Next Last Go Back Full Screen Close Quit


Parse Trees: 1 Home Page

Title Page
r1. E E T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
JJ II

J I

Page 64 of 100

Go Back

Full Screen

Close

a a / b

Quit

First Prev Next Last Go Back Full Screen Close Quit


Parse Trees: 2 Home Page

Title Page
r1. E E T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
JJ II

J I

Page 65 of 100

Go Back

Full Screen

Close

a a / b

Quit

First Prev Next Last Go Back Full Screen Close Quit


Parse Trees: 3 Home Page

Title Page
r1. E E T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
JJ II

J I

Page 66 of 100

Go Back

Full Screen

Close

a a / b

Quit

First Prev Next Last Go Back Full Screen Close Quit


Parse Trees: 3a Home Page

Title Page
r1. E E T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
JJ II

J I

Page 67 of 100

Go Back

Full Screen

Close

a a / b

Quit

First Prev Next Last Go Back Full Screen Close Quit


Parse Trees: 3b Home Page

Title Page
r1. E E T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
JJ II

J I

Page 68 of 100

Go Back

Full Screen

Close

a a / b

Quit

First Prev Next Last Go Back Full Screen Close Quit


Parse Trees: 4 Home Page

Title Page
r1. E E T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
JJ II

J I

Page 69 of 100

Go Back

Full Screen

D D

Close

a a / b

Quit

First Prev Next Last Go Back Full Screen Close Quit


Parse Trees: 5 Home Page

Title Page
r1. E E T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
JJ II

J I

Page 70 of 100

Go Back

T T

Full Screen

D D

Close

a a / b

Quit

First Prev Next Last Go Back Full Screen Close Quit


Parse Trees: 5a Home Page

Title Page
r1. E E T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
JJ II

J I

Page 71 of 100

Go Back

T T

Full Screen

D D

Close

a a / b

Quit

First Prev Next Last Go Back Full Screen Close Quit


Parse Trees: 5b Home Page

Title Page
r1. E E T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
JJ II

J I

Page 72 of 100

Go Back

T T

Full Screen

D D

Close

a a / b

Quit

First Prev Next Last Go Back Full Screen Close Quit


Parse Trees: 6 Home Page

Title Page
r1. E E T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
JJ II

J I

Page 73 of 100

Go Back

T T

Full Screen

D D D

Close

a a / b

Quit

First Prev Next Last Go Back Full Screen Close Quit


Parse Trees: 7 Home Page

Title Page
r1. E E T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
JJ II

J I

Page 74 of 100

E
T

Go Back

T T

Full Screen

D D D

Close

a a / b

Quit

First Prev Next Last Go Back Full Screen Close Quit


Parse Trees: 8 Home Page

Title Page
r1. E E T
r4 T D
r2 E T
r3 T T / D r5 D a | b | ( E )
JJ II

J I
E

Page 75 of 100

E
T

Go Back

T T

Full Screen

D D D

Close

a a / b

Quit

First Prev Next Last Go Back Full Screen Close Quit


Home Page

Parsing: Summary: 1 Title Page

All high-level languages are designed so that they may JJ II


be parsed in this fashion with only a single token look-
ahead. J I

Parsers for a language can be automatically con-


structed by parger-generators such as Yacc, Bison, ML- Page 76 of 100

Yacc.
Shift-reduce conflicts if any, are automatically detected Go Back

and reported by the parser-generator.


Full Screen
Shift-reduce conflicts may be avoided by suitably
redesigning the context-free grammar.
Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


Home Page

Parsing: Summary: 2 Title Page

Very often shift-reduce conflicts may occur because


JJ II
of the prefix problem. In such cases many parser-
generators resolve the conflict in favour of shifting.
J I
There is also a possiblility of reduce-reduce conflicts.
This usually happens when there is more than one non-
Page 77 of 100
terminal symbol to which the contents of the stack may
reduce.
Go Back

A minor reworking of the grammar to avoid redundant


non-terminal symbols will get rid of reduce-reduce con- Full Screen
flicts.
The Big Picture Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


Semantic Analysis: 1 Home Page

Every Programming langauge can be used to program Title Page


any computable function, assuming of course, it has
unbounded memory, and JJ II

unbounded time
J I
The parser of a programming language provides the
framework within which the target code is to be gener-
ated. Page 78 of 100

The parser also provides a structuring mechanism that Go Back


divides the task of code generation into bits and pieces
determined by the individual nonterminals and produc-
Full Screen
tion rules.
However, contex-free grammars are not powerful Close

enough to represent all computable functions. Exam-


ple, the language {an bn cn |n > 0}. Quit

First Prev Next Last Go Back Full Screen Close Quit


Home Page

Semantic Analysis: 2
Title Page

JJ II

There are context-sensitive aspects of a program that


cannot be represented/enforced by a context-free gram- J I
mar definition. Examples include
correspondence between formal and actual param- Page 79 of 100

eters
type consistency between declaration and use. Go Back

scope and visibility issues with respect to identifiers


Full Screen
in a program.

Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


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


Synthesized Attributes:
0 Home Page

Title Page

E T F E
JJ II
/ ( ) n

T

J I
T F
/

Page 81 of 100
n
F

Go Back
( )
E

E T Full Screen

T
F
Close

F
n
Quit

First Prev Next Last Go Back Full Screen Close Quit


Synthesized Attributes:
1 Home Page

Title Page

E T F E
JJ II
/ ( ) n

T

J I
T F
/

Page 82 of 100
n
F

Go Back
( )
E

E T Full Screen
Synthesized Attributes

4 3 2 1
T
F
Close

F
n
Quit

4 n

First Prev Next Last Go Back Full Screen Close Quit


Synthesized Attributes:
2 Home Page

Title Page

E T F E
JJ II
/ ( ) n

T

J I
T F
/

Page 83 of 100
n
F

Go Back
( )
E

E T Full Screen
Synthesized Attributes

4 3 2 1
T
F
Close

4 F
n
Quit

4 n

First Prev Next Last Go Back Full Screen Close Quit


Synthesized Attributes:
3 Home Page

Title Page

E T F E
JJ II
/ ( ) n

T

J I
T F
/

Page 84 of 100
n
F

Go Back
( )
E

E T Full Screen
Synthesized Attributes

4 3 2 1
4 T
F
Close

4 F
n
Quit

4 n

First Prev Next Last Go Back Full Screen Close Quit


Synthesized Attributes:
4 Home Page

Title Page

E T F E
JJ II
/ ( ) n

T

J I
T F
/

Page 85 of 100
n
F

Go Back
( )
E

4 E T Full Screen
Synthesized Attributes

4 3 2 1
4 T
F
Close

4 F
n
Quit

4 n

First Prev Next Last Go Back Full Screen Close Quit


Synthesized Attributes:
5 Home Page

Title Page

E T F E
JJ II
/ ( ) n

T

J I
T F
/

Page 86 of 100
n
F

Go Back
( )
E

4 E T Full Screen
Synthesized Attributes

4 3 2 1
4 T
F
Close

4 F
n 1
Quit

4 n

First Prev Next Last Go Back Full Screen Close Quit


Synthesized Attributes:
6 Home Page

Title Page

E T F E
JJ II
/ ( ) n

T

J I
T F
/

Page 87 of 100
n
F

Go Back
( )
E

4 E T Full Screen
Synthesized Attributes

4 3 2 1
4 T
F 1
Close

4 F
n 1
Quit

4 n

First Prev Next Last Go Back Full Screen Close Quit


Synthesized Attributes:
7 Home Page

Title Page

E T F E
JJ II
/ ( ) n

T

J I
T F
/

Page 88 of 100
n
F

Go Back
( )
E

4 E T Full Screen
1
Synthesized Attributes

4 3 2 1
4 T
F 1
Close

4 F
n 1
Quit

4 n

First Prev Next Last Go Back Full Screen Close Quit


Synthesized Attributes:
8 Home Page

Title Page

E T F E
JJ II
/ ( ) n

T

J I
T F
/

Page 89 of 100
n
F

Go Back
( )
3
E

4 E T Full Screen
1
Synthesized Attributes

4 3 2 1
4 T
F 1
Close

4 F
n 1
Quit

4 n

First Prev Next Last Go Back Full Screen Close Quit


Synthesized Attributes:
9 Home Page

Title Page

E T F E
JJ II
/ ( ) n

T

J I
T F
/

Page 90 of 100
n
3
F

Go Back
( )
3
E

4 E T Full Screen
1
Synthesized Attributes

4 3 2 1
4 T
F 1
Close

4 F
n 1
Quit

4 n

First Prev Next Last Go Back Full Screen Close Quit


Synthesized Attributes:
10 Home Page

Title Page

E T F E
JJ II
/ ( ) n

T

J I
T 3 F
/

Page 91 of 100
n
3
F

Go Back
( )
3
E

4 E T Full Screen
1
Synthesized Attributes

4 3 2 1
4 T
F 1
Close

4 F
n 1
Quit

4 n

First Prev Next Last Go Back Full Screen Close Quit


Synthesized Attributes:
11 Home Page

Title Page

E T F E
JJ II
/ ( ) n

T

J I
T 3 F
/

Page 92 of 100
n 2
3
F

Go Back
( )
3
E

4 E T Full Screen
1
Synthesized Attributes

4 3 2 1
4 T
F 1
Close

4 F
n 1
Quit

4 n

First Prev Next Last Go Back Full Screen Close Quit


Synthesized Attributes:
12 Home Page

Title Page

E T F E
JJ II
/ ( ) n

T

J I
T 3 F 2
/

Page 93 of 100
n 2
3
F

Go Back
( )
3
E

4 E T Full Screen
1
Synthesized Attributes

4 3 2 1
4 T
F 1
Close

4 F
n 1
Quit

4 n

First Prev Next Last Go Back Full Screen Close Quit


Synthesized Attributes:
13 Home Page

Title Page

E T F E
JJ II
/ ( ) n

T 1

J I
T 3 F 2
/

Page 94 of 100
n 2
3
F

Go Back
( )
3
E

4 E T Full Screen
1
Synthesized Attributes

4 3 2 1
4 T
F 1
Close

4 F
n 1
Quit

4 n

First Prev Next Last Go Back Full Screen Close Quit


Synthesized Attributes:
14 Home Page

Title Page

E T F E 1
JJ II
/ ( ) n

T 1

J I
T 3 F 2
/

Page 95 of 100
n 2
3
F

Go Back
( )
3
E

4 E T Full Screen
1
Synthesized Attributes

4 3 2 1
4 T
F 1
Close

4 F
n 1
Quit

4 n

First Prev Next Last Go Back Full Screen Close Quit


An Attribute Grammar Home Page

Title Page

E 1
E0 E1T B E0.val := sub(E1.val, T.val)
JJ II
T 1

E T B E.val := T.val
T 3 F 2
/ J I

F
3
n 2
T0 T1/F B T0.val := div(T1.val, F.val)
Page 96 of 100
( )
3
E

4 E T 1
T F B T.val := F.val
Go Back

4 T
F 1

F (E) B F.val := E.val


4 F Full Screen
n 1

4 n
F n B F.val := n.val Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


Inherited Attributes: 0
C -style declarations generating int x, y, z. Home Page

D TL T int | float
L L,I | I I x | y | z Title Page

D
JJ II

T L
J I

I
,
int L Page 97 of 100
z

I
Go Back
L ,

y Full Screen

I
D L T I
Close

x y z int x
Quit

First Prev Next Last Go Back Full Screen Close Quit


Inherited Attributes: 1
C -style declarations generating int x, y, z. Home Page

D TL T int | float
L L,I | I I x | y | z Title Page

D
JJ II

T L
J I

I
,
int int L Page 98 of 100
z

I
Go Back
L ,

y Full Screen

I
D L T I
Close

x y z int x
Quit

, int

First Prev Next Last Go Back Full Screen Close Quit


Inherited Attributes: 2
C -style declarations generating int x, y, z. Home Page

D TL T int | float
L L,I | I I x | y | z Title Page

D
JJ II

int T L
J I

I
,
int int L Page 99 of 100
z

I
Go Back
L ,

y Full Screen

I
D L T I
Close

x y z int x
Quit

, int int

First Prev Next Last Go Back Full Screen Close Quit


Inherited Attributes: 3
C -style declarations generating int x, y, z. Home Page

D TL T int | float
L L,I | I I x | y | z Title Page

D
JJ II

int T L int
J I

I
,
int int L Page 100 of 100
z

I
Go Back
L ,

y Full Screen

I
D L T I
Close

x y z int x
Quit

, int int

First Prev Next Last Go Back Full Screen Close Quit


Inherited Attributes: 4
C -style declarations generating int x, y, z. Home Page

D TL T int | float
L L,I | I I x | y | z Title Page

D
JJ II

int T L int
J I

I int
,
int int L int Page 101 of 100
z

I
Go Back
L ,

y Full Screen

I
D L T I
Close

x y z int x
Quit

, int int

First Prev Next Last Go Back Full Screen Close Quit


Inherited Attributes: 5 Home Page

D
Title Page

int T L int
JJ II
I int
,
int int L int
J I
z int
I int
L int , Page 102 of 100

y
Go Back
I
D L T I
Full Screen

x y z int x

Close

, int int

Quit

First Prev Next Last Go Back Full Screen Close Quit


Inherited Attributes: 6
C -style declarations generating int x, y, z. Home Page

D TL T int | float
L L,I | I I x | y | z Title Page

D
JJ II

int T L int
J I

I int
,
int int L int Page 103 of 100
z int
I int
Go Back
L int ,

y int Full Screen

I int

D L T I
Close

x y z int x
Quit

, int int

First Prev Next Last Go Back Full Screen Close Quit


Inherited Attributes: 7
C -style declarations generating int x, y, z. Home Page

D TL T int | float
L L,I | I I x | y | z Title Page

D
JJ II

int T L int
J I

I int
,
int int L int Page 104 of 100
z int
I int
Go Back
L int ,

y int Full Screen

I int

D L T I
Close

x y z int x int
Quit

, int int

First Prev Next Last Go Back Full Screen Close Quit


Home Page

An Attribute Grammar Title Page

D
D TL B L.in := T.type JJ II
int T L int

,
I int T int B T.type := int.int
int int L int J I
z int

L int ,
I int
T float B T.type := float.f loat
Page 105 of 100

y int
I int L0 L1,I B L1 := L0.in
Go Back

x int L I B I.in := L.in


Full Screen

I id B id.type := I.in
Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


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


Abstract Syntax: 0
Home Page

E ET | T
T T /F | F Title Page
F n | (E)
Suppose we want to evaluate an expression (4 1)/2. JJ II

What we actually want is a tree that looks like this:


J I

Page 107 of 100

/
Go Back

2 Full Screen

Close

4 1
Quit

First Prev Next Last Go Back Full Screen Close Quit


Evaluation: 0 Home Page

Title Page

JJ II

/
J I

Page 108 of 100

2
Go Back

Full Screen

4 1
Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


Evaluation: 1 Home Page

Title Page

JJ II

/
J I

Page 109 of 100

2
Go Back

Full Screen

4 1
Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


Evaluation: 2 Home Page

Title Page

JJ II

/
J I

Page 110 of 100

3 2
Go Back

Full Screen

Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


Evaluation: 3 Home Page

Title Page

JJ II

/
J I

Page 111 of 100

3 2
Go Back

Full Screen

Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


Evaluation: 4
Home Page

Title Page

1 JJ II

J I

Page 112 of 100

Go Back

Full Screen

Close

But what we actually get during parsing is a tree that looks Quit

like . . .
First Prev Next Last Go Back Full Screen Close Quit
Abstract Syntax: 1
. . . THIS! E
Home Page

Title Page
T

T F JJ II
/

J I
n
F

Page 113 of 100


( )
E

/
Go Back
E T

n Full Screen
T
F

n n Close

F
n

Quit

First Prev Next Last Go Back Full Screen Close Quit


Abstract Syntax: 2
Home Page

Title Page
We use attribute grammar rules to construct the abstract
syntax tree (AST)!. JJ II
But in order to do that we first require two procedures for
tree construction.
J I
makeLeaf(literal) : Creates a node with label literal and re-
turns a pointer to it. Page 114 of 100

makeBinaryNode(opr, opd1, opd2) : Creates a node with la-


bel opr (with fields which point to opd1 and opd2) and Go Back

returns a pointer to the newly created node.


Full Screen
Now we may associate a synthesized attribute called ptr
with each terminal and nonterminal symbol which points to
Close
the root of the subtree created for it.

Quit

First Prev Next Last Go Back Full Screen Close Quit


Abstract Syntax: 3 Home Page

Title Page

E0 E1T B E0.ptr := makeBinaryN ode(, E1.ptr, T.ptr)


JJ II
E T B E.ptr := T.ptr
J I
T0 T1/F B T0.ptr := makeBinaryN ode(/, T1.ptr, F.ptr)
Page 115 of 100

T F B T.ptr := F.ptr
Go Back

F (E) B F.ptr := E.ptr


Full Screen

F n B F.ptr := makeLeaf (n.val)


Close

The Big Picture


Quit

First Prev Next Last Go Back Full Screen Close Quit


Home Page

Symbol Table:1
Title Page

JJ II
The store house of context-sensitive and run-time infor-
mation about every identifier in the source program.
J I
All accesses relating to an identifier require to first find
the attributes of the identifier from the symbol table Page 116 of 100

Usually organized as a hash table provides fast ac-


cess. Go Back

Compiler-generated temporaries may also be stored in


Full Screen
the symbol table

Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


Home Page

Symbol Table:2 Title Page

Attributes stored in a symbol table for each identifier:


JJ II
type
size J I

scope/visibility information
Page 117 of 100
base address
addresses to location of auxiliary symbol tables (in case Go Back
of records, procedures, classes)
address of the location containing the string which ac- Full Screen

tually names the identifier and its length in the string


pool Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


Home Page

Symbol Table:3 Title Page

A symbol table exists through out the compilation and JJ II


run-time.
Major operations required of a symbol table: J I

insertion
Page 118 of 100
search
deletions are purely logical (depending on scope Go Back

and visibility) and not physical


Keywords are often stored in the symbol table before Full Screen

the compilation process begins.


Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


Home Page

Symbol Table:4 Title Page

Accesses to the symbol table at every stage of the compi-


lation process, JJ II

Scanning: Insertion of new identifiers.


J I
Parsing: Access to the symbol table to ensure that an
operand exists (declaration before use).
Page 119 of 100

Semantic analysis:
Determination of types of identifiers from declara- Go Back

tions
type checking to ensure that operands are used in Full Screen

type-valid contexts.
Checking scope, visibility violations. Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


Home Page

Title Page

Symbol Table:5 JJ II
IR generation: . Memory allocation and relativea address
calculation. J I

Optimization: All memory accesses through symbol table


Page 120 of 100
Target code: Translation of relative addresses to absolute
addresses in terms of word length, word boundary etc.
Go Back

The Big picture


a
i.e.relative to a base address that is known only at run-time Full Screen

Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


Intermediate Home Page

Representation Title Page

Intermediate representations are important for reasons of


JJ II
portability.
(more or less) independent of specific features of the J I
high-level language.
Example. Java byte-code for any high-level language. Page 121 of 100

(more or less) independent of specific features of any


particular target architecture (e.g. number of registers, Go Back

memory size)
Full Screen
number of registers
memory size Close

word length
Quit

First Prev Next Last Go Back Full Screen Close Quit


IR Properties: 1
1. It is fairly low-level containing instructions common to Home Page

all target architectures and assembly languages.


How low can you stoop? . . . Title Page

2. It contains some fairly high-level instructions that are


common to most high-level programming languages. JJ II

How high can you rise?


J I
3. To ensure portability
an unbounded number of variables and memory lo- Page 122 of 100

cations
no commitment to Representational Issues Go Back

4. To ensure type-safety
Full Screen

memory locations are also typed according to the


data they may contain, Close

no commitment is made regarding word boundaries,


and the structure of individual data items. Quit

Next
First Prev Next Last Go Back Full Screen Close Quit
Home Page

Title Page

IR: Representation? JJ II
No commitment to word boundaries or byte boundaries
No commitment to representation of J I

int vs. float,


Page 123 of 100
float vs. double,
packed vs. unpacked, Go Back

strings where and how?.


Back to IR Properties:1 Full Screen

Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


Home Page

IR: How low can you Title Page

stoop? JJ II

most arithmetic and logical operations, load and store


instructions etc. J I

so as to be interpreted easily,
Page 124 of 100
the interpreter is fairly small,
execution speeds are high, Go Back

to have fixed length instructions (where each operand


position has a specific meaning). Full Screen

Back to IR Properties:1 Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


Home Page

IR: How high can you Title Page

rise? JJ II
typed variables,
temporary variables instead of registers. J I

array-indexing,
Page 125 of 100
random access to record fields,
parameter-passing, Go Back

pointers and pointer management


Full Screen
no limits on memory addresses
Back to IR Properties:1 Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


A typical instruction
Home Page

set: 1 Title Page

JJ II
Three address code: A suite of instructions. Each instruc-
tion has at most 3 operands.
J I
an opcode representing an operation with at most 2
operands
Page 126 of 100

two operands on which the binary operation is per-


formed Go Back

a target operand, which accumulates the result of the


(binary) operation. Full Screen

If an operation requires less than 3 operands then one or


Close
more of the operands is made null.

Quit

First Prev Next Last Go Back Full Screen Close Quit


Home Page

Title Page

A typical instruction JJ II

set: 2 J I
Assignments (LOAD-STORE)
Jumps (conditional and unconditional) Page 127 of 100

Procedures and parameters


Go Back
Arrays and array-indexing
Pointer Referencing and Dereferencing Full Screen

Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


Home Page

A typical instruction Title Page

set: 2 JJ II
Assignments (LOAD-STORE)
x := y bop z, where bop is a binary operation J I

x := uop y, where uop is a unary operation


Page 128 of 100
x := y, load, store, copy or register transfer
Jumps (conditional and unconditional) Go Back

Procedures and parameters


Full Screen
Arrays and array-indexing
Pointer Referencing and Dereferencing Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


Home Page

A typical instruction Title Page

set: 2 JJ II
Assignments (LOAD-STORE)
Jumps (conditional and unconditional) J I

goto L Unconditional jump, Page 129 of 100

x relop y goto L Conditional jump, where


relop is a relational operator Go Back

Procedures and parameters


Arrays and array-indexing Full Screen

Pointer Referencing and Dereferencing Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


Home Page

A typical instruction Title Page

set: 2 JJ II
Assignments (LOAD-STORE)
Jumps (conditional and unconditional) J I

Procedures and parameters


Page 130 of 100

call p n, where n is the number of parameters


return y, return value from a procedures call Go Back

param x, parameter declaration


Full Screen
Arrays and array-indexing
Pointer Referencing and Dereferencing Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


A typical instruction Home Page

set: 2 Title Page

Assignments (LOAD-STORE) JJ II

Jumps (conditional and unconditional)


J I
Procedures and parameters
Arrays and array-indexing Page 131 of 100

x := a[i] array indexing for r-value


a[j] := y array indexing for l-value Go Back

Note: The two opcodes are different depending on Full Screen


whether l-value or r-value is desired. x and y are always
simple variables
Close

Pointer Referencing and Dereferencing


Quit

First Prev Next Last Go Back Full Screen Close Quit


A typical instruction Home Page

set: 2 Title Page

Assignments (LOAD-STORE)
JJ II
Jumps (conditional and unconditional)
Procedures and parameters J I

Arrays and array-indexing


Page 132 of 100
Pointer Referencing and Dereferencing
x := y referencing: set x to point to y Go Back

x := *y dereferencing: copy contents of location


pointed to by y into x Full Screen

*x := y dereferencing: copy r-value of y into the


location pointed to by x Close

Picture
Quit

First Prev Next Last Go Back Full Screen Close Quit


Pointers Home Page

Title Page
x y x y
*x *y x := ^y @y *y
JJ II

x y x y
J I
*x @z *z @z
x := *y Page 133 of 100

*z *z
z z Go Back

y y
*y *y
Full Screen
x x
*x := y @z
@z z
z Close

*z *y

Quit

First Prev Next Last Go Back Full Screen Close Quit


IR: Generation Home Page

Can be generated by recursive traversal of the abstract


syntax tree. Title Page

Can be generated by syntax-directed translation as fol-


lows: JJ II

For every non-terminal symbol N in the grammar of the


source language there exist two attributes J I

N.place , which denotes the address of a temporary


Page 134 of 100
variable where the result of the execution of the gen-
erated code is stored
Go Back
N.code , which is the actual code segment generated.
In addition a global counter for the instructions gener- Full Screen

ated is maintained as part of the generation process.


It is independent of the source language but can ex- Close

press target machine operations without committing to


too much detail. Quit

First Prev Next Last Go Back Full Screen Close Quit


Home Page

IR: Infrastructure Title Page

Given an abstract syntax tree T, with T also denoting its


root node. JJ II

T.place address of temporary variable where result of exe-


cution of the T is stored. J I

newtemp returns a fresh variable name and also installs it in


the symbol table along with relevant information Page 135 of 100

T.code the actual sequence of instructions generated for


Go Back
the tree T.
newlabel returns a label to mark an instruction in the gener- Full Screen

ated code which may be the target of a jump.


emit emits an instructions (regarded as a string). Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


Home Page

IR: Infrastructure Title Page

Colour and font coding of IR code generation JJ II

Green: Nodes of the Abstract Syntax Tree


J I
Brown: Characters and strings of the Intermediate
Representation
Page 136 of 100
Red: Variables and data structures of the language in
which the IR code generator is written Go Back

blue: Names of relevant procedures used in IR code gen-


eration. Full Screen

Black: All other stuff.


Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


IR: Example Home Page

Title Page

E id B JJ II

J I
E.place := id.place;
E.code := emit()
Page 137 of 100

E0 E1 E2 B
Go Back

E0.place := newtemp; Full Screen


E0.code := E1.code
|| E2.code
Close
|| emit(E0.place := E1.place E2.place)
Quit

First Prev Next Last Go Back Full Screen Close Quit


IR: Example
Home Page

S id := E B Title Page

JJ II
S.code := E.code
|| emit(id.place:=E.place)
J I

S0 while E do S1 B
Page 138 of 100

S0.begin := newlabel; Go Back


S0.af ter := newlabel;
S0.code := emit(S0.begin:) Full Screen
|| E.code
|| emit(if E.place= 0 goto S0.af ter)
Close
|| S1.code
|| emit(gotoS0.begin)
|| emit(S0.af ter:) Quit

First Prev Next Last Go Back Full Screen Close Quit


IR: Example
Home Page

S id := E B Title Page

JJ II
S.code := E.code
|| emit(id.place:=E.place)
J I

S0 while E do S1 B
Page 139 of 100

S0.begin := newlabel; Go Back


S0.af ter := newlabel;
S0.code := emit(S0.begin:) Full Screen
|| E.code
|| emit(if E.place= 0 goto S0.af ter)
Close
|| S1.code
|| emit(gotoS0.begin)
|| emit(S0.af ter:) Quit

First Prev Next Last Go Back Full Screen Close Quit


Home Page

IR: Generation Title Page


While generating the intermediate representation, it is
sometimes necessary to generate jumps into code that has
JJ II
not been generated as yet (hence the address of the label
is unknown). This usually happens while processing
J I
forward jumps
short-circuit evaluation of boolean expressions Page 140 of 100

It is usual in such circumstances to either fill up the empty


Go Back
label entries in a second pass over the the code or through
a process of backpatching (which is the maintenance of
lists of jumps to the same instruction number), wherein the Full Screen

blank entries are filled in once the sequence number of the


target instruction becomes known. Close

Quit

First Prev Next Last Go Back Full Screen Close Quit


A Calling Chain Home Page

Main program
Title Page
Globals

Procedure P2
Locals of P2 JJ II

Procedure P21
Locals of P21 J I
Body of P21
Call P21
Page 141 of 100

Body of P2
Call P21
Go Back

Procedure P1
Locals of P1
Full Screen
Body of P1
Call P2
Close
Main body
Call P1
Quit

First Prev Next Last Go Back Full Screen Close Quit


Run-time Structure: 1 Home Page

Main program
Title Page

Globals

Procedure P2 JJ II

Locals of P2

J I
Procedure P21
Locals of P21

Page 142 of 100

Body of P21

Body of P2 Go Back

Procedure P1
Full Screen
Locals of P1

Body of P1
Close

Main body
Globals
Quit

First Prev Next Last Go Back Full Screen Close Quit


Run-time Structure: 2 Home Page

Main program
Title Page

Globals

Procedure P2 JJ II

Locals of P2

J I
Procedure P21
Locals of P21

Page 143 of 100

Body of P21

Body of P2 Go Back

Procedure P1 Return address to Main


Full Screen
Locals of P1 Dynamic link to Main
Locals of P1
Body of P1
Static link to Main
Close
Formal par of P1
Main body
Globals
Quit

First Prev Next Last Go Back Full Screen Close Quit


Run-time Structure: 3 Home Page

Main program
Title Page

Globals

Procedure P2 JJ II
Locals of P2

Procedure P21 J I

Locals of P21

Return address to last of P1 Page 144 of 100

Body of P21 Dynamic link to last P1


Locals of P2
Body of P2 Static link to last P1 Go Back
Formal par P2

Procedure P1 Return address to Main


Full Screen
Locals of P1 Dynamic link to Main
Locals of P1
Body of P1
Static link to Main
Formal par of P1 Close

Main body
Globals
Quit

First Prev Next Last Go Back Full Screen Close Quit


Run-time Structure: 4 Home Page

Main program
Title Page

Globals

Procedure P2 JJ II
Return address to last of P2
Locals of P2
Dynamic link to last P2
Locals of P21
Procedure P21 J I
Static link last P2
Locals of P21 Formal par P21

Return address to last of P1 Page 145 of 100

Body of P21 Dynamic link to last P1


Locals of P2
Body of P2 Static link to last P1 Go Back
Formal par P2

Procedure P1 Return address to Main


Full Screen
Locals of P1 Dynamic link to Main
Locals of P1
Body of P1
Static link to Main
Formal par of P1 Close

Main body
Globals
Quit

First Prev Next Last Go Back Full Screen Close Quit


Run-time Structure: 5 Home Page

Return address to last of P21


Main program Dynamic link to last P21 Title Page
Locals of P21
Globals Static link to last P2
Formal par P21
Procedure P2
JJ II
Return address to last of P2
Locals of P2
Dynamic link to last P2
Locals of P21 J I
Procedure P21 Static link last P2
Locals of P21 Formal par P21
Page 146 of 100
Return address to last of P1
Body of P21 Dynamic link to last P1
Locals of P2
Body of P2 Static link to last P1 Go Back
Formal par P2

Procedure P1 Return address to Main


Full Screen
Locals of P1 Dynamic link to Main
Locals of P1
Body of P1
Static link to Main
Close
Formal par of P1
Main body
Globals
Quit

First Prev Next Last Go Back Full Screen Close Quit


Home Page

Title Page

JJ II

J I

Page 147 of 100

Go Back

Full Screen

Close

Quit

First Prev Next Last Go Back Full Screen Close Quit

You might also like