cs 301
play

CS 301 Lecture 09 Context-free grammars Stephen Checkoway - PowerPoint PPT Presentation

CS 301 Lecture 09 Context-free grammars Stephen Checkoway February 14, 2018 1 / 22 Context-free grammars (CFGs) Method of generating (or describing) languages by giving rules to derive strings Rules contain Terminals symbols from an


  1. CS 301 Lecture 09 – Context-free grammars Stephen Checkoway February 14, 2018 1 / 22

  2. Context-free grammars (CFGs) Method of generating (or describing) languages by giving rules to derive strings Rules contain Terminals symbols from an alphabet (written in typewriter font) Variables which expand to sequences of terminals and variables (typically upper case letters) Rules have a variable on the left, an arrow ( → ), and a sequence of terminals and variables on the right Example: 2 / 22

  3. Context-free grammars (CFGs) Method of generating (or describing) languages by giving rules to derive strings Rules contain Terminals symbols from an alphabet (written in typewriter font) Variables which expand to sequences of terminals and variables (typically upper case letters) Rules have a variable on the left, an arrow ( → ), and a sequence of terminals and variables on the right Example: S → AB A → a A A → ε B → b B B → ε 2 / 22

  4. Context-free grammars (CFGs) Method of generating (or describing) languages by giving rules to derive strings Rules contain Terminals symbols from an alphabet (written in typewriter font) Variables which expand to sequences of terminals and variables (typically upper case letters) Rules have a variable on the left, an arrow ( → ), and a sequence of terminals and variables on the right Example: We often combine multiple rules with the same left-hand side using ∣ S → AB A → a A S → AB A → ε A → a A ∣ ε B → b B B → b B ∣ ε B → ε 2 / 22

  5. Deriving strings A CFG derives a string by starting with the start variable (usually the variable on the left in the first rule) and applying rules until no variables remain The CFG S → AB A → a A ∣ ε B → b B ∣ ε derives the following strings S ⇒ AB ⇒ εB ⇒ εε = ε S ⇒ AB ⇒ a AB ⇒ a εB ⇒ a εε = a S ⇒ AB ⇒ a AB ⇒ aa AB ⇒ aa εB ⇒ aab B ⇒ aab ε = aab ⋮ 3 / 22

  6. Derivations The order in which we replace a variable in a derivation with the RHS of a production rule doesn’t matter 1 In a left-most derivation, we replace the left-most variable in each step In a right-most derivation, we replace the right-most variable in each step 1 except in one case we’ll get to 4 / 22

  7. Left-most/right-most derivation example S → ST ∣ a T a T → S ∣ a T a ∣ b Left-most derivation of aabaaaba : S ⇒ 5 / 22

  8. Left-most/right-most derivation example S → ST ∣ a T a T → S ∣ a T a ∣ b Left-most derivation of aabaaaba : S ⇒ ST 5 / 22

  9. Left-most/right-most derivation example S → ST ∣ a T a T → S ∣ a T a ∣ b Left-most derivation of aabaaaba : S ⇒ ST ⇒ a T a T 5 / 22

  10. Left-most/right-most derivation example S → ST ∣ a T a T → S ∣ a T a ∣ b Left-most derivation of aabaaaba : S ⇒ ST ⇒ a T a T ⇒ aa T aa T 5 / 22

  11. Left-most/right-most derivation example S → ST ∣ a T a T → S ∣ a T a ∣ b Left-most derivation of aabaaaba : S ⇒ ST ⇒ a T a T ⇒ aa T aa T ⇒ aabaa T 5 / 22

  12. Left-most/right-most derivation example S → ST ∣ a T a T → S ∣ a T a ∣ b Left-most derivation of aabaaaba : S ⇒ ST ⇒ a T a T ⇒ aa T aa T ⇒ aabaa T ⇒ aabaaa T a 5 / 22

  13. Left-most/right-most derivation example S → ST ∣ a T a T → S ∣ a T a ∣ b Left-most derivation of aabaaaba : S ⇒ ST ⇒ a T a T ⇒ aa T aa T ⇒ aabaa T ⇒ aabaaa T a ⇒ aabaaaba 5 / 22

  14. Left-most/right-most derivation example S → ST ∣ a T a T → S ∣ a T a ∣ b Left-most derivation of aabaaaba : Right-most derivation of aabaaaba : S ⇒ ST S ⇒ ST ⇒ a T a T ⇒ S a T a ⇒ aa T aa T ⇒ S aba ⇒ aabaa T ⇒ a T aaba ⇒ aabaaa T a ⇒ aa T aaaba ⇒ aabaaaba ⇒ aabaaaba 5 / 22

  15. Another example The CFG S → a S b ∣ ε derives S ⇒ ε S ⇒ a S b ⇒ ab S ⇒ a S b ⇒ aa S bb ⇒ aabb ⋮ S ⇒ a S b ⇒ ⋯ ⇒ a n S b n ⇒ a n b n ⋮ The language of this CFG is { a n b n ∣ n ≥ 0 } 6 / 22

  16. Nested brackets Given the alphabet Σ = { ( , ) , [ , ] } , design a CFG that generates the language of properly nested brackets. • ε • () • [] • ([])[](()) • . . . 7 / 22

  17. Nested brackets Given the alphabet Σ = { ( , ) , [ , ] } , design a CFG that generates the language of properly nested brackets. • ε • () • [] • ([])[](()) • . . . S → P ∣ B ∣ SS ∣ ε P → ( S ) B → [ S ] 7 / 22

  18. More CFG examples Let Σ = { a , b } Construct a CFG for the languages over Σ • A = Σ ∗ • B = { w ∣ w contains at least three b s } • C = { w ∣ w starts and ends with different symbols } • D = { w ∣ the length of w is odd and the middle symbol is b } • E = { w ∣ w = w R } • F = ∅ 8 / 22

  19. Formally speaking A CFG is a 4-tuple G = ( V, Σ , R, S ) where • V is a finite set of variables (or nonterminals) • Σ is a finite set of terminals ( V ∩ Σ = ∅ ) • R is a finite set of production rules • S ∈ V is the start variable 9 / 22

  20. Formally speaking A CFG is a 4-tuple G = ( V, Σ , R, S ) where • V is a finite set of variables (or nonterminals) • Σ is a finite set of terminals ( V ∩ Σ = ∅ ) • R is a finite set of production rules • S ∈ V is the start variable If u, v, w ∈ ( Σ ∪ V ) ∗ and G has a rule A → v , then we say uAw yields uvw and write uAw ⇒ uvw 9 / 22

  21. Formally speaking A CFG is a 4-tuple G = ( V, Σ , R, S ) where • V is a finite set of variables (or nonterminals) • Σ is a finite set of terminals ( V ∩ Σ = ∅ ) • R is a finite set of production rules • S ∈ V is the start variable If u, v, w ∈ ( Σ ∪ V ) ∗ and G has a rule A → v , then we say uAw yields uvw and write uAw ⇒ uvw ∗ We say u derives v , written u ⇒ v to mean either u = v or there exist u 1 , u 2 , . . . , u n ∈ ( Σ ∪ V ) ∗ such that u = u 1 ⇒ u 2 ⇒ ⋯ ⇒ u n = v 9 / 22

  22. Formally speaking A CFG is a 4-tuple G = ( V, Σ , R, S ) where • V is a finite set of variables (or nonterminals) • Σ is a finite set of terminals ( V ∩ Σ = ∅ ) • R is a finite set of production rules • S ∈ V is the start variable If u, v, w ∈ ( Σ ∪ V ) ∗ and G has a rule A → v , then we say uAw yields uvw and write uAw ⇒ uvw ∗ We say u derives v , written u ⇒ v to mean either u = v or there exist u 1 , u 2 , . . . , u n ∈ ( Σ ∪ V ) ∗ such that u = u 1 ⇒ u 2 ⇒ ⋯ ⇒ u n = v The language of G is L ( G ) = { w ∣ w ∈ Σ ∗ and S ∗ ⇒ w } We say G generates a language A if L ( G ) = A 9 / 22

  23. Arithmetic expressions Given the alphabet Σ = { ( , ) , 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } , design a CFG that generates the language of arithmetic expressions • 37 • 8+22-8/6 • 10*(8-2) • . . . An expression can be a number or two expressions separated by an operator or a parenthesized expression A number is one or more digits 10 / 22

  24. Arithmetic expressions Given the alphabet Σ = { ( , ) , 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 } , design a CFG that generates the language of arithmetic expressions • 37 • 8+22-8/6 • 10*(8-2) • . . . An expression can be a number or two expressions separated by an operator or a parenthesized expression A number is one or more digits E → N ∣ E * E ∣ E / E ∣ E + E ∣ E - E ∣ ( E ) N → DN ∣ D D → 0 ∣ 1 ∣ 2 ∣ 3 ∣ 4 ∣ 5 ∣ 6 ∣ 7 ∣ 8 ∣ 9 10 / 22

  25. Parse trees give a way to visualize a derivation E → N ∣ E * E ∣ E / E ∣ E + E ∣ E - E ∣ ( E ) N → DN ∣ D D → 0 ∣ 1 ∣ 2 ∣ 3 ∣ 4 ∣ 5 ∣ 6 ∣ 7 ∣ 8 ∣ 9 Derivation Parse tree E E 11 / 22

  26. Parse trees give a way to visualize a derivation E → N ∣ E * E ∣ E / E ∣ E + E ∣ E - E ∣ ( E ) N → DN ∣ D D → 0 ∣ 1 ∣ 2 ∣ 3 ∣ 4 ∣ 5 ∣ 6 ∣ 7 ∣ 8 ∣ 9 Derivation Parse tree E ⇒ E + E E E + E 11 / 22

  27. Parse trees give a way to visualize a derivation E → N ∣ E * E ∣ E / E ∣ E + E ∣ E - E ∣ ( E ) N → DN ∣ D D → 0 ∣ 1 ∣ 2 ∣ 3 ∣ 4 ∣ 5 ∣ 6 ∣ 7 ∣ 8 ∣ 9 Derivation Parse tree E ⇒ E + E E ⇒ E + E * E E + E * E E 11 / 22

  28. Parse trees give a way to visualize a derivation E → N ∣ E * E ∣ E / E ∣ E + E ∣ E - E ∣ ( E ) N → DN ∣ D D → 0 ∣ 1 ∣ 2 ∣ 3 ∣ 4 ∣ 5 ∣ 6 ∣ 7 ∣ 8 ∣ 9 Derivation Parse tree E ⇒ E + E E ⇒ E + E * E E + E ⇒ N + E * E * N E E 11 / 22

  29. Parse trees give a way to visualize a derivation E → N ∣ E * E ∣ E / E ∣ E + E ∣ E - E ∣ ( E ) N → DN ∣ D D → 0 ∣ 1 ∣ 2 ∣ 3 ∣ 4 ∣ 5 ∣ 6 ∣ 7 ∣ 8 ∣ 9 Derivation Parse tree E ⇒ E + E E ⇒ E + E * E E + E ⇒ N + E * E ⇒ D + E * E * N E E D 11 / 22

  30. Parse trees give a way to visualize a derivation E → N ∣ E * E ∣ E / E ∣ E + E ∣ E - E ∣ ( E ) N → DN ∣ D D → 0 ∣ 1 ∣ 2 ∣ 3 ∣ 4 ∣ 5 ∣ 6 ∣ 7 ∣ 8 ∣ 9 Derivation Parse tree E ⇒ E + E E ⇒ E + E * E E + E ⇒ N + E * E ⇒ D + E * E * N E E ⇒ 3+ E * E D 3 11 / 22

  31. Parse trees give a way to visualize a derivation E → N ∣ E * E ∣ E / E ∣ E + E ∣ E - E ∣ ( E ) N → DN ∣ D D → 0 ∣ 1 ∣ 2 ∣ 3 ∣ 4 ∣ 5 ∣ 6 ∣ 7 ∣ 8 ∣ 9 Derivation Parse tree E ⇒ E + E E ⇒ E + E * E E + E ⇒ N + E * E ⇒ D + E * E * N E E ⇒ 3+ E * E ⇒ 3+ N * E D N 3 11 / 22

Recommend


More recommend