Compiler Construction Sohail Aslam Lecture 32
2 Number Sign List Bit – 1 List Bit 1 0 Parse tree for – 101
3 Number Sign List Bit – 1 val: -5 List Bit 1 0 attributed tree pos: 0 val: 5 pos: 0 val: 1 pos: 1 val: 4 pos: 1 val: 0 pos: 2 val: 4 neg: true
4 Number Sign List Bit – 1 val: -5 List Bit 1 0 Inherited Attributes pos: 0 val: 5 pos: 0 val: 1 pos: 1 val: 4 pos: 1 val: 0 pos: 2 val: 4 neg: true
5 Number Sign List Bit – 1 val: -5 List Bit 1 0 Synthesized Attributes pos: 0 val: 5 pos: 0 val: 1 pos: 1 val: 4 pos: 1 val: 0 pos: 2 val: 4 neg: true
6 Number Sign List Bit – 1 val: -5 List Bit 1 0 Together pos: 0 val: 5 pos: 0 val: 1 pos: 1 val: 4 pos: 1 val: 0 pos: 2 val: 4 neg: true
7 Number Sign List Bit – 1 val: -5 List Bit 1 0 peel away parse tree pos: 0 val: 5 pos: 0 val: 1 pos: 1 val: 4 pos: 1 val: 0 pos: 2 val: 4 neg: true
8 – 1 val: dependence graph pos: 0 val: 5 pos: 0 val: 1 pos: 1 val: 4 pos: 1 val: 0 pos: 2 val: 4 neg: true
9 – 1 val: dependence graph pos: 0 val: 5 pos: 0 val: 1 pos: 1 val: 4 pos: 1 val: 0 pos: 2 val: 4 neg: true must be acyclic!
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 ProblemsProblems Attribute grammars have not achieved widespread use due to a myraid of problems
18 ProblemsProblems non-local computation traversing parse tree storage management for short-lived attributes lack of high-quality inexpensive tools
19 ProblemsProblems non-local computation traversing parse tree storage management for short-lived attributes lack of high-quality inexpensive tools
20 ProblemsProblems non-local computation traversing parse tree storage management for short-lived attributes lack of high-quality inexpensive tools
21 ProblemsProblems 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( Action[s,token] == “reduce A → ”) { stack.pop(2 | |); s = stack.top(); stack.push( A ); stack.push(Goto[s, A ]); } else if( Action[s,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( Action[s,token] == “reduce A → ”) { stack.pop(2 | |); s = stack.top(); stack.push( A ); stack.push(Goto[s, A ]); } else if( Action[s,token] == “shift i”){ stack.push(token); stack.push(i); token = scanner.next(); }
29 LR(1) Skeleton Parser if( Action[s,token] == “reduce A → ”) { stack.pop(2 | |); s = stack.top(); stack.push( A ); stack.push(Goto[s, 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 → BitList.val ← Bit.val List 0 → List 1 Bit List 0.val ← 2 List 1.val + Bit.val Bit → 0 Bit.val ← 0 Bit → 1 Bit.val ← 1