Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

163692498 grammar

83 views

Published on

Fundamental of english grammar

Published in: Education
  • Be the first to comment

163692498 grammar

  1. 1. 1 Grammars
  2. 2. 2 Grammars Grammars express languages Example: the English language verbpredicate nounarticlephrasenoun predicatephrasenounsentence → → → _ _
  3. 3. 3walksverb runsverb dognoun boynoun thearticle aarticle → → → → → →
  4. 4. 4 A derivation of “the boy walks”: walksboythe verbboythe verbnounthe verbnounarticle verbphrasenoun predicatephrasenounsentence ⇒ ⇒ ⇒ ⇒ ⇒ ⇒ _ _
  5. 5. 5 A derivation of “a dog runs”: runsdoga verbdoga verbnouna verbnounarticle verbphrasenoun predicatephrasenounsentence ⇒ ⇒ ⇒ ⇒ ⇒ ⇒ _ _
  6. 6. 6 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”,
  7. 7. 7 Notation dognoun boynoun → → Variable or Non-terminal TerminalProduction rule
  8. 8. 8 Another Example Grammar: Derivation of sentence : λ→ → S aSbS abaSbS ⇒⇒ ab aSbS → λ→S
  9. 9. 9 aabbaaSbbaSbS ⇒⇒⇒ aSbS → λ→S aabb λ→ → S aSbS Grammar: Derivation of sentence :
  10. 10. 10 Other derivations: aaabbbaaaSbbbaaSbbaSbS ⇒⇒⇒⇒ aaaabbbbaaaaSbbbb aaaSbbbaaSbbaSbS ⇒⇒ ⇒⇒⇒
  11. 11. 11 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” }
  12. 12. 12 Language of the grammar λ→ → S aSbS }0:{ ≥= nbaL nn
  13. 13. 13 More Notation Grammar ( )SPTVG ,,,= :V :T :S :P A finiteSet of variables A finiteSet of terminal symbols Start variable Set of Production rules V and T are assumed to be disjoint
  14. 14. 14 Example Grammar : λ→ → S aSbSG ( ), , ,G V T P S= }{SV = },{ baT = },{ λ→→= SaSbSP
  15. 15. 15 More Notation Sentential Form: A sentence that contains variables and terminals Example: aaabbbaaaSbbbaaSbbaSbS ⇒⇒⇒⇒ Sentential Forms sentence
  16. 16. 16 We write: Instead of: aaabbbS * ⇒ aaabbbaaaSbbbaaSbbaSbS ⇒⇒⇒⇒
  17. 17. 17 In general we write: If: nww * 1 ⇒ nwwww ⇒⇒⇒⇒ 321
  18. 18. 18 By default: ww * ⇒
  19. 19. 19 Example λ→ → S aSbS aaabbbS aabbS abS S * * * * ⇒ ⇒ ⇒ ⇒λ Grammar Derivations
  20. 20. 20 baaaaaSbbbbaaSbb aaSbbS ∗ ∗ ⇒ ⇒ λ→ → S aSbS Grammar Example Derivations
  21. 21. 21 Another Grammar Example Grammar : λ→ → → A aAbA AbS Derivations: G aabbbaaAbbbaAbbAbS abbaAbbAbS →→→→ →→→
  22. 22. 22 baaaaaSbbbbaaSbb aaSbbS ∗ ∗ ⇒ ⇒ λ→ → S aSbS Grammar Example Derivations
  23. 23. 23 More Derivations aaaabbbbbaaaaAbbbbb aaaAbbbbaaAbbbaAbbAbS ⇒⇒ ⇒⇒⇒⇒ bbaS bbbaaaaaabbbbS aaaabbbbbS nn ∗ ∗ ∗ ⇒ ⇒ ⇒
  24. 24. 24 Language of a Grammar For a grammar with start variable : G S }:{)( wSwGL ∗ ⇒= String of terminals
  25. 25. 25 Example For grammar : λ→ → → A aAbA AbS }0:{)( ≥= nbbaGL nn Since: bbaS nn ∗ ⇒ G
  26. 26. 26 A Convenient Notation λ→ → A aAbA λ|aAbA → thearticle aarticle → → theaarticle |→
  27. 27. 27 Linear Grammars
  28. 28. 28 Linear Grammars Grammars with at most one variable at the right side of a production Examples: λ→ → → A aAbA AbS λ→ → S aSbS
  29. 29. 29 A Non-Linear Grammar bSaS aSbS S SSS → → → → λ Grammar :G )}()(:{)( wnwnwGL ba == Number of in stringa w Why S SS What will happen if S S instead
  30. 30. 30 Another Linear Grammar Grammar : AbB aBA AS → → → λ| }0:{)( ≥= nbaGL nn G
  31. 31. 31 Right-Linear Grammars All productions have form: Example: xBA → xA →or aS abSS → → string of terminals
  32. 32. 32 Left-Linear Grammars All productions have form: Example: BxA → aB BAabA AabS → → → | xA → or string of terminals
  33. 33. 33 Regular Grammars
  34. 34. 34 Regular Grammars A regular grammar is any right-linear or left-linear grammar Examples: aS abSS → → aB BAabA AabS → → → | 1G 2G
  35. 35. 35 Observation Regular grammars generate regular languages Examples: aS abSS → → aabGL *)()( 1 = aB BAabA AabS → → → | *)()( 2 abaabGL = 1G 2G
  36. 36. 36 Regular Grammars Generate Regular Languages
  37. 37. 37 Theorem Languages Generated by Regular Grammars Regular Languages=
  38. 38. 38 Theorem - Part 1 Languages Generated by Regular Grammars Regular Languages ⊆ Any regular grammar generates a regular language
  39. 39. 39 Theorem - Part 2 Languages Generated by Regular Grammars Regular Languages ⊇ Any regular language is generated by a regular grammar
  40. 40. 40 Proof – Part 1 Languages Generated by Regular Grammars Regular Languages ⊆ The language generated by any regular grammar is regular )(GL G
  41. 41. 41 The case of Right-Linear Grammars Let be a right-linear grammar We will prove: is regular Proof idea: We will construct NFA with L(M)=L(G) G )(GL M )()( GLML =
  42. 42. 42 Grammar is right-linearG Example: aBbB BaaA BaAS | | → → →
  43. 43. 43 Construct NFA such that every state is a grammar variable: M aBbB BaaA BaAS | | → → → S FV A B special final state
  44. 44. 44 Add edges for each production: S FV A B a aAS →
  45. 45. 45 S FV A B a BaAS |→ λ
  46. 46. 46 S FV A B a λ BaaA BaAS → → | a a
  47. 47. 47 S FV A B a λ bBB BaaA BaAS → → → | a a b
  48. 48. 48 S FV A B a λ abBB BaaA BaAS | | → → → a a b a
  49. 49. 49aaabaaaabBaaaBaAS ⇒⇒⇒⇒ S FV A B a λ a a b a
  50. 50. 50 S FV A B a λ a a b a abBB BaaA BaAS | | → → → G M GrammarNFA abaaaab GLML ** )()( + ==
  51. 51. 51 In General A right-linear grammar has variables: and productions: G ,,, 210 VVV jmi VaaaV 21→ mi aaaV 21→ or
  52. 52. 52 We construct the NFA such that: each variable corresponds to a node: M iV 0V FV 1V 2V 3V 4V special final state
  53. 53. 53 For each production: we add transitions and intermediate nodes jmi VaaaV 21→ iV jV………1a 2a ma
  54. 54. 54 For each production: we add transitions and intermediate nodes mi aaaV 21→ iV FV………1a 2a ma
  55. 55. 55 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:
  56. 56. 56 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 )()( ′=
  57. 57. 57 Since is left-linear grammar the productions look like: G kaaBaA 21→ kaaaA 21→
  58. 58. 58 Construct right-linear grammar G′ Left linear G kaaBaA 21→ G′ BaaaA k 12→ BvA → BvA R → Right linear
  59. 59. 59 Construct right-linear grammar G′ G kaaaA 21→ G′ 12aaaA k → vA → R vA → Left linear Right linear
  60. 60. 60 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
  61. 61. 61 Proof - Part 2 Languages Generated by Regular Grammars Regular Languages ⊇ Any regular language is generated by some regular grammar L G
  62. 62. 62 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 =
  63. 63. 63 Since is regular there is an NFA such that L M )(MLL = Example: a b a bλ *)*(* abbababL = )(MLL = M 1q 2q 3q 0q
  64. 64. 64 Convert to a right-linear grammarM a b a bλ M 0q 1q 2q 3q 10 aqq →
  65. 65. 65 a b a bλ M 0q 1q 2q 3q 21 11 10 aqq bqq aqq → → →
  66. 66. 66 a b a bλ M 0q 1q 2q 3q 32 21 11 10 bqq aqq bqq aqq → → → →
  67. 67. 67 a b a bλ M 0q 1q 2q 3q λ→ → → → → → 3 13 32 21 11 10 q qq bqq aqq bqq aqq G LMLGL == )()(
  68. 68. 68 In General For any transition: a q p Add production: apq → variable terminal variable
  69. 69. 69 For any final state: fq Add production: λ→fq
  70. 70. 70 Since is right-linear grammar is also a regular grammar with G G LMLGL == )()(
  71. 71. 71 For any regular language one can construct left linear as well as right linear grammar.
  72. 72. 72 S FV A B a λ abBB BaaA BaAS | | → → → a a b a F → Ba B → Bb here F is start symbol B → Aaa B → S A → Sa S → λ this is new rule as S is now a final state
  73. 73. 73 A → aB B → bB B → aC C → b D D →λ B D → λ D → Cb D → Bλ C → B a B → B b Now D is the start variable B → Aa A → λ
  74. 74. 74 Language generated by both aaab* a +b* a
  75. 75. 75
  76. 76. 76
  77. 77. 77
  78. 78. 78 Context-Free Languages
  79. 79. 79 Regular Languages }{ nn ba }{ R ww Context-Free Languages
  80. 80. 80 Context-Free Languages Pushdown Automata Context-Free Grammars stack automaton
  81. 81. 81 Context-Free Grammars
  82. 82. 82 Example A context-free grammar : λ→ → S aSbS aabbaaSbbaSbS ⇒⇒⇒ G A derivation:
  83. 83. 83 λ→ → S aSbS =)(GL (((( )))) }0:{ ≥nba nn
  84. 84. 84 λ→ → → S bSbS aSaS abaabaabaSabaabSbaaSaS ⇒⇒⇒⇒ A context-free grammar :G Another derivation:
  85. 85. 85 λ→ → → S bSbS aSaS =)(GL }*},{:{ bawwwR ∈
  86. 86. 86 λ→ → → S SSS aSbS ababSaSbSSSS ⇒⇒⇒⇒ A context-free grammar :G A derivation: Example
  87. 87. 87 λ→ → → S SSS aSbS abababaSbabSaSbSSSS ⇒⇒⇒⇒⇒ A context-free grammar :G A derivation:
  88. 88. 88 λ→ → → S SSS aSbS }prefixanyin )()(and ),()(:{ v vnvn wnwnw ba ba ≥ = () ((( ))) (( )) =)(GL
  89. 89. 89 Definition: Context-Free Grammars Grammar Productions of the form: xA → x is string of variables and terminals ),,,( SPTVG = Variables Terminal symbols Start variable
  90. 90. 90 Definition: Context-Free Languages A language is context-free if and only if there is a C.F.grammar with L G )(GLL =
  91. 91. 91 Derivation Order ABS →.1 λ→ → A aaAA .3 .2 λ→ → B BbB .5 .4 aabaaBbaaBaaABABS 54321 ⇒⇒⇒⇒⇒ Leftmost derivation: aabaaAbAbABbABS 32541 ⇒⇒⇒⇒⇒ Rightmost derivation:
  92. 92. 92 λ|AB bBbA aABS → → → Leftmost derivation: abbbbabbbbB abbBbbBabAbBabBbBaABS ⇒⇒ ⇒⇒⇒⇒ Rightmost derivation: abbbbabbBbb abAbabBbaAaABS ⇒⇒ ⇒⇒⇒⇒
  93. 93. 93 Derivation Trees
  94. 94. 94 Def: G =(V,T,P,S) An ordered tree is a derivation tree for G iff 1 The root is labeled S 2 every leaf has a label from T∪{ λ} 3 Every interior vertex has a label from V 4 If a vertex has label A(variable) and its children are labeled from (L to R) a1,a2,…an then P must contain a production A a1 a2 …an 5 A leaf labeled λ has no sibling.
  95. 95. 95 Yield: The string of terminals obtained by reading the leaves of the tree from left to right omitting any λ’s encountered is called yield of the tree.
  96. 96. 96 Partial derivation tree A tree that has properties 3,4,5 but 1 need not And 2 is replaced by V ∪ T ∪ {λ }
  97. 97. 97 ABS ⇒ ABS → λ|aaAA → λ|BbB → S BA
  98. 98. 98 ABS → λ|aaAA → λ|BbB → aaABABS ⇒⇒ a a A S BA
  99. 99. 99 ABS → λ|aaAA → λ|BbB → aaABbaaABABS ⇒⇒⇒ S BA a a A B b
  100. 100. 100 ABS → λ|aaAA → λ|BbB → aaBbaaABbaaABABS ⇒⇒⇒⇒ S BA a a A B b λ
  101. 101. 101 ABS → λ|aaAA → λ|BbB → aabaaBbaaABbaaABABS ⇒⇒⇒⇒⇒ S BA a a A B b λ λ Derivation Tree
  102. 102. 102 aabaaBbaaABbaaABABS ⇒⇒⇒⇒⇒ yield aab baa = λλ S BA a a A B b λ λ Derivation Tree ABS → λ|aaAA → λ|BbB →
  103. 103. 103 Partial Derivation Trees ABS ⇒ S BA Partial derivation tree ABS → λ|aaAA → λ|BbB →
  104. 104. 104 aaABABS ⇒⇒ S BA a a A Partial derivation tree
  105. 105. 105 aaABABS ⇒⇒ S BA a a A Partial derivation tree sentential form yield aaAB
  106. 106. 106 aabaaBbaaBaaABABS ⇒⇒⇒⇒⇒ aabaaAbAbABbABS ⇒⇒⇒⇒⇒ S BA a a A B b λ λ Same derivation tree Sometimes, derivation order doesn’t matter Leftmost: Rightmost:
  107. 107. 107 Ambiguity
  108. 108. 108 aEEEEEE |)(|| ∗+→ aaa ∗+ E EE EE + a a a ∗ aaaEaa EEaEaEEE *+⇒∗+⇒ ∗+⇒+⇒+⇒ leftmost derivation
  109. 109. 109 aEEEEEE |)(|| ∗+→ aaa ∗+ E EE + a a ∗ EE a aaaEaa EEaEEEEEE ∗+⇒∗+⇒ ∗+⇒∗+⇒∗⇒ leftmost derivation
  110. 110. 110 aEEEEEE |)(|| ∗+→ aaa ∗+ E EE + a a ∗ EE a E EE EE + a a a ∗ Two derivation trees
  111. 111. 111 The grammar aEEEEEE |)(|| ∗+→ is ambiguous: E EE + a a ∗ EE a E EE EE + a a a ∗ string aaa ∗+ has two derivation trees
  112. 112. 112 string aaa ∗+ has two leftmost derivations aaaEaa EEaEEEEEE ∗+⇒∗+⇒ ∗+⇒∗+⇒∗⇒ aaaEaa EEaEaEEE *+⇒∗+⇒ ∗+⇒+⇒+⇒ The grammar aEEEEEE |)(|| ∗+→ is ambiguous:
  113. 113. 113 Definition: A context-free grammar is ambiguous if some string has: two or more derivation trees G )(GLw∈
  114. 114. 114 In other words: A context-free grammar is ambiguous if some string has: two or more leftmost derivations G )(GLw∈ (or rightmost)
  115. 115. 115 Why do we care about ambiguity? E EE + a a ∗ EE a E EE EE + a a a ∗ aaa ∗+ take 2=a
  116. 116. 116 E EE + ∗ EE E EE EE + ∗ 222 ∗+ 2 2 2 2 2 2
  117. 117. 117 E EE + ∗ EE E EE EE + ∗ 6222 =∗+ 2 2 2 2 2 2 8222 =∗+ 4 2 2 2 6 2 2 24 8
  118. 118. 118 E EE EE + ∗ 6222 =∗+ 2 2 2 4 2 2 2 6 Correct result:
  119. 119. 119 • We want to remove ambiguity • Ambiguity is bad for programming languages
  120. 120. 120 We fix the ambiguous grammar: aEEEEEE |)(|| ∗+→ New non-ambiguous grammar: aF EF FT FTT TE TEE → → → ∗→ → +→ )(
  121. 121. 121 aF EF FT FTT TE TEE → → → ∗→ → +→ )( aaaFaaFFa FTaTaTFTTTEE ∗+⇒∗+⇒∗+⇒ ∗+⇒+⇒+⇒+⇒+⇒ E E T+ T ∗ F F a T F a a aaa ∗+
  122. 122. 122 E E T+ T ∗ F F a T F a a aaa ∗+ Unique derivation tree
  123. 123. 123 The grammar : aF EF FT FTT TE TEE → → → ∗→ → +→ )( is non-ambiguous: Every string has a unique derivation tree G )(GLw∈
  124. 124. 124 Inherent Ambiguity Some context free languages have only ambiguous grammars Example: }{}{ mmnmnn cbacbaL ∪= λ| |11 aAbA AcSS → → λ| |22 bBcB BaSS → →21 | SSS →
  125. 125. 125 The string nnn cba has two derivation trees S 1S S 2S 1S c 2Sa
  126. 126. 126 Lexical analyzer parser Compiler program machine code input output
  127. 127. 127 A parser knows the grammar of the programming language
  128. 128. 128 The parser finds the derivation of a particular input 10 + 2 * 5 Parser E -> E + E | E * E | INT E => E + E => E + E * E => 10 + E*E => 10 + 2 * E => 10 + 2 * 5 input derivation
  129. 129. 129 10 E 2 5 E => E + E => E + E * E => 10 + E*E => 10 + 2 * E => 10 + 2 * 5 derivation derivation tree E E E E + *

×