Compiler Design July 2004 Page 1 of 100 S. Arun-Kumar Go Back - - PowerPoint PPT Presentation

compiler design
SMART_READER_LITE
LIVE PREVIEW

Compiler Design July 2004 Page 1 of 100 S. Arun-Kumar Go Back - - PowerPoint PPT Presentation

Home Page Title Page CS432F/CSL 728: Compiler Design 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


slide-1
SLIDE 1

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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

  • S. Arun-Kumar

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

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

July 30, 2004

slide-2
SLIDE 2

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 . . .

slide-3
SLIDE 3

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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
slide-4
SLIDE 4

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 4 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
slide-5
SLIDE 5

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 of characters stream of tokens

slide-6
SLIDE 6

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 of characters stream of tokens parse tree

slide-7
SLIDE 7

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 of characters stream of tokens parse tree abstract syntax tree

slide-8
SLIDE 8

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 of characters stream of tokens parse tree intermediate representation abstract syntax tree

slide-9
SLIDE 9

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 of characters stream of tokens parse tree intermediate representation

  • ptimized

intermediate representation abstract syntax tree

slide-10
SLIDE 10

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 of characters stream of tokens parse tree intermediate representation

  • ptimized

intermediate representation target code abstract syntax tree

slide-11
SLIDE 11

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 of characters stream of tokens parse tree intermediate representation

  • ptimized

intermediate representation target code abstract syntax tree

slide-12
SLIDE 12

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 of characters stream of tokens parse tree intermediate representation

  • ptimized

intermediate representation target code abstract syntax tree

Scanner Parser Semantic Analysis Symbol Table IR Optimization Contents

slide-13
SLIDE 13

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 13 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
slide-14
SLIDE 14

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 from

the lexemes.

  • Eliminates comments and redundant whitepace.
  • Keeps track of line numbers and column numbers and

passes them as parameters to the other phases to en- able error-reporting to the user.

slide-15
SLIDE 15

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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

slide-16
SLIDE 16

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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.)

slide-17
SLIDE 17

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 a

large 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.

slide-18
SLIDE 18

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 9 13 i f a − h , j − z a−e,g−z 0−9,a−z 0−9,a−z 0−9 0−9 − 9 0−9 0−9 ( * 10 11 12 14 * if identifier real real error comment white space error int identifier

  • t

h e r c h a r s any char )

The Big Picture

slide-19
SLIDE 19

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 19 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Syntax Analysis

Consider the following two languages over an alphabet

A = {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 regular

expression

  • A regular expression is not powerful enough to repre-

sent languages which require parenthesis matching to arbitrary 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.

slide-20
SLIDE 20

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 consists

  • f
  • 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.
slide-21
SLIDE 21

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 21 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

CFG: Example

G = {S}, {a, b}, P, S, where S − → ab and S − → aSb

are the only productions in P. Derivations look like this:

S ⇒ ab S ⇒ aSb ⇒ aabb S ⇒ aSb ⇒ aaSbb ⇒ aaabbb L(G), the language generated by G is {anbn|n > 0}.

Actually can be proved by induction on the length and struc- ture of derivations.

slide-22
SLIDE 22

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 22 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

CFG: Empty word

G = {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!

slide-23
SLIDE 23

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 23 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

CFG: Derivation trees 1

Derivation sequences

  • put an artificial order in which productions are fired.
  • instead look at trees of derivations in which we may think
  • f productions as being fired in parallel.
  • There is then no highlighting in red to determine which

copy of a nonterminal was used to get the next member

  • f the sequence.
  • Of course, generation of the empty word ε must be

shown explicitly in the tree.

slide-24
SLIDE 24

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 24 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

CFG: Derivation trees 2

S S S S S S S S a b a b a b ε ε ε

Derivation tree of abaabb

ε

slide-25
SLIDE 25

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 25 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

CFG: Derivation trees 3

S S S S S S S a b a b ε ε S a b ε

Another Derivation tree of abaabb

slide-26
SLIDE 26

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 26 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

CFG: Derivation trees 4

S S S S S S a b a b ε S a b ε S ε

Yet another Derivation tree of abaabb

slide-27
SLIDE 27

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 27 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
slide-28
SLIDE 28

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 28 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Ambiguity: 1

E → I | C | E+E | E∗E I → y | z C → 4

Consider the sentence y + 4 ∗ z.

E E

slide-29
SLIDE 29

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 29 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Ambiguity: 2

E → I | C | E+E | E∗E I → y | z C → 4

Consider the sentence y + 4 ∗ z.

E E E * E E E +

slide-30
SLIDE 30

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 30 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Ambiguity: 3

E → I | C | E+E | E∗E I → y | z C → 4

Consider the sentence y + 4 ∗ z.

E E E E * E + I E E E E + E I *

slide-31
SLIDE 31

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 31 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Ambiguity: 4

E → I | C | E+E | E∗E I → y | z C → 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 *

slide-32
SLIDE 32

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 32 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Ambiguity: 5

E → I | C | E+E | E∗E I → y | z C → 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 *

slide-33
SLIDE 33

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 33 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
slide-34
SLIDE 34

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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

slide-35
SLIDE 35

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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

Shift

a

Principle:

Reduce whenever possible. Shift only when reduce is impossible

slide-36
SLIDE 36

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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

slide-37
SLIDE 37

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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

slide-38
SLIDE 38

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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

slide-39
SLIDE 39

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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

slide-40
SLIDE 40

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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

Shift

a

slide-41
SLIDE 41

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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

slide-42
SLIDE 42

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 E T T

Reduce by r4

slide-43
SLIDE 43

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 E T T

Reduce by r4

slide-44
SLIDE 44

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 E T

Reduce by r1

slide-45
SLIDE 45

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 E T

Shift

slide-46
SLIDE 46

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 E T

Shift

b

slide-47
SLIDE 47

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 E T D

Reduce by r5

slide-48
SLIDE 48

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 E T T

Reduce by r4

slide-49
SLIDE 49

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 E T E

Reduce by r2 S t u c k ! Get back!

slide-50
SLIDE 50

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 E T E

Reduce by r2 Get back!

slide-51
SLIDE 51

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 E T T

Reduce by r4 Get back!

slide-52
SLIDE 52

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 E T D

Reduce by r5 Get back!

slide-53
SLIDE 53

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 E T

Shift

b

Get back!

slide-54
SLIDE 54

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 E T

Shift Get back!

slide-55
SLIDE 55

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 E T

Reduce by r1 Get back to where you

  • nce belonged!
slide-56
SLIDE 56

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 E T T

Reduce by r4

Shift instead

  • f reduce here!

Principle: m

  • d

i f i e d

Reduce whenever possible, but but depending upon lookahead Shift−reduce conflict

slide-57
SLIDE 57

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 E T T

Reduce by r4

slide-58
SLIDE 58

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 E T T

Shift

slide-59
SLIDE 59

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 E T T

Shift

b b

slide-60
SLIDE 60

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 E T T / D

Reduce by r5

slide-61
SLIDE 61

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 E T T

Reduce by r3

slide-62
SLIDE 62

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 E T

Reduce by r1

slide-63
SLIDE 63

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 ( ) a D | | E r5 r4 T D a − / b a

slide-64
SLIDE 64

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 ( ) a D | | E r5 r4 T D D

slide-65
SLIDE 65

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 ( ) a D | | E r5 r4 T D D

slide-66
SLIDE 66

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 ( ) a D | | E r5 r4 T D E D

slide-67
SLIDE 67

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 ( ) a D | | E r5 r4 T D E D

slide-68
SLIDE 68

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 ( ) a D | | E r5 r4 T D E D

slide-69
SLIDE 69

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 ( ) a D | | E r5 r4 T D E D

slide-70
SLIDE 70

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 T T −

  • r1. E

E T r2 E T / r3 T T D b ( ) a D | | E r5 r4 T D E D

slide-71
SLIDE 71

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 T T −

  • r1. E

E T r2 E T / r3 T T D b ( ) a D | | E r5 r4 T D E D

slide-72
SLIDE 72

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 T T −

  • r1. E

E T r2 E T / r3 T T D b ( ) a D | | E r5 r4 T D E D

slide-73
SLIDE 73

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 T T −

  • r1. E

E T r2 E T / r3 T T D b ( ) a D | | E r5 r4 T D E D

slide-74
SLIDE 74

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 ( ) a D | | E r5 r4 T D E D

slide-75
SLIDE 75

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 ( ) a D | | E r5 r4 T D E E D

slide-76
SLIDE 76

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 detected

and reported by the parser-generator.

  • Shift-reduce conflicts may be avoided by suitably

redesigning the context-free grammar.

slide-77
SLIDE 77

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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
  • f 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 may reduce.

  • A minor reworking of the grammar to avoid redundant

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

slide-78
SLIDE 78

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 the

framework within which the target code is to be gener- ated.

  • The parser also provides a structuring mechanism that

divides the task of code generation into bits and pieces determined by the individual nonterminals and produc- tion rules.

  • However,

contex-free grammars are not powerful enough to represent all computable functions. Exam- ple, the language {anbncn|n > 0}.

slide-79
SLIDE 79

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 identifiers in a program.

slide-80
SLIDE 80

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 80 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
slide-81
SLIDE 81

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 81 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Synthesized Attributes:

E T F T T F / / ( n F ( ) E E ) n E T − − T F F n n

slide-82
SLIDE 82

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 82 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Synthesized Attributes: 1

E T F T T F / / ( n F ( ) E E ) n E T − − T F F n n 4 4 3 2 1 Synthesized Attributes

slide-83
SLIDE 83

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 83 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Synthesized Attributes: 2

E T F T T F / / ( n F ( ) E E ) n E T − − T F F n n 4 4 3 2 1 4 Synthesized Attributes

slide-84
SLIDE 84

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 84 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Synthesized Attributes: 3

E T F T T F / / ( n F ( ) E E ) n E T − − T F F n n 4 4 4 4 3 2 1 Synthesized Attributes

slide-85
SLIDE 85

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 85 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Synthesized Attributes: 4

E T F T T F / / ( n F ( ) E E ) n E T − − T F F n n 4 4 4 4 4 3 2 1 Synthesized Attributes

slide-86
SLIDE 86

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 86 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Synthesized Attributes: 5

E T F T T F / / ( n F ( ) E E ) n E T − − T F F n n 4 4 4 4 4 1 3 2 1 Synthesized Attributes

slide-87
SLIDE 87

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 87 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Synthesized Attributes: 6

E T F T T F / / ( n F ( ) E E ) n E T − − T F F n n 4 4 4 4 4 1 1 3 2 1 Synthesized Attributes

slide-88
SLIDE 88

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 88 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Synthesized Attributes: 7

E T F T T F / / ( n F ( ) E E ) n E T − − T F F n n 4 4 4 4 4 1 1 1 3 2 1 Synthesized Attributes

slide-89
SLIDE 89

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 89 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Synthesized Attributes: 8

E T F T T F / / ( n F ( ) E E ) n E T − − T F F n n 4 4 4 4 4 1 1 1 3 2 1 3 Synthesized Attributes

slide-90
SLIDE 90

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 90 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Synthesized Attributes: 9

E T F T T F / / ( n F ( ) E E ) n E T − − T F F n n 4 4 4 4 4 1 1 1 3 3 2 1 3 Synthesized Attributes

slide-91
SLIDE 91

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 91 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Synthesized Attributes: 10

E T F T T F / / ( n F ( ) E E ) n E T − − T F F n n 4 4 4 4 4 1 1 1 3 3 3 2 1 3 Synthesized Attributes

slide-92
SLIDE 92

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 92 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Synthesized Attributes: 11

E T F T T F / / ( n F ( ) E E ) n E T − − T F F n n 4 4 4 4 4 1 1 1 3 2 3 3 2 1 3 Synthesized Attributes

slide-93
SLIDE 93

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 93 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Synthesized Attributes: 12

E T F T T F / / ( n F ( ) E E ) n E T − − T F F n n 4 4 4 4 4 1 1 1 3 2 2 3 3 2 1 3 Synthesized Attributes

slide-94
SLIDE 94

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 94 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Synthesized Attributes: 13

E T F T T F / / ( n F ( ) E E ) n E T − − T F F n n 4 4 4 4 4 1 1 1 3 2 2 3 3 2 1 1 3 Synthesized Attributes

slide-95
SLIDE 95

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 95 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Synthesized Attributes: 14

E T F T T F / / ( n F ( ) E E ) n E T − − T F F n n 4 4 4 4 4 1 1 1 3 2 2 3 3 2 1 1 1 3 Synthesized Attributes

slide-96
SLIDE 96

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 / n F ( ) E E T − T F F n n 4 4 4 4 1 1 1 3 2 2 3 1 1 3

E0 → E1−T ⊲ E0.val := sub(E1.val, T.val) E → T ⊲ E.val := T.val T0 → T1/F ⊲ T0.val := div(T1.val, F.val) T → F ⊲ T.val := F.val F → (E) ⊲ F.val := E.val F → n ⊲ F.val := n.val

slide-97
SLIDE 97

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 97 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Inherited Attributes: 0

C-style declarations generating int x, y, z. D → T L T → int | float L → 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 ,

slide-98
SLIDE 98

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 98 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Inherited Attributes: 1

C-style declarations generating int x, y, z. D → T L T → int | float L → 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 ,

slide-99
SLIDE 99

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 99 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Inherited Attributes: 2

C-style declarations generating int x, y, z. D → T L T → int | float L → 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 ,

slide-100
SLIDE 100

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 100 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Inherited Attributes: 3

C-style declarations generating int x, y, z. D → T L T → int | float L → 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

slide-101
SLIDE 101

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 101 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Inherited Attributes: 4

C-style declarations generating int x, y, z. D → T L T → int | float L → 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

slide-102
SLIDE 102

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 102 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Inherited Attributes: 5

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

slide-103
SLIDE 103

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 103 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Inherited Attributes: 6

C-style declarations generating int x, y, z. D → T L T → int | float L → 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

slide-104
SLIDE 104

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 104 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Inherited Attributes: 7

C-style declarations generating int x, y, z. D → T L T → int | float L → 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

slide-105
SLIDE 105

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 ⊲ L.in := T.type T → int ⊲ T.type := int.int T → float ⊲ T.type := float.float L0 → L1,I ⊲ L1 := L0.in L → I ⊲ I.in := L.in I → id ⊲ id.type := I.in

slide-106
SLIDE 106

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 106 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit
slide-107
SLIDE 107

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 107 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Abstract Syntax: 0

E → E−T | T T → T/F | F F → 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 −− /

slide-108
SLIDE 108

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 −− /

slide-109
SLIDE 109

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 −− /

slide-110
SLIDE 110

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 110 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Evaluation: 2

2 / 3

slide-111
SLIDE 111

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 111 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Evaluation: 3

2 / 3

slide-112
SLIDE 112

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 looks like . . .

slide-113
SLIDE 113

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 / n F ( ) E E T − T F F n n

n n / n −−

slide-114
SLIDE 114

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 114 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Abstract Syntax: 2

We use attribute grammar rules to construct the abstract syntax tree (AST)!. But in order to do that we first require two procedures for tree 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) and returns a pointer to the newly created node. Now we may associate a synthesized attribute called ptr with each terminal and nonterminal symbol which points to the root of the subtree created for it.

slide-115
SLIDE 115

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 115 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Abstract Syntax: 3

E0 → E1−T ⊲ E0.ptr := makeBinaryNode(−, E1.ptr, T.ptr) E → T ⊲ E.ptr := T.ptr T0 → T1/F ⊲ T0.ptr := makeBinaryNode(/, T1.ptr, F.ptr) T → F ⊲ T.ptr := F.ptr F → (E) ⊲ F.ptr := E.ptr F → n ⊲ F.ptr := makeLeaf(n.val) The Big Picture

slide-116
SLIDE 116

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 find

the 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 in

the symbol table

slide-117
SLIDE 117

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 117 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Symbol Table:2

Attributes stored in a symbol table for each identifier:

  • type
  • size
  • scope/visibility information
  • base address
  • addresses to location of auxiliary symbol tables (in case
  • f records, procedures, classes)
  • address of the location containing the string which ac-

tually names the identifier and its length in the string pool

slide-118
SLIDE 118

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 scope and visibility) and not physical

  • Keywords are often stored in the symbol table before

the compilation process begins.

slide-119
SLIDE 119

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 119 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Symbol Table:4

Accesses 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 an

  • perand exists (declaration before use).

Semantic analysis:

  • Determination of types of identifiers from declara-

tions

  • type checking to ensure that operands are used in

type-valid contexts.

  • Checking scope, visibility violations.
slide-120
SLIDE 120

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 120 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Symbol Table:5

IR generation: . Memory allocation and relativea address calculation. Optimization: All memory accesses through symbol table Target code: Translation of relative addresses to absolute addresses in terms of word length, word boundary etc. The Big picture

ai.e.relative to a base address that is known only at run-time

slide-121
SLIDE 121

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 121 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Intermediate Representation

Intermediate representations are important for reasons of portability.

  • (more or less) independent of specific features of the

high-level language.

  • Example. Java byte-code for any high-level language.
  • (more or less) independent of specific features of any

particular target architecture (e.g. number of registers, memory size) – number of registers – memory size – word length

slide-122
SLIDE 122

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 122 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

IR Properties: 1

  • 1. 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 are

common 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 the

data they may contain,

  • no commitment is made regarding word boundaries,

and the structure of individual data items. Next

slide-123
SLIDE 123

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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

slide-124
SLIDE 124

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 124 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

IR: How low can you stoop?

  • most arithmetic and logical operations, load and store

instructions etc.

  • so as to be interpreted easily,
  • the interpreter is fairly small,
  • execution speeds are high,
  • to have fixed length instructions (where each operand

position has a specific meaning). Back to IR Properties:1

slide-125
SLIDE 125

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 125 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

IR: How high can you rise?

  • 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

slide-126
SLIDE 126

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 126 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

A typical instruction set: 1

Three address code: A suite of instructions. Each instruc- tion has at most 3 operands.

  • an opcode representing an operation with at most 2
  • perands
  • 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 or more of the operands is made null.

slide-127
SLIDE 127

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 127 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

A typical instruction set: 2

  • Assignments (LOAD-STORE)
  • Jumps (conditional and unconditional)
  • Procedures and parameters
  • Arrays and array-indexing
  • Pointer Referencing and Dereferencing
slide-128
SLIDE 128

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 128 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

A typical instruction set: 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
slide-129
SLIDE 129

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 129 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

A typical instruction set: 2

  • Assignments (LOAD-STORE)
  • Jumps (conditional and unconditional)

– goto L – Unconditional jump, – x relop y goto L – Conditional jump, where relop is a relational operator

  • Procedures and parameters
  • Arrays and array-indexing
  • Pointer Referencing and Dereferencing
slide-130
SLIDE 130

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 130 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

A typical instruction set: 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
slide-131
SLIDE 131

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 131 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

A typical instruction set: 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 on whether l-value or r-value is desired. x and y are always simple variables

  • Pointer Referencing and Dereferencing
slide-132
SLIDE 132

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 132 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

A typical instruction set: 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 location pointed to by y into x – *x := y – dereferencing: copy r-value of y into the location pointed to by x Picture

slide-133
SLIDE 133

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 133 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Pointers

x x *y y @z *x *x @z *z z *z z x := ^y x := *y *x := y *y @y @z *z *z x y y x x y z x y y *y *y *y @z z

slide-134
SLIDE 134

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 the source language there exist two attributes N.place , which denotes the address of a temporary variable 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 to too much detail.

slide-135
SLIDE 135

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 135 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

IR: Infrastructure

Given an abstract syntax tree T, with T also denoting its root node. T.place address of temporary variable where result of exe- cution of the T is stored. newtemp returns a fresh variable name and also installs it in the symbol table along with relevant information T.code the actual sequence of instructions generated for the tree T. newlabel returns 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).

slide-136
SLIDE 136

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 136 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

IR: Infrastructure

Colour and font coding of IR code generation

  • Green: Nodes of the Abstract Syntax Tree
  • Brown: Characters and strings of the Intermediate

Representation

  • Red: Variables and data structures of the language in

which the IR code generator is written

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

eration.

  • Black: All other stuff.
slide-137
SLIDE 137

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 137 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

IR: Example

E → id ⊲ E.place := id.place; E.code := emit() E0 → E1 − E2 ⊲ E0.place := newtemp; E0.code := E1.code || E2.code || emit(E0.place := E1.place − E2.place)

slide-138
SLIDE 138

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 ⊲ S.code := E.code || emit(id.place:=E.place) S0 → while E do S1 ⊲ 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:)

slide-139
SLIDE 139

Home Page Title Page ◭◭ ◮◮ ◭ ◮ 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 ⊲ S.code := E.code || emit(id.place:=E.place) S0 → while E do S1 ⊲ 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:)

slide-140
SLIDE 140

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 140 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

IR: Generation

While generating the intermediate representation, it is sometimes necessary to generate jumps into code that has not been generated as yet (hence the address of the label is 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 empty 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 blank entries are filled in once the sequence number of the target instruction becomes known.

slide-141
SLIDE 141

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 141 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

A Calling Chain

Main program Globals Procedure P2 Locals 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

slide-142
SLIDE 142

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 142 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Run-time Structure: 1

Main 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

slide-143
SLIDE 143

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 143 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Run-time Structure: 2

Main 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 Dynamic link to Main Static link to Main Body of P21

slide-144
SLIDE 144

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 144 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Run-time Structure: 3

Main 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

slide-145
SLIDE 145

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 145 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Run-time Structure: 4

Main 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

slide-146
SLIDE 146

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 146 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit

Run-time Structure: 5

Main 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

slide-147
SLIDE 147

Home Page Title Page ◭◭ ◮◮ ◭ ◮ Page 147 of 100 Go Back Full Screen Close Quit

  • First •Prev •Next •Last •Go Back •Full Screen •Close •Quit