Compiler Construction Lecture 13: Semantic Analysis II (Circularity Check) 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
Outline Recap: Attribute Grammars 1 Circularity of Attribute Grammars 2 Attribute Dependency Graphs 3 Testing Attribute Grammars for Circularity 4 The Circularity Check 5 Compiler Construction Summer Semester 2014 13.2
Conceptual Structure of a Compiler Source code Lexical analysis (Scanner) Assgn Syntax analysis (Parser) Var Exp Sum Var Const Semantic analysis attribute grammars Assgn ok Exp int Var int int Sum Generation of intermediate code int Var Const int Code optimization Generation of machine code Target code Compiler Construction Summer Semester 2014 13.3
Attribute Grammars Goal: compute context-dependent but runtime-independent properties of a given program Idea: enrich context-free grammar by semantic rules which annotate syntax tree with attribute values ⇒ Semantic analysis = attribute evaluation = Result: attributed syntax tree In greater detail: With every nonterminal a set of attributes is associated. Two types of attributes are distinguished: Synthesized: bottom-up computation (from the leaves to the root) Inherited: top-down computation (from the root to the leaves) With every production a set of semantic rules is associated. Compiler Construction Summer Semester 2014 13.4
Formal Definition of Attribute Grammars I Definition (Attribute grammar) Let G = � N , Σ , P , S � ∈ CFG Σ with X := N ⊎ Σ. Let Att = Syn ⊎ Inh be a set of (synthesized or inherited) attributes, and let α ∈ Att V α be a union of value sets. V = � Let att : X → 2 Att be an attribute assignment, and let syn ( Y ) := att ( Y ) ∩ Syn and inh ( Y ) := att ( Y ) ∩ Inh for every Y ∈ X . Every production π = Y 0 → Y 1 . . . Y r ∈ P determines the set Var π := { α. i | α ∈ att ( Y i ) , i ∈ { 0 , . . ., r }} of attribute variables of π with the subsets of inner and outer variables: In π := { α. i | ( i = 0 , α ∈ syn ( Y i )) or ( i ∈ [ r ] , α ∈ inh ( Y i )) } Out π := Var π \ In π A semantic rule of π is an equation of the form α. i = f ( α 1 . i 1 , . . . , α n . i n ) where n ∈ N , α. i ∈ In π , α j . i j ∈ Out π , and f : V α 1 × . . . × V α n → V α . For each π ∈ P , let E π be a set with exactly one semantic rule for every inner variable of π , and let E := ( E π | π ∈ P ). Then A := � G , E , V � is called an attribute grammar: A ∈ AG . Compiler Construction Summer Semester 2014 13.5
Attribution of Syntax Trees I Definition (Attribution of syntax trees) Let A = � G , E , V � ∈ AG , and let t be a syntax tree of G with the set of nodes K . K determines the set of attribute variables of t : Var t := { α. k | k ∈ K labelled with Y ∈ X , α ∈ att ( Y ) } . Let k 0 ∈ K be an (inner) node where production π = Y 0 → Y 1 . . . Y r ∈ P is applied, and let k 1 , . . . , k r ∈ K be the corresponding successor nodes. The attribute equation system E k 0 of k 0 is obtained from E π by substituting every attribute index i ∈ { 0 , . . . , r } by k i . The attribute equation system of t is given by � { E k | k inner node of t } . E t := Compiler Construction Summer Semester 2014 13.6
Attribution of Syntax Trees II Example (cf. Example 12.2) k 0 : S d Attributed syntax tree for 10.1 : p p k 1 : L d l k 7 : . k 8 : L d l p p p k 2 : L d l k 5 : B d k 9 : B d p k 3 : B k 6 : 0 k 10 : 1 d k 4 : 1 E S → L . L : d . 0 = d . 1 + d . 3 E k 0 : d . k 0 = d . k 1 + d . k 8 subst p . 1 = 0 − → p . k 1 = 0 p . 3 = − l . 3 p . k 8 = − l . k 8 E L → LB : d . 0 = d . 1 + d . 2 E k 1 : d . k 1 = d . k 2 + d . k 5 l . 0 = l . 1 + 1 Compiler Construction l . k 1 = l . k 2 + 1 Summer Semester 2014 13.7 subst − →
Attribution of Syntax Trees III Corollary For each α. k ∈ Var t except the inherited attribute variables at the root and the synthesized attribute variables at the leaves of t, E t contains exactly one equation with left-hand side α. k. Assumptions: The start symbol does not have inherited attributes: inh ( S ) = ∅ . Synthesized attributes of terminal symbols are provided by the scanner. Compiler Construction Summer Semester 2014 13.8
Outline Recap: Attribute Grammars 1 Circularity of Attribute Grammars 2 Attribute Dependency Graphs 3 Testing Attribute Grammars for Circularity 4 The Circularity Check 5 Compiler Construction Summer Semester 2014 13.9
Solvability of Attribute Equation System I Definition 13.1 (Solution of attribute equation system) Let A = � G , E , V � ∈ AG , and let t be a syntax tree of G . A solution of E t is a mapping v : Var t → V such that, for every α. k ∈ Var t and α. k = f ( α. k 1 , . . . , α. k n ) ∈ E t , v ( α. k ) = f ( v ( α. k 1 ) , . . . , v ( α. k n )) . In general, the attribute equation system E t of a given syntax tree t can have no solution, exactly one solution, or several solutions. Compiler Construction Summer Semester 2014 13.10
Solvability of Attribute Equation System II Example 13.2 A → aB , B → b ∈ P α ∈ syn ( B ), β ∈ inh ( B ) = ⇒ cyclic dependency: β. 2 = f ( α. 2) ∈ E A → aB A α. 0 = β. 0 ∈ E B → b a α β k : B ⇒ for V α := V β := N and = f ( x ) := x + 1: no solution b f ( x ) := 2 x : exactly one solution E t : β. k = f ( α. k ) ( v ( α. k ) = v ( β. k ) = 0) α. k = β. k f ( x ) := x : infinitely many solutions ( v ( α. k ) = v ( β. k ) = y for any y ∈ N ) Compiler Construction Summer Semester 2014 13.11
Circularity of Attribute Grammars Goal: unique solvability of equation system ⇒ avoid cyclic dependencies = Definition 13.3 (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 13.12
Outline Recap: Attribute Grammars 1 Circularity of Attribute Grammars 2 Attribute Dependency Graphs 3 Testing Attribute Grammars for Circularity 4 The Circularity Check 5 Compiler Construction Summer Semester 2014 13.13
Attribute Dependency Graphs I Goal: graphic representation of attribute dependencies Definition 13.4 (Production dependency graph) Let A = � G , E , V � ∈ AG with G = � N , Σ , P , S � . Every production π ∈ P determines the dependency graph D π := � Var π , → π � where the set of edges → π ⊆ Var π × Var π is given by x → π y iff y = f ( . . . , x , . . . ) ∈ E π . Corollary 13.5 The dependency graph of a production is acyclic (since → π ⊆ Out π × In π ). Compiler Construction Summer Semester 2014 13.14
Attribute Dependency Graphs II Example 13.6 (cf. Example 12.2) N → L . L : = ⇒ D N → L . L : 1 N d . 0 d . 0 = d . 1 + d . 3 p . 1 = 0 p . 3 = − l . 3 p . 1 . p . 3 L d . 1 l . 1 L d . 3 l . 3 L → LB : = ⇒ D N → LB : 2 p . 0 L d . 0 l . 0 d . 0 = d . 1 + d . 2 l . 0 = l . 1 + 1 p . 1 = p . 0 + 1 p . 1 p . 2 L d . 1 l . 1 B d . 2 p . 2 = p . 0 Compiler Construction Summer Semester 2014 13.15
Attribute Dependency Graphs III Just as the attribute equation system E t of a syntax tree t is obtained from the semantic rules of the contributing productions, the dependency graph of t is obtained by “glueing together” the dependency graphs of the productions. Definition 13.7 (Tree dependency graph) Let A = � G , E , V � ∈ AG , and let t be a syntax tree of G . The dependency graph of t is defined by D t := � Var t , → t � where the set of edges, → t ⊆ Var t × Var t , is given by x → t y iff y = f ( . . . , x , . . . ) ∈ E t . D t is called cyclic if there exists x ∈ Var t such that x → + t x . Corollary 13.8 An attribute grammar A = � G , E , V � ∈ AG is circular iff there exists a syntax tree t of G such that D t is cyclic. Compiler Construction Summer Semester 2014 13.16
Attribute Dependency Graphs IV Example 13.9 (cf. Example 12.2) (Acyclic) dependency graph of the syntax tree for 10.1 : k 0 : N d . k 0 p . k 1 k 1 : L d . k 1 l . k 1 k 7 : . p . k 8 k 8 : L d . k 8 l . k 8 p . k 2 k 2 : L d . k 2 l . k 2 p . k 5 k 5 : B d . k 5 p . k 9 k 9 : B d . k 9 p . k 3 k 3 : B d . k 3 k 6 : 0 k 10 : 1 k 4 : 1 Compiler Construction Summer Semester 2014 13.17
Outline Recap: Attribute Grammars 1 Circularity of Attribute Grammars 2 Attribute Dependency Graphs 3 Testing Attribute Grammars for Circularity 4 The Circularity Check 5 Compiler Construction Summer Semester 2014 13.18
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 13.10 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 13.19
Recommend
More recommend