Class5

827 views
767 views

Published on

language and grammar

Published in: Education, Technology
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
827
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
8
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Class5

  1. 1. 1 Reverse of a Regular Language
  2. 2. 2 Theorem: The reverse of a regular language is a regular language R L L Proof idea: Construct NFA that accepts : R L invert the transitions of the NFA that accepts L
  3. 3. 3 Proof Since is regular, there is NFA that accepts L Example: baabL += * a b b a L
  4. 4. 4 Invert Transitions a b b a
  5. 5. 5 Make old initial state a final state a b b a
  6. 6. 6 Add a new initial state a b b a λ λ
  7. 7. 7 a b b a λ λ Resulting machine accepts R L baabL += * ababLR += * R L is regular
  8. 8. 8 Grammars
  9. 9. 9 Grammars Grammars express languages Example: the English language verbpredicate nounarticlephrasenoun predicatephrasenounsentence → → → _ _
  10. 10. 10 walksverb runsverb dognoun boynoun thearticle aarticle → → → → → →
  11. 11. 11 A derivation of “the boy walks”: walksboythe verbboythe verbnounthe verbnounarticle verbphrasenoun predicatephrasenounsentence ⇒ ⇒ ⇒ ⇒ ⇒ ⇒ _ _
  12. 12. 12 A derivation of “a dog runs”: runsdoga verbdoga verbnouna verbnounarticle verbphrasenoun predicatephrasenounsentence ⇒ ⇒ ⇒ ⇒ ⇒ ⇒ _ _
  13. 13. 13 Language of the grammar: L = { “a boy runs”, “a boy walks”, “the boy runs”, “the boy walks”, “a dog runs”, “a dog walks”, “the dog runs”, “the dog walks” }
  14. 14. 14 Notation dognoun boynoun → → Variable or Non-terminal TerminalProduction rule
  15. 15. 15 Another Example Grammar: Derivation of sentence : λ→ → S aSbS abaSbS ⇒⇒ ab aSbS → λ→S
  16. 16. 16 aabbaaSbbaSbS ⇒⇒⇒ aSbS → λ→S aabb λ→ → S aSbSGrammar: Derivation of sentence :
  17. 17. 17 Other derivations: aaabbbaaaSbbbaaSbbaSbS ⇒⇒⇒⇒ aaaabbbbaaaaSbbbb aaaSbbbaaSbbaSbS ⇒⇒ ⇒⇒⇒
  18. 18. 18 Language of the grammar λ→ → S aSbS }0:{ ≥= nbaL nn
  19. 19. 19 More Notation Grammar ( )PSTVG ,,,= :V :T :S :P Set of variables Set of terminal symbols Start variable Set of Production rules
  20. 20. 20 Example Grammar : λ→ → S aSbSG ( )PSTVG ,,,= }{SV = },{ baT = },{ λ→→= SaSbSP
  21. 21. 21 More Notation Sentential Form: A sentence that contains variables and terminals Example: aaabbbaaaSbbbaaSbbaSbS ⇒⇒⇒⇒ Sentential Forms sentence
  22. 22. 22 We write: Instead of: aaabbbS * ⇒ aaabbbaaaSbbbaaSbbaSbS ⇒⇒⇒⇒
  23. 23. 23 In general we write: If: nww * 1 ⇒ nwwww ⇒⇒⇒⇒ 321
  24. 24. 24 By default: ww * ⇒
  25. 25. 25 Example λ→ → S aSbS aaabbbS aabbS abS S * * * * ⇒ ⇒ ⇒ ⇒λ Grammar Derivations
  26. 26. 26 baaaaaSbbbbaaSbb aaSbbS ∗ ∗ ⇒ ⇒ λ→ → S aSbS Grammar Example Derivations
  27. 27. 27 Another Grammar Example Grammar : λ→ → → A aAbA AbS Derivations: aabbbaaAbbbaAbbS abbaAbbAbS bAbS ⇒⇒⇒ ⇒⇒⇒ ⇒⇒ G
  28. 28. 28 More Derivations aaaabbbbbaaaaAbbbbb aaaAbbbbaaAbbbaAbbAbS ⇒⇒ ⇒⇒⇒⇒ bbaS bbbaaaaaabbbbS aaaabbbbbS nn ∗ ∗ ∗ ⇒ ⇒ ⇒
  29. 29. 29 Language of a Grammar For a grammar with start variable : G S }:{)( wSwGL ∗ ⇒= String of terminals
  30. 30. 30 Example For grammar : λ→ → → A aAbA AbS }0:{)( ≥= nbbaGL nn Since: bbaS nn ∗ ⇒ G
  31. 31. 31 A Convenient Notation λ→ → A aAbA λ|aAbA → thearticle aarticle → → theaarticle |→
  32. 32. 32 Linear Grammars
  33. 33. 33 Linear Grammars Grammars with at most one variable at the right side of a production Examples: λ→ → → A aAbA AbS λ→ → S aSbS
  34. 34. 34 A Non-Linear Grammar bSaS aSbS S SSS → → → → λ Grammar :G )}()(:{)( wnwnwGL ba ==
  35. 35. 35 Another Linear Grammar Grammar : AbB aBA AS → → → λ| }0:{)( ≥= nbaGL nn G
  36. 36. 36 Right-Linear Grammars All productions have form: Example: xBA → xA → or aS abSS → →
  37. 37. 37 Left-Linear Grammars All productions have form: Example: BxA → aB BAabA AabS → → → | xA → or
  38. 38. 38 Regular Grammars
  39. 39. 39 Regular Grammars A regular grammar is any right-linear or left-linear grammar Examples: aS abSS → → aB BAabA AabS → → → | 1G 2G
  40. 40. 40 Observation Regular grammars generate regular languages Examples: aS abSS → → aabGL *)()( 1 = aB BAabA AabS → → → | *)()( 2 abaabGL = 1G 2G
  41. 41. 41 Regular Grammars Generate Regular Languages
  42. 42. 42 Theorem Languages Generated by Regular Grammars Regular Languages=
  43. 43. 43 Theorem - Part 1 Languages Generated by Regular Grammars Regular Languages ⊆ Any regular grammar generates a regular language
  44. 44. 44 Theorem - Part 2 Languages Generated by Regular Grammars Regular Languages ⊇ Any regular language is generated by a regular grammar
  45. 45. 45 Proof – Part 1 Languages Generated by Regular Grammars Regular Languages ⊆ The language generated by any regular grammar is regular )(GL G
  46. 46. 46 The case of Right-Linear Grammars Let be a right-linear grammar We will prove: is regular Proof idea: We will construct NFA with G )(GL M )()( GLML =
  47. 47. 47 Grammar is right-linearG Example: aBbB BaaA BaAS | | → → →
  48. 48. 48 Construct NFA such that every state is a grammar variable: M aBbB BaaA BaAS | | → → → S FV A B special final state
  49. 49. 49 Add edges for each production: S FV A B a aAS →
  50. 50. 50 S FV A B a BaAS |→ λ
  51. 51. 51 S FV A B a λ BaaA BaAS → → | a a
  52. 52. 52 S FV A B a λ bBB BaaA BaAS → → → | a a b
  53. 53. 53 S FV A B a λ abBB BaaA BaAS | | → → → a a b a
  54. 54. 54 aaabaaaabBaaaBaAS ⇒⇒⇒⇒ S FV A B a λ a a b a
  55. 55. 55 S FV A B a λ a a b a abBB BaaA BaAS | | → → → G M GrammarNFA abaaaab GLML ** )()( + ==
  56. 56. 56 In General A right-linear grammar has variables: and productions: G ,,, 210 VVV jmi VaaaV 21→ mi aaaV 21→ or
  57. 57. 57 We construct the NFA such that: each variable corresponds to a node: M iV 0V FV 1V 2V 3V 4V special final state
  58. 58. 58 For each production: we add transitions and intermediate nodes jmi VaaaV 21→ iV jV………1a 2a ma
  59. 59. 59 For each production: we add transitions and intermediate nodes mi aaaV 21→ iV FV………1a 2a ma
  60. 60. 60 Resulting NFA looks like this:M 0V FV 1V 2V 3V 4V 1a 3a 3a 4a 8a 2a 4a 5a 9a 5a 9a )()( MLGL =It holds that:
  61. 61. 61 The case of Left-Linear Grammars Let be a left-linear grammar We will prove: is regular Proof idea: We will construct a right-linear grammar with G )(GL G′ R GLGL )()( ′=
  62. 62. 62 Since is left-linear grammar the productions look like: G kaaBaA 21→ kaaaA 21→
  63. 63. 63 Construct right-linear grammar G′ In :G kaaBaA 21→ In :G′ BaaaA k 12→ vBA → BvA R →
  64. 64. 64 Construct right-linear grammar G′ In :G kaaaA 21→ In :G′ 12aaaA k → vA → R vA →
  65. 65. 65 It is easy to see that: Since is right-linear, we have: R GLGL )()( ′= )(GL ′ R GL )( ′ G′ )(GL Regular Language Regular Language Regular Language
  66. 66. 66 Proof - Part 2 Languages Generated by Regular Grammars Regular Languages ⊇ Any regular language is generated by some regular grammar L G
  67. 67. 67 Proof idea: Let be the NFA with . Construct from a regular grammar such that Any regular language is generated by some regular grammar L G M )(MLL = M G )()( GLML =
  68. 68. 68 Since is regular there is an NFA such that L M )(MLL = Example: a b a bλ *)*(* abbababL = )(MLL = M 1q 2q 3q 0q
  69. 69. 69 Convert to a right-linear grammarM a b a bλ M 0q 1q 2q 3q 10 aqq →
  70. 70. 70 a b a bλ M 0q 1q 2q 3q 21 11 10 aqq bqq aqq → → →
  71. 71. 71 a b a bλ M 0q 1q 2q 3q 32 21 11 10 bqq aqq bqq aqq → → → →
  72. 72. 72 a b a bλ M 0q 1q 2q 3q λ→ → → → → → 3 13 32 21 11 10 q qq bqq aqq bqq aqq G LMLGL == )()(
  73. 73. 73 In General For any transition: a q p Add production: apq → variable terminal variable
  74. 74. 74 For any final state: fq Add production: λ→fq
  75. 75. 75 Since is right-linear grammar is also a regular grammar with G G LMLGL == )()(

×