inf2080
play

INF2080 Context-Free Langugaes Daniel Lupp Universitetet i Oslo - PowerPoint PPT Presentation

INF2080 Context-Free Langugaes Daniel Lupp Universitetet i Oslo 1st February 2018 Department of University of Informatics Oslo INF2080 Lecture :: 1st February 1 / 37 Repetition Weve looked at one of the simpler computational models:


  1. Pumping Lemma revisited L = { ab n c n | n ≥ 0 } ∪ { a k w | k � = 1 , and w ∈ Σ ∗ doesn’t start with a } Assume s = a k w 1 w 2 · · · w n , for k � = 1 and w ∈ Σ ∗ not starting with a , where n , k are such that | s | ≥ p . if k = 2, choose x = ε, y = aa , z = w 1 w 2 · · · w n . Then | y | > 0 and | xy | ≤ p . INF2080 Lecture :: 1st February 9 / 37

  2. Pumping Lemma revisited L = { ab n c n | n ≥ 0 } ∪ { a k w | k � = 1 , and w ∈ Σ ∗ doesn’t start with a } Assume s = a k w 1 w 2 · · · w n , for k � = 1 and w ∈ Σ ∗ not starting with a , where n , k are such that | s | ≥ p . if k = 2, choose x = ε, y = aa , z = w 1 w 2 · · · w n . Then | y | > 0 and | xy | ≤ p . The string xz is in Σ ∗ and doesn’t start with a . INF2080 Lecture :: 1st February 9 / 37

  3. Pumping Lemma revisited L = { ab n c n | n ≥ 0 } ∪ { a k w | k � = 1 , and w ∈ Σ ∗ doesn’t start with a } Assume s = a k w 1 w 2 · · · w n , for k � = 1 and w ∈ Σ ∗ not starting with a , where n , k are such that | s | ≥ p . if k = 2, choose x = ε, y = aa , z = w 1 w 2 · · · w n . Then | y | > 0 and | xy | ≤ p . The string xz is in Σ ∗ and doesn’t start with a . xz ∈ L INF2080 Lecture :: 1st February 9 / 37

  4. Pumping Lemma revisited L = { ab n c n | n ≥ 0 } ∪ { a k w | k � = 1 , and w ∈ Σ ∗ doesn’t start with a } Assume s = a k w 1 w 2 · · · w n , for k � = 1 and w ∈ Σ ∗ not starting with a , where n , k are such that | s | ≥ p . if k = 2, choose x = ε, y = aa , z = w 1 w 2 · · · w n . Then | y | > 0 and | xy | ≤ p . The string xz is in Σ ∗ and doesn’t start with a . xz ∈ L The string xy i z for i ≥ 1 starts with 2 or more a ’s, followed by a word w ∈ Σ ∗ that does not start with an a . INF2080 Lecture :: 1st February 9 / 37

  5. Pumping Lemma revisited L = { ab n c n | n ≥ 0 } ∪ { a k w | k � = 1 , and w ∈ Σ ∗ doesn’t start with a } Assume s = a k w 1 w 2 · · · w n , for k � = 1 and w ∈ Σ ∗ not starting with a , where n , k are such that | s | ≥ p . if k = 2, choose x = ε, y = aa , z = w 1 w 2 · · · w n . Then | y | > 0 and | xy | ≤ p . The string xz is in Σ ∗ and doesn’t start with a . xz ∈ L The string xy i z for i ≥ 1 starts with 2 or more a ’s, followed by a word w ∈ Σ ∗ that does not start with an a . ⇒ xy i z ∈ L . INF2080 Lecture :: 1st February 9 / 37

  6. Pumping Lemma revisited L = { ab n c n | n ≥ 0 } ∪ { a k w | k � = 1 , and w ∈ Σ ∗ doesn’t start with a } Assume s = a k w 1 w 2 · · · w n , for k � = 1 and w ∈ Σ ∗ not starting with a , where n , k are such that | s | ≥ p . if k ≥ 3, choose x = ε, y = a , z = a k − 1 w 2 · · · w n . Then | y | > 0 and | xy | ≤ p . INF2080 Lecture :: 1st February 10 / 37

  7. Pumping Lemma revisited L = { ab n c n | n ≥ 0 } ∪ { a k w | k � = 1 , and w ∈ Σ ∗ doesn’t start with a } Assume s = a k w 1 w 2 · · · w n , for k � = 1 and w ∈ Σ ∗ not starting with a , where n , k are such that | s | ≥ p . if k ≥ 3, choose x = ε, y = a , z = a k − 1 w 2 · · · w n . Then | y | > 0 and | xy | ≤ p . The string xz is of the form a k − 1 w , where w ∈ Σ ∗ and doesn’t start with a . INF2080 Lecture :: 1st February 10 / 37

  8. Pumping Lemma revisited L = { ab n c n | n ≥ 0 } ∪ { a k w | k � = 1 , and w ∈ Σ ∗ doesn’t start with a } Assume s = a k w 1 w 2 · · · w n , for k � = 1 and w ∈ Σ ∗ not starting with a , where n , k are such that | s | ≥ p . if k ≥ 3, choose x = ε, y = a , z = a k − 1 w 2 · · · w n . Then | y | > 0 and | xy | ≤ p . The string xz is of the form a k − 1 w , where w ∈ Σ ∗ and doesn’t start with a . xz ∈ L INF2080 Lecture :: 1st February 10 / 37

  9. Pumping Lemma revisited L = { ab n c n | n ≥ 0 } ∪ { a k w | k � = 1 , and w ∈ Σ ∗ doesn’t start with a } Assume s = a k w 1 w 2 · · · w n , for k � = 1 and w ∈ Σ ∗ not starting with a , where n , k are such that | s | ≥ p . if k ≥ 3, choose x = ε, y = a , z = a k − 1 w 2 · · · w n . Then | y | > 0 and | xy | ≤ p . The string xz is of the form a k − 1 w , where w ∈ Σ ∗ and doesn’t start with a . xz ∈ L The string xy i z for i ≥ 1 is of the form a k + i − 1 w where w ∈ Σ ∗ that does not start with an a . INF2080 Lecture :: 1st February 10 / 37

  10. Pumping Lemma revisited L = { ab n c n | n ≥ 0 } ∪ { a k w | k � = 1 , and w ∈ Σ ∗ doesn’t start with a } Assume s = a k w 1 w 2 · · · w n , for k � = 1 and w ∈ Σ ∗ not starting with a , where n , k are such that | s | ≥ p . if k ≥ 3, choose x = ε, y = a , z = a k − 1 w 2 · · · w n . Then | y | > 0 and | xy | ≤ p . The string xz is of the form a k − 1 w , where w ∈ Σ ∗ and doesn’t start with a . xz ∈ L The string xy i z for i ≥ 1 is of the form a k + i − 1 w where w ∈ Σ ∗ that does not start with an a . ⇒ xy i z ∈ L . INF2080 Lecture :: 1st February 10 / 37

  11. Pumping Lemma revisited L = { ab n c n | n ≥ 0 } ∪ { a k w | k � = 1 , and w ∈ Σ ∗ doesn’t start with a } can be pumped!! Does that mean L is regular? INF2080 Lecture :: 1st February 11 / 37

  12. Pumping Lemma revisited L = { ab n c n | n ≥ 0 } ∪ { a k w | k � = 1 , and w ∈ Σ ∗ doesn’t start with a } can be pumped!! Does that mean L is regular? If L is regular, then so is L ∩ ab Σ ∗ (recall: regular languages are closed under intersection). INF2080 Lecture :: 1st February 11 / 37

  13. Pumping Lemma revisited L = { ab n c n | n ≥ 0 } ∪ { a k w | k � = 1 , and w ∈ Σ ∗ doesn’t start with a } can be pumped!! Does that mean L is regular? If L is regular, then so is L ∩ ab Σ ∗ (recall: regular languages are closed under intersection). L ∩ ab Σ ∗ = { ab n c n | n ≥ 1 } INF2080 Lecture :: 1st February 11 / 37

  14. Pumping Lemma revisited L = { ab n c n | n ≥ 0 } ∪ { a k w | k � = 1 , and w ∈ Σ ∗ doesn’t start with a } can be pumped!! Does that mean L is regular? If L is regular, then so is L ∩ ab Σ ∗ (recall: regular languages are closed under intersection). L ∩ ab Σ ∗ = { ab n c n | n ≥ 1 } Exercise: show that this language is nonregular! (analogous to proof for a n b n ) So L is nonregular...is this a counter-example to the pumping lemma? INF2080 Lecture :: 1st February 11 / 37

  15. Pumping Lemma revisited L = { ab n c n | n ≥ 0 } ∪ { a k w | k � = 1 , and w ∈ Σ ∗ doesn’t start with a } can be pumped!! Does that mean L is regular? If L is regular, then so is L ∩ ab Σ ∗ (recall: regular languages are closed under intersection). L ∩ ab Σ ∗ = { ab n c n | n ≥ 1 } Exercise: show that this language is nonregular! (analogous to proof for a n b n ) So L is nonregular...is this a counter-example to the pumping lemma? No, pumping lemma is not an if and only if statement! INF2080 Lecture :: 1st February 11 / 37

  16. Context-Free Grammars Today: Context-free grammars and languages INF2080 Lecture :: 1st February 12 / 37

  17. Context-Free Grammars Today: Context-free grammars and languages grammars describe the syntax of a language; they try to describe the relationship of all the parts to one another, such as placement of nouns/verbs in sentences INF2080 Lecture :: 1st February 12 / 37

  18. Context-Free Grammars Today: Context-free grammars and languages grammars describe the syntax of a language; they try to describe the relationship of all the parts to one another, such as placement of nouns/verbs in sentences useful for programming languages, specifically compilers and parsers: if the grammar of a programming language is available, parsing is very straightforward. INF2080 Lecture :: 1st February 12 / 37

  19. Context-Free Grammars First example: S → aSb S → ε INF2080 Lecture :: 1st February 13 / 37

  20. Context-Free Grammars First example: S → aSb S → ε Every grammar consists of rules , which are a pair consisting of one variable (to the left of → ) and a string of variables and symbols (to the right of → ) INF2080 Lecture :: 1st February 13 / 37

  21. Context-Free Grammars First example: S → aSb S → ε Every grammar consists of rules , which are a pair consisting of one variable (to the left of → ) and a string of variables and symbols (to the right of → ) Every grammar contains a start variable (above: variable S ). Common convention: the first listed variable is the start variable (if you choose a different start variable, you must specify!). INF2080 Lecture :: 1st February 13 / 37

  22. Context-Free Grammars First example: S → aSb S → ε Every grammar consists of rules , which are a pair consisting of one variable (to the left of → ) and a string of variables and symbols (to the right of → ) Every grammar contains a start variable (above: variable S ). Common convention: the first listed variable is the start variable (if you choose a different start variable, you must specify!). Words are generated by starting with the start variable and recursively replacing variables with the righthand side of a rule. S � aSb � aaSbb � aa ε bb � aabb INF2080 Lecture :: 1st February 13 / 37

  23. Parse Trees Derivations of the form S � aSb � aaSbb � aa ε bb � aabb can also be encoded as a parse tree: S a S b b S b ε INF2080 Lecture :: 1st February 14 / 37

  24. Context-Free Grammars Second example: S → aSa S → bSb S → cSc S → ε INF2080 Lecture :: 1st February 15 / 37

  25. Context-Free Grammars Second example: S → aSa S → bSb S → cSc S → ε To simplify notation, you can summarize multiple rules into one line: S → aSa | bSb | cSc | ε. INF2080 Lecture :: 1st February 15 / 37

  26. Context-Free Grammars Second example: S → aSa S → bSb S → cSc S → ε To simplify notation, you can summarize multiple rules into one line: S → aSa | bSb | cSc | ε. The symbol | takes on the meaning of “or.” INF2080 Lecture :: 1st February 15 / 37

  27. Context-Free Grammars Second example: S → aSa S → bSb S → cSc S → ε To simplify notation, you can summarize multiple rules into one line: S → aSa | bSb | cSc | ε. The symbol | takes on the meaning of “or.” → palindromes of even length over { a , b , c } . INF2080 Lecture :: 1st February 15 / 37

  28. Context-Free Grammar Definition (Context-Free Grammar) A context-free grammar is a 4-tuple ( V , Σ , R , S ) where 1 V is a finite set of variables 2 Σ is a finite set disjoint from V of terminals 3 R is a finite set of rules , each consisting of a variable and of a string of variables and terminals 4 and S is the start variable INF2080 Lecture :: 1st February 16 / 37

  29. Context-Free Grammar Definition (Context-Free Grammar) A context-free grammar is a 4-tuple ( V , Σ , R , S ) where 1 V is a finite set of variables 2 Σ is a finite set disjoint from V of terminals 3 R is a finite set of rules , each consisting of a variable and of a string of variables and terminals 4 and S is the start variable We call L ( G ) the language generated by a context-free grammar. A language is called a context-free language if it is generated by a context-free grammar. INF2080 Lecture :: 1st February 16 / 37

  30. Context-Free Grammar So what can context-free grammars (CFGs) express? INF2080 Lecture :: 1st February 17 / 37

  31. Context-Free Grammar So what can context-free grammars (CFGs) express? Regular languages? INF2080 Lecture :: 1st February 17 / 37

  32. Context-Free Grammar So what can context-free grammars (CFGs) express? Regular languages? Is the class of context-free languages closed under union/intersection/concatanation/complement/Kleene star? INF2080 Lecture :: 1st February 17 / 37

  33. Context-Free Grammar So what can context-free grammars (CFGs) express? Regular languages? Is the class of context-free languages closed under union/intersection/concatanation/complement/Kleene star? Regular languages could be modelled by an automaton with finite memory...what about context-free languages? INF2080 Lecture :: 1st February 17 / 37

  34. Context-Free Grammar So what can context-free grammars (CFGs) express? Regular languages? Is the class of context-free languages closed under union/intersection/concatanation/complement/Kleene star? Regular languages could be modelled by an automaton with finite memory...what about context-free languages? Answers to these over the course of this and next lecture (and group sessions) INF2080 Lecture :: 1st February 17 / 37

  35. RLs and CFLs Can regular languages be described using context-free grammars? INF2080 Lecture :: 1st February 18 / 37

  36. RLs and CFLs Can regular languages be described using context-free grammars? Given a RL L , there exists some DFA ( Q , Σ , δ, q 0 , F ) that accepts L INF2080 Lecture :: 1st February 18 / 37

  37. RLs and CFLs Can regular languages be described using context-free grammars? Given a RL L , there exists some DFA ( Q , Σ , δ, q 0 , F ) that accepts L What if we encode traversing the DFA into grammar rules, i.e., for each transition δ ( q 1 , a ) = q 2 we create a rule Q 1 → aQ 2 INF2080 Lecture :: 1st February 18 / 37

  38. RLs and CFLs Can regular languages be described using context-free grammars? Given a RL L , there exists some DFA ( Q , Σ , δ, q 0 , F ) that accepts L What if we encode traversing the DFA into grammar rules, i.e., for each transition δ ( q 1 , a ) = q 2 we create a rule Q 1 → aQ 2 the variables of our grammar correspond to the states in Q , with Q 0 as the start variable. INF2080 Lecture :: 1st February 18 / 37

  39. RLs and CFLs Can regular languages be described using context-free grammars? Given a RL L , there exists some DFA ( Q , Σ , δ, q 0 , F ) that accepts L What if we encode traversing the DFA into grammar rules, i.e., for each transition δ ( q 1 , a ) = q 2 we create a rule Q 1 → aQ 2 the variables of our grammar correspond to the states in Q , with Q 0 as the start variable. How do we deal with accept states? INF2080 Lecture :: 1st February 18 / 37

  40. RLs and CFLs Can regular languages be described using context-free grammars? Given a RL L , there exists some DFA ( Q , Σ , δ, q 0 , F ) that accepts L What if we encode traversing the DFA into grammar rules, i.e., for each transition δ ( q 1 , a ) = q 2 we create a rule Q 1 → aQ 2 the variables of our grammar correspond to the states in Q , with Q 0 as the start variable. How do we deal with accept states? � for each q i ∈ F , add rule Q i → ε INF2080 Lecture :: 1st February 18 / 37

  41. RLs and CFLs Can regular languages be described using context-free grammars? Given a RL L , there exists some DFA ( Q , Σ , δ, q 0 , F ) that accepts L What if we encode traversing the DFA into grammar rules, i.e., for each transition δ ( q 1 , a ) = q 2 we create a rule Q 1 → aQ 2 the variables of our grammar correspond to the states in Q , with Q 0 as the start variable. How do we deal with accept states? � for each q i ∈ F , add rule Q i → ε Theorem Every regular language is context-free. INF2080 Lecture :: 1st February 18 / 37

  42. Properties of CFLs Closure under union/concatanation/Kleene star? INF2080 Lecture :: 1st February 19 / 37

  43. Properties of CFLs Closure under union/concatanation/Kleene star? Let G 1 = ( V 1 , Σ 1 , R 1 , S 1 ) and G 2 = ( V 2 , Σ 2 , R 2 , S 2 ) be two grammars that generate L 1 , L 2 respectively. INF2080 Lecture :: 1st February 19 / 37

  44. Properties of CFLs Closure under union/concatanation/Kleene star? Let G 1 = ( V 1 , Σ 1 , R 1 , S 1 ) and G 2 = ( V 2 , Σ 2 , R 2 , S 2 ) be two grammars that generate L 1 , L 2 respectively. Union: create grammar G L 1 ∪ L 2 that generates all words w ∈ L 1 ∪ L 2 . INF2080 Lecture :: 1st February 19 / 37

  45. Properties of CFLs Closure under union/concatanation/Kleene star? Let G 1 = ( V 1 , Σ 1 , R 1 , S 1 ) and G 2 = ( V 2 , Σ 2 , R 2 , S 2 ) be two grammars that generate L 1 , L 2 respectively. Union: create grammar G L 1 ∪ L 2 that generates all words w ∈ L 1 ∪ L 2 . Create new start variable S . G L 1 ∪ L 2 = ( V , Σ , R , S ) where V = V 1 ∪ V 2 ∪ { S } , Σ = Σ 1 ∪ Σ 2 , and R = R 1 ∪ R 2 ∪ { S → S 1 | S 2 } . INF2080 Lecture :: 1st February 19 / 37

  46. CFL Union: Example S 1 → aS 1 b | ε ∪ S 2 → aS 2 a | bS 2 b | cS 2 c | ε INF2080 Lecture :: 1st February 20 / 37

  47. CFL Union: Example S 1 → aS 1 b | ε ∪ S 2 → aS 2 a | bS 2 b | cS 2 c | ε S → S 1 | S 2 S 1 → aS 1 b | ε S 2 → aS 2 a | bS 2 b | cS 2 c | ε INF2080 Lecture :: 1st February 20 / 37

  48. Properties of CFLs: Concatanation Let G 1 = ( V 1 , Σ 1 , R 1 , S 1 ) and G 2 = ( V 2 , Σ 2 , R 2 , S 2 ) be two grammars that generate L 1 , L 2 respectively. Concatanation: create grammar G L 1 L 2 = ( V , Σ , R , S ) that accepts all words w = w 1 w 2 , where w 1 ∈ L 1 and w 2 ∈ L 2 . INF2080 Lecture :: 1st February 21 / 37

  49. Properties of CFLs: Concatanation Let G 1 = ( V 1 , Σ 1 , R 1 , S 1 ) and G 2 = ( V 2 , Σ 2 , R 2 , S 2 ) be two grammars that generate L 1 , L 2 respectively. Concatanation: create grammar G L 1 L 2 = ( V , Σ , R , S ) that accepts all words w = w 1 w 2 , where w 1 ∈ L 1 and w 2 ∈ L 2 . new start variable S V = V 1 ∪ V 2 ∪ { S } , Σ = Σ 1 ∪ Σ 2 , and R = R 1 ∪ R 2 ∪ { S → S 1 S 2 } . INF2080 Lecture :: 1st February 21 / 37

  50. CFL Concatanation: Example S 1 → aS 1 b | ε S 2 → aS 2 a | bS 2 b | cS 2 c | ε INF2080 Lecture :: 1st February 22 / 37

  51. CFL Concatanation: Example S 1 → aS 1 b | ε S 2 → aS 2 a | bS 2 b | cS 2 c | ε S → S 1 S 2 S 1 → aS 1 b | ε S 2 → aS 2 a | bS 2 b | cS 2 c | ε INF2080 Lecture :: 1st February 22 / 37

  52. Properties of CFLs: Kleene star Let G 1 = ( V 1 , Σ 1 , R 1 , S 1 ) generate language L 1 . Kleene star: create grammar G = ( V , Σ , R , S ) that generates all words in L ∗ 1 . INF2080 Lecture :: 1st February 23 / 37

  53. Properties of CFLs: Kleene star Let G 1 = ( V 1 , Σ 1 , R 1 , S 1 ) generate language L 1 . Kleene star: create grammar G = ( V , Σ , R , S ) that generates all words in L ∗ 1 . V = V 1 , Σ = Σ 1 , R = R 1 ∪ { S 1 → ε, S 1 → S 1 S 1 } , S = S 1 . INF2080 Lecture :: 1st February 23 / 37

  54. Properties of CFLs: Kleene star Let G 1 = ( V 1 , Σ 1 , R 1 , S 1 ) generate language L 1 . Kleene star: create grammar G = ( V , Σ , R , S ) that generates all words in L ∗ 1 . V = V 1 , Σ = Σ 1 , R = R 1 ∪ { S 1 → ε, S 1 → S 1 S 1 } , S = S 1 . Example: S 1 → ε | S 1 S 1 S 1 → aS 1 b | ε S 1 → aS 1 b | ε INF2080 Lecture :: 1st February 23 / 37

  55. Properties of CFLs Closure under complement/intersection? INF2080 Lecture :: 1st February 24 / 37

  56. Properties of CFLs Closure under complement/intersection? � No, but we need to know more before we can determine if a language is not context-free. (next week) INF2080 Lecture :: 1st February 24 / 37

  57. Ambiguity Consider the grammar E → E + E | E × E | ( E ) | a INF2080 Lecture :: 1st February 25 / 37

  58. Ambiguity Consider the grammar E → E + E | E × E | ( E ) | a Here: the alphabet is { a , + , × , ( , ) } . INF2080 Lecture :: 1st February 25 / 37

  59. Ambiguity Consider the grammar E → E + E | E × E | ( E ) | a Here: the alphabet is { a , + , × , ( , ) } . → arithmetic expressions over a INF2080 Lecture :: 1st February 25 / 37

  60. Ambiguity Consider the grammar E → E + E | E × E | ( E ) | a Here: the alphabet is { a , + , × , ( , ) } . → arithmetic expressions over a What does the parse tree for the string a + a × a look like? INF2080 Lecture :: 1st February 25 / 37

  61. Ambiguity E + E E a × E E a a INF2080 Lecture :: 1st February 26 / 37

  62. Ambiguity E + E E a × E E a a Intuitively corresponds to a + ( a × a ) INF2080 Lecture :: 1st February 26 / 37

  63. Ambiguity E E + × E E E E a × + a E E E E a a a a Intuitively corresponds to a + ( a × a ) INF2080 Lecture :: 1st February 26 / 37

  64. Ambiguity E E + × E E E E a × + a E E E E a a a a Intuitively corresponds to a + ( a × a ) Intuitively corresponds to ( a + a ) × a INF2080 Lecture :: 1st February 26 / 37

  65. Ambiguity E E + × E E E E a × + a E E E E a a a a Intuitively corresponds to a + ( a × a ) Intuitively corresponds to ( a + a ) × a This is called ambiguity INF2080 Lecture :: 1st February 26 / 37

  66. Ambiguity But just having multiple possible derivations does not mean that a grammar is ambiguous. INF2080 Lecture :: 1st February 27 / 37

  67. Ambiguity But just having multiple possible derivations does not mean that a grammar is ambiguous. Two derivations could look different, yet “structurally” the same: apply the same rules to the same variables, yet in a different order. INF2080 Lecture :: 1st February 27 / 37

  68. Ambiguity But just having multiple possible derivations does not mean that a grammar is ambiguous. Two derivations could look different, yet “structurally” the same: apply the same rules to the same variables, yet in a different order. E � E + E � E + E × E � a + E × E � a + a × E � a + a × a E � E + E � a + E � a + E × E � a + a × E � a + a × a INF2080 Lecture :: 1st February 27 / 37

  69. Ambiguity But just having multiple possible derivations does not mean that a grammar is ambiguous. Two derivations could look different, yet “structurally” the same: apply the same rules to the same variables, yet in a different order. E � E + E � E + E × E � a + E × E � a + a × E � a + a × a E � E + E � a + E � a + E × E � a + a × E � a + a × a E + E E Both have the same parse tree! a × E E a a INF2080 Lecture :: 1st February 27 / 37

  70. Ambiguity We are interested in structurally different derivations, i.e., two derivations of the same word that, given a predefined order of derivation, are different INF2080 Lecture :: 1st February 28 / 37

  71. Ambiguity We are interested in structurally different derivations, i.e., two derivations of the same word that, given a predefined order of derivation, are different Definition A leftmost derivation of a string replaces, in each derivation step, the leftmost variable. Then a string is derived ambiguously over a grammar G if it has two or more leftmost derivations over G . INF2080 Lecture :: 1st February 28 / 37

  72. Ambiguity We are interested in structurally different derivations, i.e., two derivations of the same word that, given a predefined order of derivation, are different Definition A leftmost derivation of a string replaces, in each derivation step, the leftmost variable. Then a string is derived ambiguously over a grammar G if it has two or more leftmost derivations over G . If L ( G ) contains a string that is derived ambiguously, we say that G is ambiguous. INF2080 Lecture :: 1st February 28 / 37

  73. Chomsy Normal Form Context-free languages have a nice property: Every CFL can be described by a CFG in Chomsky Normal Form : Definition A grammar is in Chomsky Normal Form if every rule is of the form: A → BC A → a where a is any terminal, A is any variable, B , C are any variables that are not the start variable. In addition the rule S → ε is permitted. INF2080 Lecture :: 1st February 29 / 37

  74. Definition A grammar is in Chomsky Normal Form if every rule is of the form: A → BC A → a where a is any terminal, A is any variable, B , C are any variables that are not the start variable. In addition the rule S → ε is permitted. Proof sketch: Given an arbitrary grammar G . First, add new start variable S 0 and new rule S 0 → S to G . INF2080 Lecture :: 1st February 30 / 37

  75. Definition A grammar is in Chomsky Normal Form if every rule is of the form: A → BC A → a where a is any terminal, A is any variable, B , C are any variables that are not the start variable. In addition the rule S → ε is permitted. Proof sketch: Given an arbitrary grammar G . First, add new start variable S 0 and new rule S 0 → S to G .Then, remove all rules A → ε , followed by all “unit” rules A → B . INF2080 Lecture :: 1st February 30 / 37

  76. Definition A grammar is in Chomsky Normal Form if every rule is of the form: A → BC A → a where a is any terminal, A is any variable, B , C are any variables that are not the start variable. In addition the rule S → ε is permitted. Proof sketch: Given an arbitrary grammar G . First, add new start variable S 0 and new rule S 0 → S to G .Then, remove all rules A → ε , followed by all “unit” rules A → B . For each such occurence of A in the righthand side of a rule, add a new rule with ε (resp. B ) substituted for A (see examples on next slide). INF2080 Lecture :: 1st February 30 / 37

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