compiler construction
play

Compiler Construction Lecture 14: Semantic Analysis III (Attribute - PowerPoint PPT Presentation

Compiler Construction Lecture 14: Semantic Analysis III (Attribute Evaluation) Thomas Noll Lehrstuhl f ur Informatik 2 (Software Modeling and Verification) noll@cs.rwth-aachen.de http://moves.rwth-aachen.de/teaching/ss-14/cc14/ Summer


  1. Compiler Construction Lecture 14: Semantic Analysis III (Attribute Evaluation) Thomas Noll Lehrstuhl f¨ ur Informatik 2 (Software Modeling and Verification) noll@cs.rwth-aachen.de http://moves.rwth-aachen.de/teaching/ss-14/cc14/ Summer Semester 2014

  2. Outline Recap: Circularity of Attribute Grammars 1 The Circularity Check 2 Correctness and Complexity of the Circularity Check 3 Attribute Evaluation 4 Attribute Evaluation by Topological Sorting 5 L-Attributed Grammars 6 Compiler Construction Summer Semester 2014 14.3

  3. Circularity of Attribute Grammars Goal: unique solvability of equation system ⇒ avoid cyclic dependencies = Definition (Circularity) An attribute grammar A = � G , E , V � ∈ AG is called circular if there exists a syntax tree t such that the attribute equation system E t is recursive (i.e., some attribute variable of t depends on itself). Otherwise it is called noncircular. Remark: because of the division of Var π into In π and Out π , cyclic dependencies cannot occur at production level. Compiler Construction Summer Semester 2014 14.4

  4. Attribute Dependency Graphs and Circularity I Observation: a cycle in the dependency graph D t of a given syntax tree t is caused by the occurrence of a “cover” production π = A 0 → w 0 A 1 w 1 . . . A r w r ∈ P in a node k 0 of t such that the dependencies in E k 0 yield the “upper end” of the cycle and for at least one i ∈ [ r ], some attributes in syn ( A i ) depend on attributes in inh ( A i ). Example on the board To identify such “critical” situations we need to determine for each i ∈ [ r ] the possible ways in which attributes in syn ( A i ) can depend on attributes in inh ( A i ). Compiler Construction Summer Semester 2014 14.5

  5. Attribute Dependency Graphs and Circularity II Definition (Attribute dependence) Let A = � G , E , V � ∈ AG with G = � N , Σ , P , S � . If t is a syntax tree with root label A ∈ N and root node k , α ∈ syn ( A ), and β ∈ inh ( A ) such that β. k → + t α. k , then α is A → α ). dependent on β below A in t (notation: β ֒ For every syntax tree t with root label A ∈ N , A is ( A , t ) := { ( β, α ) ∈ inh ( A ) × syn ( A ) | β ֒ → α in t } . For every A ∈ N , IS ( A ) := { is ( A , t ) | t syntax tree with root label A } ⊆ 2 Inh × Syn . Remark: it is important that IS ( A ) is a system of attribute dependence sets, not a union (otherwise: strong noncircularity—see exercises). Example on the board Compiler Construction Summer Semester 2014 14.6

  6. The Circularity Check I In the circularity check, the dependency systems IS ( A ) are iteratively computed. The following notation is employed: Definition Given π = A → w 0 A 1 w 1 . . . A r w r ∈ P and is i ⊆ inh ( A i ) × syn ( A i ) for every i ∈ [ r ], let is [ π ; is 1 , . . . , is r ] ⊆ inh ( A ) × syn ( A ) be given by is [ π ; is 1 , . . . , is r ] := � i =1 { ( β ′ . p i , α ′ . p i ) | ( β ′ , α ′ ) ∈ is i } ) + � ( β, α ) | ( β. 0 , α. 0) ∈ ( → π ∪ � r where p i := � i j =1 | w j − 1 | + i . Example on the board Compiler Construction Summer Semester 2014 14.7

  7. Outline Recap: Circularity of Attribute Grammars 1 The Circularity Check 2 Correctness and Complexity of the Circularity Check 3 Attribute Evaluation 4 Attribute Evaluation by Topological Sorting 5 L-Attributed Grammars 6 Compiler Construction Summer Semester 2014 14.8

  8. The Circularity Check II Algorithm 14.1 (Circularity check for attribute grammars) Input: A = � G , E , V � ∈ AG with G = � N , Σ , P , S � Procedure: for every A ∈ N, iteratively construct IS ( A ) as follows: 1 if π = A → w ∈ P, then is [ π ] ∈ IS ( A ) 1 if π = A → w 0 A 1 w 1 . . . A r w r ∈ P and is i ∈ IS ( A i ) for 2 every i ∈ [ r ] , then is [ π ; is 1 , . . . , is r ] ∈ IS ( A ) test whether A is circular by checking if there exist 2 π = A → w 0 A 1 w 1 . . . A r w r ∈ P and is i ∈ IS ( A i ) for every i ∈ [ r ] such that the following relation is cyclic: → π ∪ � r i =1 { ( β. p i , α. p i ) | ( β, α ) ∈ is i } (where p i := � i j =1 | w j − 1 | + i) Output: “yes” or “no” Compiler Construction Summer Semester 2014 14.9

  9. The Circularity Check III Example 14.2 D S → AB : D A → a : α 1 α 2 α β S A α 1 α 2 α 1 α 2 a β A β B D B → AB : D A → c : α 1 α 2 α 1 α 2 β B β A α 1 α 2 α 1 α 2 c β β A B D A → B : D B → b : α 1 α 2 α 1 α 2 β β A B α 1 α 2 β B b Application of Algorithm 14.1: on the board Compiler Construction Summer Semester 2014 14.10

  10. Outline Recap: Circularity of Attribute Grammars 1 The Circularity Check 2 Correctness and Complexity of the Circularity Check 3 Attribute Evaluation 4 Attribute Evaluation by Topological Sorting 5 L-Attributed Grammars 6 Compiler Construction Summer Semester 2014 14.11

  11. Correctness and Complexity of Circularity Check Theorem 14.3 (Correctness of circularity check) An attribute grammar is circular iff Algorithm 14.1 yields the answer “yes”. Proof. by induction on the syntax tree t with cyclic D t Lemma 14.4 The time complexity of the circularity check is exponential in the size of the attribute grammar (= maximal length of right-hand sides of productions). Proof. by reduction of the word problem of alternating Turing machines (see M. Jazayeri: A Simpler Construction for Showing the Intrinsically Exponential Complexity of the Circularity Problem for Attribute Grammars , Comm. of the ACM 28(4), 1981, pp. 715–720) Compiler Construction Summer Semester 2014 14.12

  12. Outline Recap: Circularity of Attribute Grammars 1 The Circularity Check 2 Correctness and Complexity of the Circularity Check 3 Attribute Evaluation 4 Attribute Evaluation by Topological Sorting 5 L-Attributed Grammars 6 Compiler Construction Summer Semester 2014 14.13

  13. Attribute Evaluation Methods Given: noncircular attribute grammar A = � G , E , V � ∈ AG syntax tree t of G valuation v : Syn Σ → V where Syn Σ := { α. k | k labelled by a ∈ Σ , α ∈ syn ( a ) } ⊆ Var t Goal: extend v to (partial) solution v : Var t → V Methods: Topological sorting of D t (later): 1 start with variables which depend at most on Syn Σ 1 proceed by successive substitution 2 Strongly noncircular AGs: recursive functions (details 2 omitted) for every A ∈ N and α ∈ syn ( A ), define evaluation 1 function g A ,α with the following parameters: the node of t where α has to be evaluated and all inherited attributes of A on which α (potentially) depends for every α ∈ syn ( S ), evaluate g S ,α ( k 0 ) where k 0 2 denotes the root of t L-attributed grammars: integration with top-down parsing 3 (later) S-attributed grammars (i.e., Inh = ∅ ): yacc 4 Compiler Construction Summer Semester 2014 14.14

  14. Outline Recap: Circularity of Attribute Grammars 1 The Circularity Check 2 Correctness and Complexity of the Circularity Check 3 Attribute Evaluation 4 Attribute Evaluation by Topological Sorting 5 L-Attributed Grammars 6 Compiler Construction Summer Semester 2014 14.15

  15. Attribute Evaluation by Topological Sorting Algorithm 14.5 (Evaluation by topological sorting) Input: noncircular A = � G , E , V � ∈ AG, syntax tree t of G, valuation v : Syn Σ → V Procedure: let Var := Var t \ Syn Σ (* attributes to be evaluated *) 1 while Var � = ∅ do 2 let x ∈ Var such that { y ∈ Var | y → t x } = ∅ 1 let x = f ( x 1 , . . . , x n ) ∈ E t 2 let v ( x ) := f ( v ( x 1 ) , . . . , v ( x n )) 3 let Var := Var \ { x } 4 Output: solution v : Var t → V Remark: noncircularity guarantees that in step 2.1 at least one such x is available Example 14.6 see Examples 12.1 and 12.2 (Knuth’s binary numbers) Compiler Construction Summer Semester 2014 14.16

  16. Outline Recap: Circularity of Attribute Grammars 1 The Circularity Check 2 Correctness and Complexity of the Circularity Check 3 Attribute Evaluation 4 Attribute Evaluation by Topological Sorting 5 L-Attributed Grammars 6 Compiler Construction Summer Semester 2014 14.17

  17. L-Attributed Grammars I In an L-attributed grammar, attribute dependencies on the right-hand sides of productions are only allowed to run from left to right. Definition 14.1 (L-attributed grammar) Let A = � G , E , V � ∈ AG such that, for every π ∈ P and β. i = f ( . . . , α. j , . . . ) ∈ E π with β ∈ Inh and α ∈ Syn , j < i . Then A is called an L-attributed grammar (notation: A ∈ LAG ). Remark: note that no restrictions are imposed for β ∈ Syn (for i = 0) or α ∈ Inh (for j = 0). Thus, in an L-attributed grammar, synthesized attributes of the left-hand side can depend on any outer variable and every inner variable can depend on any inherited attribute of the left-hand side. Corollary 14.2 Every A ∈ LAG is noncircular. Compiler Construction Summer Semester 2014 14.18

  18. L-Attributed Grammars II Example 14.3 S 6 L-attributed grammar: S → AB i . 1 = 0 0 A 3 4 B 5 i . 2 = s . 1 + 1 s . 0 = s . 2 + 1 A → a A i . 2 = i . 0 + 1 a 1 A 2 b s . 0 = s . 2 + 1 A → c s . 0 = i . 0 + 1 B → b s . 0 = i . 0 + 1 c Compiler Construction Summer Semester 2014 14.19

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend