Compiler Construction - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

Compiler Construction

Description:

Compiler Construction Sohail Aslam Lecture 32 Evaluation Methods Dynamic methods Build the parse tree Build the dependence graph Topological sort the graph Define ... – PowerPoint PPT presentation

Number of Views:91
Avg rating:3.0/5.0
Slides: 31
Provided by: white
Category:

less

Transcript and Presenter's Notes

Title: Compiler Construction


1
Compiler Construction
  • Sohail Aslam
  • Lecture 32

2

Parse tree for 101
Number
List
Sign
List

Bit
List
1
Bit
Bit
0
1
3

attributed tree
val -5
Number
pos 0val 5
List
Sign
neg true
pos 1val 4
List
pos 0val 1
Bit

pos 2val 4
pos 1val 0
List
1
Bit
pos 2val 4
Bit
0
1
4

Inherited Attributes
val -5
Number
pos 0val 5
List
Sign
neg true
pos 1val 4
List
pos 0val 1
Bit

pos 2val 4
pos 1val 0
List
1
Bit
pos 2val 4
Bit
0
1
5

Synthesized Attributes
val -5
Number
pos 0val 5
List
Sign
neg true
pos 1val 4
List
pos 0val 1
Bit

pos 2val 4
pos 1val 0
List
1
Bit
pos 2val 4
Bit
0
1
6

Together
val -5
Number
pos 0val 5
List
Sign
neg true
pos 1val 4
List
pos 0val 1
Bit

pos 2val 4
pos 1val 0
List
1
Bit
pos 2val 4
Bit
0
1
7

peel away parse tree
val -5
Number
pos 0val 5
List
Sign
neg true
pos 1val 4
List
pos 0val 1
Bit

pos 2val 4
pos 1val 0
List
1
Bit
pos 2val 4
Bit
0
1
8

dependence graph
val -5
pos 0val 5
neg true
pos 1val 4
pos 0val 1

pos 2val 4
pos 1val 0
1
pos 2val 4
0
1
9

dependence graph
val -5
pos 0val 5
neg true
pos 1val 4
pos 0val 1

pos 2val 4
pos 1val 0
1
pos 2val 4
0
must be acyclic!
1
10
Evaluation Methods
  • Dynamic methods
  • Build the parse tree
  • Build the dependence graph
  • Topological sort the graph
  • Define attributes in topological order

11
Evaluation Methods
  • Dynamic methods
  • Build the parse tree
  • Build the dependence graph
  • Topological sort the graph
  • Define attributes in topological order

12
Evaluation Methods
  • Dynamic methods
  • Build the parse tree
  • Build the dependence graph
  • Topological sort the graph
  • Define attributes in topological order

13
Evaluation Methods
  • Dynamic methods
  • Build the parse tree
  • Build the dependence graph
  • Topological sort the graph
  • Define attributes in topological order

14
Evaluation Methods
  • Dynamic methods
  • Build the parse tree
  • Build the dependence graph
  • Topological sort the graph
  • Define attributes in topological order

15
Evaluation Methods
  • Rule-based (treewalk)
  • Analyze attribute rules at compiler-generation
    time
  • Determine a fixed (static) ordering
  • Evaluate nodes in that order

16
Evaluation Methods
  • Oblivious (passes, dataflow)
  • Ignore rules and parse tree
  • Pick a convenient order (at design time) and use
    it

17
Problems
  • Attribute grammars have not achieved widespread
    use due to a myraid of problems

18
Problems
  • non-local computation
  • traversing parse tree
  • storage management for short-lived attributes
  • lack of high-quality inexpensive tools

19
Problems
  • non-local computation
  • traversing parse tree
  • storage management for short-lived attributes
  • lack of high-quality inexpensive tools

20
Problems
  • non-local computation
  • traversing parse tree
  • storage management for short-lived attributes
  • lack of high-quality inexpensive tools

21
Problems
  • non-local computation
  • traversing parse tree
  • storage management for short-lived attributes
  • lack of high-quality inexpensive tools

22
Ad-Hoc Analysis
  • In rule-based evaluators, a sequence of actions
    are associated with grammar productions

23
Ad-Hoc Analysis
  • Organizing actions required for context-sensitive
    analysis around structure of the grammar leads to
    powerful, albeit ad-hoc, approach which is used
    on most parsers

24
Ad-Hoc Analysis
  • A snippet of code (action) is associated with
    each production that executes at parse time
  • In top-down parsers, the snippet is added to the
    appropriate parsing routine

25
Ad-Hoc Analysis
  • A snippet of code (action) is associated with
    each production that executes at parse time
  • In top-down parsers, the snippet is added to the
    appropriate parsing routine

26
Ad-Hoc Analysis
  • In a bottom-up shift-reduce parsers, the actions
    are performed each time the parser performs a
    reduction.

27
LR(1) Skeleton Parser
  • stack.push(dummy) stack.push(0)
  • done false token scanner.next()
  • while (!done)
  • s stack.top()
  • if( Actions,token reduce A?b)
  • stack.pop(2?b)
  • s stack.top()
  • stack.push(A)
  • stack.push(Gotos,A)
  • else if( Actions,token shift i)
  • stack.push(token) stack.push(i) token
    scanner.next()

28
LR(1) Skeleton Parser
  • stack.push(dummy) stack.push(0)
  • done false token scanner.next()
  • while (!done)
  • s stack.top()
  • if( Actions,token reduce A?b)
  • stack.pop(2?b)
  • s stack.top()
  • stack.push(A)
  • stack.push(Gotos,A)
  • else if( Actions,token shift i)
  • stack.push(token) stack.push(i) token
    scanner.next()

29
LR(1) Skeleton Parser
  • if( Actions,token reduce A?b)

stack.pop(2?b) s stack.top()
stack.push(A) stack.push(Gotos,A)
invoke the code snippet
30
Productions Code snippet
Number ? Sign List Number.val ? Sign.val ? List.val
Sign ? Sign.val ? 1
Sign ? Sign.val ? 1
List ? Bit List.val ? Bit.val
List0 ? List1 Bit List0.val ? 2?List1.val Bit.val
Bit ? 0 Bit.val ? 0
Bit ? 1 Bit.val ? 1
Write a Comment
User Comments (0)
About PowerShow.com