grammar flow analysis
play

Grammar Flow Analysis Wilhelm/Maurer: Compiler Design, Chapter 8 - PowerPoint PPT Presentation

Grammar Flow Analysis Grammar Flow Analysis Wilhelm/Maurer: Compiler Design, Chapter 8 Reinhard Wilhelm Universitt des Saarlandes wilhelm@cs.uni-sb.de 2. November 2009 Grammar Flow Analysis Notation Generic names for A , B , C ,


  1. Grammar Flow Analysis Grammar Flow Analysis – Wilhelm/Maurer: Compiler Design, Chapter 8 – Reinhard Wilhelm Universität des Saarlandes wilhelm@cs.uni-sb.de 2. November 2009

  2. Grammar Flow Analysis Notation Generic names for A , B , C , X , Y , Z Non-terminal symbols Terminal symbols a , b , c , . . . u , v , w , x , y , z Terminal strings Strings over V N ∪ V T α, β, γ, ϕ, ψ p , p ′ , p 1 , p 2 , . . . Productions ◮ Standard notation for production p = ( X 0 → u 0 X 1 u 1 . . . X n p u n p ) n p – Arity of p ◮ ( p , i ) – Position i in production p ( 0 ≤ i ≤ n p ) ◮ p [ i ] stands for X i , ( 0 ≤ i ≤ n p ) , ◮ X occurs at position i – p [ i ] = X

  3. Grammar Flow Analysis Reachability and Productivity Non-terminal A is reachable: iff there exist ϕ 1 , ϕ 2 ∈ V T ∪ V N such that ∗ ⇒ ϕ 1 A ϕ 2 S = ∗ productive: iff there exists w ∈ V ∗ T , A = ⇒ w These definitions are useless for tests; they involve quantifications over infinite sets.

  4. Grammar Flow Analysis A two level Definition 1. A non-terminal is reachable through its occurrence ( p , i ) iff p [ 0 ] is reachable, 2. A non-terminal is reachable iff it is reachable through at least one of its occurrences, 3. S ′ is reachable. 1. A non-terminal A is productive through production p iff A = p [ 0 ] and all non-terminals p [ i ]( 1 ≤ i ≤ n p ) are productive. 2. A non-terminal is productive iff it is productive through at least one of its alternatives. ◮ Reachability and productivity for a grammar given by a (recursive) system of equations. ◮ Least solution wanted to eliminate as many useless non-terminals as possible.

  5. Grammar Flow Analysis Typical Two Level Simultaneous Recursion Productivity: 1. dependence of property of left side non-terminal on right side non-terminals, 2. combination of the information from the different alternatives for a non-terminal. Reachability: 1. dependence of property of occurrences of non-terminals on the right side on the property of the left side non-terminal, 2. combination of the information from the different occurrences for a non-terminal, 3. the initial property. In the specification 1. given by transfer functions 2. given by combination functions

  6. Grammar Flow Analysis Schema for the Computation ◮ Grammar Flow Analysis (GFA) computes a property function I : V N → D where D is some domain of information for non-terminals, mostly properties of sets of trees, ◮ Productivity computed by a bottom-up Grammar Flow Analysis (bottom-up GFA) ◮ Reachability computed by a top-down Grammar Flow Analysis (top-down GFA)

  7. Grammar Flow Analysis Trees, Subtrees, Tree Fragments S S X X X Parse tree Subtree upper treefragment for X for X X reachable: Set of upper tree fragments for X not empty, X productive: Set of subtrees for X not empty.

  8. Grammar Flow Analysis Bottom-up GFA Given a cfg G . A bottom-up GFA-problem for G and a property function I : D: a domain D ↑ , T: transfer functions F p ↑ : D ↑ n p → D ↑ for each p ∈ P , C: a combination function ∇↑ : 2 D ↑ → D ↑ . This defines a system of equations for G and I : I ( X )= ∇↑{ F p ↑ ( I ( p [ 1 ]) , . . . , I ( p [ n p ])) | p [ 0 ] = X } ∀ X ∈ V N ( I ↑ )

  9. Grammar Flow Analysis Top-down GFA Given a cfg G . A top down – GFA-problem for G and a property function I : D: a domain D ↓ ; T: n p transfer functions F p , i ↓ : D ↓ → D ↓ , 1 ≤ i ≤ n p , for each production p ∈ P , C: a combination function ∇↓ : 2 D ↓ → D ↓ , S: a value I 0 for S under the function I . A top-down GFA-problem defines a system of equations for G and I I ( S ) = I 0 I ( p , i )= F p , i ↓ ( I ( p [ 0 ])) for all p ∈ P , 1 ≤ i ≤ n p ( I ↓ ) I ( X ) = ∇↓ { I ( p , i ) | p [ i ] = X } , for all X ∈ V N − { S }

  10. Grammar Flow Analysis Recursive System of Equations Systems like ( I ↑ ) and ( I ↓ ) are in general recursive. Questions: Do they have ◮ solutions? ◮ unique solutions?

  11. Grammar Flow Analysis They do have solutions if ◮ the domain ◮ is partially ordered by some relation ⊑ , ◮ has a uniquely defined smallest element, ⊥ , ◮ has a least upper bound, d 1 ⊔ d 2 , for each two elements d 1 , d 2 ◮ and has only finitely ascending chains, and ◮ the transfer and the combination functions are monotonic. Our domains are finite, all functions are monotonic.

  12. Grammar Flow Analysis Fixpoint Iteration ◮ Solutions are fixpoints of a function I : [ V N → D ] → [ V N → D ] . ◮ Computed iteratively starting with ⊥ ⊥ , the function which maps all non-terminals to ⊥ . ◮ Apply transfer functions and combination functions until nothing changes. We always compute least fixpoints.

  13. Grammar Flow Analysis Productivity Revisited D ↑ { false ⊑ true } true for productive F p ↑ � ( true for n p = 0) � ∇↑ ( false for non-terminals without productions) Domain: D ↑ satisfies the conditions, transfer functions: conjunctions are monotonic, combination function: disjunction is monotonic. Resulting system of equations: = � { � n p Pr ( X ) i = 1 Pr ( p [ i ]) | p [ 0 ] = X } for all X ∈ V N ( Pr )

  14. Grammar Flow Analysis Example: Productivity Given the following grammar: S ′   → S     → S aX       G = ( { S ′ , S , X , Y , Z } , { a , b } , , S ′ ) X → bS | aYbY → ba | aZ Y         Z → aZX   Resulting system of equations: Fixpoint iteration S X Y Z Pr ( S ) = Pr ( X ) false false false false Pr ( X ) = Pr ( S ) ∨ Pr ( Y ) Pr ( Y ) = true ∨ Pr ( Z ) = true Pr ( Z ) = Pr ( Z ) ∧ Pr ( X )

  15. Grammar Flow Analysis Reachability Revisited D ↓ false ⊑ { true } true for reachable F p , i ↓ id identity mapping ∇↓ � Boolean Or ( false , if there is no occ. of the non-terminal) I 0 true Domain: D ↓ satisfies the conditions, transfer functions: identity is monotonic, combination function: disjunction is monotonic. Resulting system of equations for reachability: Re ( S ) = true ( Re ) Re ( X )= � { Re ( p [ 0 ]) | p [ i ] = X , 1 ≤ i ≤ n p } ∀ X � = S

  16. Grammar Flow Analysis Example: Reachability Given the grammar G = ( { S , U , V , X , Y , Z } , { a , b , c , d } , The equations:  S → Y    Re ( S ) = true   Y → YZ | Ya | b       Re ( U ) = false   U → V   , S ) Re ( U ) ∨ Re ( V ) Re ( V ) = X → c     Re ( X ) = Re ( Z ) V → Vd | d         Re ( Y ) = Re ( S ) ∨ Re ( Y ) Z → ZX   Re ( Z ) = Re ( Y ) ∨ Re ( Z ) Fixpoint iteration: S U V X Y Z true false false false false false

  17. Grammar Flow Analysis First and Follow Sets Parser generators need precomputed information about sets of ◮ prefixes of words for non-terminals (words that can begin words for non-terminals) ◮ followers of non-terminals (words which can follow a non-terminal). Strategic use: Removing non-determinism from expand moves of the P G These sets can be computed by GFA.

  18. Grammar Flow Analysis Another Grammar for Arithmetic Expressions Left-factored grammar G 2 , i.e. left recursion removed. S → E E → TE ′ E generates T with a continuation E ′ E ′ → + E | ǫ E ′ generates possibly empty sequence of + T s T → FT ′ T generates F with a continuation T ′ T ′ → ∗ T | ǫ T ′ generates possibly empty sequence of ∗ F s F → id | ( E ) G 2 defines the same language as G 0 und G 1 .

  19. Grammar Flow Analysis The FIRST 1 Sets ◮ A production N → α is applicable for symbols that “begin” α ◮ Example: Arithmetic Expressions, Grammar G 2 ◮ The production F → id is applied when the current symbol is id ◮ The production F → ( E ) is applied when the current symbol is ( ◮ The production T → F is applied when the current symbol is id or ( ◮ Formal definition: ∗ FIRST 1 ( α ) = { a ∈ V T |∃ γ : α = ⇒ a γ }

  20. Grammar Flow Analysis The FOLLOW 1 Sets ◮ A production N → ǫ is applicable for symbols that “can follow” N in some derivation ◮ Example: Arithmetic Expressions, Grammar G 2 ◮ The production E ′ → ǫ is applied for symbols # and ) ◮ The production T ′ → ǫ is applied for symbols # , ) and + ◮ Formal definition: ∗ FOLLOW 1 ( N ) = { a ∈ V T |∃ α, γ : S ⇒ α Na γ } =

  21. Grammar Flow Analysis Definitions Let k ≥ 1 k - prefix of a word w = a 1 . . . a n � a 1 . . . a n n ≤ k if k : w = a 1 . . . a k otherwise k - concatenation ⊕ k : V ∗ × V ∗ → V ≤ k , defined by u ⊕ k v = k : uv extended to languages k : L = { k : w | w ∈ L } L 1 ⊕ k L 2 = { x ⊕ k y | x ∈ L 1 , y ∈ L 2 } . V ≤ k = � k i = 1 V i set of words of length at most k . . . V ≤ k T # = V ≤ k ∪ V k − 1 { # } . . . possibly terminated by # . T T

  22. Grammar Flow Analysis FIRST k and FOLLOW k FIRST k : ( V N ∪ V T ) ∗ → 2 V ≤ k where T ∗ FIRST k ( α ) = { k : u | α = ⇒ u } X set of k –prefixes of terminal words for α . ∈ FIRST k ( X ) ∈ FOLLOW k ( X ) FOLLOW k : V N → 2 V ≤ k T # where ∗ FOLLOW k ( X ) = { w | S ⇒ β X γ and w ∈ FIRST k ( γ ) } = set of k –prefixes of terminal words that may immediately follow X .

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