parse trees 2it70 finite automata and process theory
play

Parse Trees 2IT70 Finite Automata and Process Theory Technische - PowerPoint PPT Presentation

Parse Trees 2IT70 Finite Automata and Process Theory Technische Universiteit Eindhoven May 26, 2014 Identifying production sequences parentheses grammar S SS ( S ) several production sequences for string ()(()) S G SS G (


  1. Parse Trees 2IT70 Finite Automata and Process Theory Technische Universiteit Eindhoven May 26, 2014

  2. Identifying production sequences parentheses grammar S → ε ∣ SS ∣ ( S ) several production sequences for string ()(()) S ⇒ G SS ⇒ G ( S ) S ⇒ G () S ⇒ G ()( S ) ⇒ G ()(( S )) ⇒ G ()(()) S ⇒ G SS ⇒ G ( S ) S ⇒ G ( S )( S ) ⇒ G ()( S ) ⇒ G ()(( S )) ⇒ G ()(()) swapping independent productions 2 IT70 (2014) Parse Trees 2 / 18

  3. Identifying production sequences (cont.) S S S ( ) ( ) S S ( ) ε S ε 2 IT70 (2014) Parse Trees 3 / 18

  4. Identifying production sequences (once more) S S S ( ) ( ) S S ( ) ε S ε 2 IT70 (2014) Parse Trees 4 / 18

  5. Yield of a parse tree CFG G = ( V , T , R , S ) set PT G of all parse trees of G [ X ] single node tree, X ∈ V ∪ T [ A → ε ] two node tree, root A , leaf ε for rule A → ε ∈ R [ A → PT 1 , PT 2 ,... , PT k ] rule A → X 1 ⋯ X k ∈ R parse trees PT i with root X i yield function yield ∶ PT G → ( V ∪ T ) ∗ yield ([ X ]) = X yield ([ A → ε ]) = ε yield ([ A → PT 1 ,... , PT k ]) = yield ( PT 1 ) ⋅ ... ⋅ yield ( PT k ) parse tree PT is complete if yield ( PT ) ∈ T ∗ 2 IT70 (2014) Parse Trees 5 / 18

  6. A parse tree with yield ()(()) S S S ( ) ( ) S S ( ) ε S ε 2 IT70 (2014) Parse Trees 6 / 18

  7. Another parse tree CFG S → AB A → ε ∣ aaA B → ε ∣ Bb S A B a a A B b ε ε parse tree with yield aab 2 IT70 (2014) Parse Trees 7 / 18

  8. Parsing CFG G with rules S → ε ∣ aSb ∣ bSa ∣ SS aabb ∈ L ( G ) ? 1 2 3 4 S S S S S ε a a S b b S S S w 1 w 2 ε w ∈ { a , b } ∗ awb bwa 2 IT70 (2014) Parse Trees 8 / 18

  9. Parsing CFG G with rules S → ε ∣ aSb ∣ bSa ∣ SS aabb ∈ L ( G ) ? 2 . 1 2 . 2 2 . 3 2 . 4 S S S S a a a a S b S b S b S b ε a a S b b S S S aw 1 w 2 b ab aawbb abwab 2 IT70 (2014) Parse Trees 9 / 18

  10. Parsing CFG G with rules S → ε ∣ aSb ∣ bSa ∣ SS aabb ∈ L ( G ) ? 2 . 2 . 1 2 . 2 . 2 2 . 2 . 3 2 . 2 . 4 S S S S a a a a S b S b S b S b a ε a a a S b S b S b S b ε a a S b b S S S aabb aaawbbb aabwabb aaw 1 w 2 bb Thus aabb ∈ L ( G ) 2 IT70 (2014) Parse Trees 10 / 18

  11. Clicker question L91 parsing takes at most 2 ∣ w ∣ − 1 rounds if no summand ε summands have at least one terminal or at least two variables With the parsing procedure and restrictions above, we have that A. Parsing is linear in the length of the string B. Parsing is quadratic in the length of the string C. Parsing is exponential in the length of the string D. Can’t tell 2 IT70 (2014) Parse Trees 11 / 18

  12. generated strings of terminals vs. yields of parse trees CFG G = ( V , T , R , S ) theorem ∗ A ⇒ G w implies w = yield ( PT ) for parse tree PT with root A by induction on n : proof A ⇒ n ∃ PT ∈ PT G ( A ) ∶ w = yield ( PT ) G w implies ⊠ for all A ∈ V and w ∈ T ∗ thus L ( G ) = { w ∈ T ∗ ∣ S ⇒ ∗ G w } ⊆ { yield ( PT ) ∣ PT complete parse tree of G , root S } 2 IT70 (2014) Parse Trees 12 / 18

  13. Clicker question L92 ℓ ⇒ ∗ Suppose X G β for a CFG G . Then it holds that ℓ G αβγ for all α ∈ T ∗ and γ ∈ T ∗ A. α X γ ⇒ ∗ ℓ G αβγ for all α ∈ T ∗ and γ ∈ ( V ∪ T ) ∗ B. α X γ ⇒ ∗ ℓ G αβγ for all α ∈ ( V ∪ T ) ∗ and γ ∈ T ∗ C. α X γ ⇒ ∗ ℓ G αβγ for all α ∈ ( V ∪ T ) ∗ and γ ∈ ( V ∪ T ) ∗ ⇒ ∗ D. α X γ E. Can’t tell 2 IT70 (2014) Parse Trees 13 / 18

  14. From parse tree to leftmost production sequence CFG G theorem ℓ for parse tree PT , root A and yield w : A ⇒ ∗ G w ⊠ induction on the height of the parse tree PT proof thus { yield ( PT ) ∣ PT complete parse tree of G , root S } ⊆ { w ∈ T ∗ ∣ S ⇒ ∗ G w } = L ( G ) 2 IT70 (2014) Parse Trees 14 / 18

  15. Different parse trees (harmless) S S S S a ε a S b S b a ε a ε S b S b ε ε aabb aabb ambiguous grammar S → ε ∣ aSb ∣ SS 2 IT70 (2014) Parse Trees 15 / 18

  16. Different parse trees (harmful) E → I ∣ E ‘+’ E ∣ E ‘*’ E ∣ ‘(’ E ‘)’ I → a ∣ b ∣ c E E E ∗ E E + E + ∗ I E E E E E a c I I I I c a b b a*b+c a*b+c ambigious grammar 2 IT70 (2014) Parse Trees 16 / 18

  17. Different parse trees (harmful, cont.) E → I ∣ E ‘+’ E ∣ E ‘*’ E ∣ ‘(’ E ‘)’ I → a ∣ b ∣ c 14 10 ∗ + 2 7 6 4 ∗ + 2 3 4 2 3 4 a c 3 4 2 3 c a b b 2*3+4? wrong 2*3+4? right ambigious grammar 2 IT70 (2014) Parse Trees 17 / 18

  18. Disambiguation E + E T T F E → T ∣ E ‘+’ T T → F ∣ T ‘*’ F ∗ T F I F → I ∣ ‘(’ E ‘)’ I → a ∣ b ∣ c c F I I b a a*b+c syntactic categories: expression, term, factor, identifier 2 IT70 (2014) Parse Trees 18 / 18

Recommend


More recommend