Your SlideShare is downloading. ×
Grammar
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

Grammar

712
views

Published on

Published in: Education, Technology

0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
712
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
49
Comments
0
Likes
1
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Grammars 1
  • 2. Grammars Grammars express languages Example: the English languagesentence noun _ phrase predicatenoun _ phrase article noun predicate verb 2
  • 3. article aarticle thenoun boynoun dogverb runsverb walks 3
  • 4. A derivation of “the boy walks”:sentence noun _ phrase predicate noun _ phrase verb article noun verb the noun verb the boy verb the boy walks 4
  • 5. A derivation of “a dog runs”:sentence noun _ phrase predicate noun _ phrase verb article noun verb a noun verb a dog verb a dog runs 5
  • 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”, 6
  • 7. Notation noun boy noun dog Variable Terminal Production or ruleNon-terminal 7
  • 8. Another Example Grammar: S aSb S Derivation of sentence ab : S aSb ab S aSb S 8
  • 9. S aSbGrammar:SDerivation of sentence aabb : S aSb aaSbb aabb S aSb S 9
  • 10. S aSb aaSbb aaaSbbb aaabbb Other derivations:S aSb aaSbb aaaSbbb aaaaSbbbb aaaabbbb 10
  • 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” } 11
  • 12. Language of the grammar S aSb S n n L {a b : n 0} 12
  • 13. More Notation G V , T , P, S Grammar V : A finiteSet of variables T : A finiteSet of terminal symbols S : Start variable P: Set of Production rules 13V and T are assumed to be disjoint
  • 14. Example G S aSb Grammar : S G V , T , P, SV {S } T {a, b} P {S aSb, S 14 }
  • 15. More Notation Sentential Form: A sentence that contains variables and terminals Example:S aSb aaSbb aaaSbbb aaabbb Sentential Forms sentence 15
  • 16. * S aaabbb We write: Instead of:S aSb aaSbb aaaSbbb aaabbb 16
  • 17. * w1 wnIn general we write: w1 w2 w3  wnIf: 17
  • 18. * w wBy default: 18
  • 19. ExampleGrammar DerivationsS aSb * SS * S ab * S aabb * S aaabbb 19
  • 20. ExampleGrammar DerivationsS aSb S aaSbbS aaSbb aaaaaSbbbbb 20
  • 21. Another Grammar Example G S Ab Grammar : A aAb A Derivations:S Ab aAbb abbS Ab aAbb aaAbbb aabbb 21
  • 22. ExampleGrammar DerivationsS aSb S aaSbbS aaSbb aaaaaSbbbbb 22
  • 23. More DerivationsS Ab aAbb aaAbbb aaaAbbbb aaaaAbbbbb aaaabbbbbS aaaabbbbbS aaaaaabbbbbbb n nS a b b 23
  • 24. Language of a Grammar For a grammar G with start variable S : L(G ) {w : S w} String of terminals 24
  • 25. Example G S Ab For grammar : A aAb A n n L(G ) {a b b : n 0} n n Since: S a b b 25
  • 26. A Convenient Notation A aAb A aAb | A article a article a | the article the 26
  • 27. Linear Grammars 27
  • 28. Linear Grammars Grammars with at most one variable at the right side of a production S aSb S Ab Examples: S A aAb A 28
  • 29. A Non-Linear Grammar Grammar G : S SS SWhy S SS S aSbWhat will happen if S bSaS S instead L(G) {w : na ( w) nb ( w)} Number of a in string w 29
  • 30. Another Linear Grammar G S A Grammar : A aB | B Ab n n L(G ) {a b : n 0} 30
  • 31. Right-Linear Grammars All productions have form: A xB A x string of Example: terminals S abS S a 31
  • 32. Left-Linear Grammars A Bx All productions have form: or A x string of S Aab terminals Example: A Aab | B B a 32
  • 33. Regular Grammars 33
  • 34. Regular Grammars A regular grammar is any right-linear or left-linear grammar Examples: G1 G2 S abS S Aab S a A Aab | B B a 34
  • 35. Observation Regular grammars generate regular languages G2 G1 Examples: S Aab S abS A Aab | B S a B a L(G1) (ab) * a L(G2 ) aab(ab) * 35
  • 36. Regular GrammarsGenerateRegular Languages 36
  • 37. TheoremLanguages RegularGenerated by LanguagesRegular Grammars 37
  • 38. Theorem - Part 1Languages RegularGenerated by LanguagesRegular Grammars Any regular grammar generates a regular language 38
  • 39. Theorem - Part 2Languages RegularGenerated by LanguagesRegular Grammars Any regular language is generated by a regular grammar 39
  • 40. Proof – Part 1 Languages Regular Generated by Languages Regular Grammars The language L (G ) generated by any regular grammar G is regular 40
  • 41. The case of Right-LinearGrammars Let G be a right-linear grammar We will prove: L(G ) is regular L( M ) L(G ) Proof idea: We will construct NFA M 41 with L(M)=L(G)
  • 42. Grammar G is right-linearExample: S aA | B A aa B B b B|a 42
  • 43. Construct NFA M such that every state is a grammar variable: A special S VF final state BS aA | BA aa BB b B|a 43
  • 44. Add edges for each production: a A S VF BS aA 44
  • 45. a A S VF BS aA | B 45
  • 46. A a a S a VF BS aA | BA aa B 46
  • 47. A a a S a VF BS aA | B bA aa BB bB 47
  • 48. A a a S a VF a BS aA | B bA aa BB bB | a 48
  • 49. A a a S a VF a B bS aA aaaB aaabB aaaba 49
  • 50. NFA M Grammar A G a S aA | B a A aa BS a B bB | a VF a B L( M ) L(G ) b aaab * a b * a 50
  • 51. In General A right-linear grammar G has variables: V0 ,V1,V2 , and productions: Vi a1a2 amV j or Vi a1a2 am 51
  • 52. We construct the NFA M such that:each variable Vicorresponds to anode: V1 V3 V0 VF V2 special V4 final state 52
  • 53. Vi a1a2 amV jFor each production:we add transitions and intermediatenodesVi a1 a2 ……… am Vj 53
  • 54. For each production: Vi a1a2 amwe add transitions and intermediatenodesVi a1 a2 ……… am VF 54
  • 55. Resulting NFA M looks like this: a9 a2 a4 a1 V1 V3 a3 a5 V0 a3 a4 VF a8 a9 V2 a5 V4It holds that: L(G ) L( M ) 55
  • 56. The case of Left-LinearGrammars Let G be a left-linear grammar We will prove: L (G ) is regular Proof idea: We will construct a right-linear grammar G with R L(G ) L(G ) 56
  • 57. Since G is left-linear grammarthe productions look like: A Ba1a2 ak A a1a2 ak 57
  • 58. Left G A Ba1a2 aklinear Construct right-linear grammar G A BvRight G A ak a2a1Blinear R A v B 58
  • 59. Construct right-linear grammar GLeft G A a1a2 aklinear A vRight G A ak a2a1linear R A v 59
  • 60. R L(G ) L(G ) It is easy to see that: G Since is right-linear, we have: R L(G )L(G ) L(G )Regular Regular RegularLanguage Language Language 60
  • 61. Proof - Part 2 Languages Regular Generated by Languages Regular Grammars Any regular language L is generated by some regular grammar G 61
  • 62. Any regular language L is generatedby some regular grammar GProof idea: Let M be the NFA with L L(M ). Construct from M a regular grammar G such that L ( M ) L (G ) 62
  • 63. Since L is regular there is an NFA M such that L L(M ) bExample: M a a q0 q1 q2 bL ab * ab(b * ab) * q3L L(M ) 63
  • 64. Convert M to a right-linear grammar b M a a q0 q1 q2 bq0 aq1 q3 64
  • 65. b M a a q0 q1 q2q0 aq1 bq1 bq1 q3q1 aq2 65
  • 66. b M a a q0 q1 q2q0 aq1q1 bq1 bq1 aq2 q3q2 bq3 66
  • 67. L(G ) L( M ) L G bq0 aq1 M a a q0 q1 q2q1 bq1q1 aq2 bq2 bq3 q3q3 q1q3 67
  • 68. In General aFor any transition: q p Add production: q ap variable terminal variable 68
  • 69. For any final state: qfAdd production: qf 69
  • 70. Since G is right-linear grammar G is also a regular grammar with L(G ) L( M ) L 70
  • 71. For any regular language one canconstruct left linear as well as rightlinear grammar. 71
  • 72. A a a a VFS a F → Ba B B → Bb here F is start symbol S aA | B B → Aaa b B→S A aa B A → Sa B bB | a S→ this is new rule 72 as S is now a final state
  • 73. A → aB D → CbB → bB D→B C→B aB → aC B →B b Now D is theC→b D start variableD→ B B → Aa A→D→ 73
  • 74. Language generated by bothaaab* a +b*a 74
  • 75. 75
  • 76. 76
  • 77. 77
  • 78. Context-Free Languages 78
  • 79. Context-Free Languages n n R{a b } {ww } Regular Languages 79
  • 80. Context-Free LanguagesContext-Free PushdownGrammars Automata stack automaton 80
  • 81. Context-Free Grammars 81
  • 82. ExampleA context-free grammar G: S aSb SA derivation: S aSb aaSbb aabb 82
  • 83. S aSb SL(G ) n n {a b : n 0} (((( )))) 83
  • 84. A context-free grammar G: S aSa S bSb SAnother derivation:S aSa abSba abaSaba abaaba 84
  • 85. S aSa S bSb SL(G ) R {ww : w {a, b}*} 85
  • 86. ExampleA context-free grammar G: S aSb S SS SA derivation: S SS aSbS abS ab 86
  • 87. A context-free grammar G: S aSb S SS SA derivation:S SS aSbS abS abaSb abab 87
  • 88. S aSb S SS SL(G ) {w : na ( w) nb ( w), and na (v) nb (v) in any prefix v} () ((( ))) (( )) 88
  • 89. Definition: Context-FreeGrammars Grammar G (V , T , P, S )Variables Terminal Start symbols variable Productions of the form: A x x is string of variables and terminals 89
  • 90. Definition: Context-FreeLanguages A language L is context-free if and only ifthere is a C.F.grammar Gwith L L(G ) 90
  • 91. Derivation Order1. S AB 2. A aaA 4. B Bb 3. A 5. BLeftmost derivation: 1 2 3 4 5 S AB aaAB aaB aaBb aabRightmost derivation: 1 4 5 2 3 S AB ABb Ab aaAb aab 91
  • 92. S aAB A bBb B A|Leftmost derivation:S aAB abBbB abAbB abbBbbB abbbbB abbbbRightmost derivation: S aAB aA abBb abAb abbBbb abbbb 92
  • 93. Derivation Trees 93
  • 94. Def: G =(V,T,P,S)An ordered tree is a derivation tree for G iff1 The root is labeled S2 every leaf has a label from T { }3 Every interior vertex has a label from V4 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 …an5 A leaf labeled has no sibling. 94
  • 95. Yield:The string of terminals obtained byreading the leaves of the tree from leftto right omitting any ’s encounteredis called yield of the tree. 95
  • 96. Partial derivation treeA tree that has properties 3,4,5 but 1need notAnd 2 is replaced byV T { } 96
  • 97. S AB A aaA | B Bb | S AB S A B 97
  • 98. S AB A aaA | B Bb | S AB aaAB S A B a a A 98
  • 99. S AB A aaA | B Bb | S AB aaAB aaABb S A B a a A B b 99
  • 100. S AB A aaA | B Bb |S AB aaAB aaABb aaBb S A B a a A B b 100
  • 101. S AB A aaA | B Bb |S AB aaAB aaABb aaBb aabDerivation Tree S A B a a A B b 101
  • 102. S AB A aaA | B Bb |S AB aaAB aaABb aaBb aabDerivation Tree S A B yield a a A B b aa b aab 102
  • 103. Partial Derivation TreesS AB A aaA | B Bb | S ABPartial derivation tree S A B 103
  • 104. S AB aaABPartial derivation tree S A B a a A 104
  • 105. sentential S AB aaAB formPartial derivation tree S A B yield a a A aaAB 105
  • 106. Sometimes, derivation order doesn’t matterLeftmost:S AB aaAB aaB aaBb aabRightmost:S AB ABb Ab aaAb aab SSame derivation tree A B a a A B b 106
  • 107. Ambiguity 107
  • 108. E E E | E E | (E) | a a a a E E E E a E a E E a a E a a*aE E leftmost derivationa E E a a 108
  • 109. E E E | E E | (E) | a a a aE E E E E E a E E E a a E a a a E E leftmost derivation E E a a 109 a
  • 110. E E E | E E | (E) | a a a a Two derivation trees E EE E E Ea E E E E a a a a 110 a
  • 111. The grammarE E E | E E | (E) | ais ambiguous: string a a a has two derivation trees E E E E E E a E E E E a a a a a 111
  • 112. The grammarE E E | E E | (E) | ais ambiguous:string a a a has two leftmost derivations E E E a E a E E a a E a a*a E E E E E E a E E a a E a a a 112
  • 113. Definition: A context-free grammar G is ambiguous if some string w L(G ) has: two or more derivation trees 113
  • 114. In other words: A context-free grammar G is ambiguous if some string w L(G ) has: two or more leftmost derivations (or rightmost) 114
  • 115. Why do we care about ambiguity? a a a take a 2 E EE E E Ea E E E E a a a a 115 a
  • 116. 2 2 2 E EE E E E2 E E E E 2 2 2 2 116 2
  • 117. 2 2 2 6 2 2 2 8 6 8 E E 2 4 4 2E E E E 2 2 2 22 E E E E 2 2 2 2 117 2
  • 118. Correct result: 2 2 2 6 6 E 2 4 E E 2 2 2 E E 2 2 118
  • 119. • Ambiguity is bad for programming languages• We want to remove ambiguity 119
  • 120. We fix the ambiguous grammar: E E E | E E | (E) | aNew non-ambiguous grammar: E E T E T T T F T F F (E) 120 F a
  • 121. E E T T T F T a T a T F a F F a a F a a a E a a aE E T E TE TT T F T T FT F F F aF (E)F a a a 121
  • 122. Unique derivation tree E a a a E T T T F F F a a a 122
  • 123. The grammar G: E E T E T T T F T F F (E) F ais non-ambiguous: Every string w L (G ) has a unique derivation tree 123
  • 124. Inherent Ambiguity Some context free languages have only ambiguous grammars Example: n n m n m m L {a b c } {a b c }S S1 | S2 S1 S1c | A S2 aS2 | B A aAb | B bBc | 124
  • 125. n n nThe string a b chas two derivation trees S S S1 S2 S1 c a S2 125
  • 126. Compiler Lexical parser analyzerinput output machine program code 126
  • 127. A parser knows the grammarof the programming language 127
  • 128. The parser finds the derivationof a particular input derivation Parser input E => E + E E -> E + E => E + E * E10 + 2 * 5 |E*E => 10 + E*E | INT => 10 + 2 * E => 10 + 2 * 5 128
  • 129. derivation tree derivation EE => E + E E + E => E + E * E => 10 + E*E 10 E * E => 10 + 2 * E => 10 + 2 * 5 2 5 129