Upcoming SlideShare
×

# Theory of Computation (Fall 2013): Regular Grammars, Right Linear & Left Linear Grammars, Regular Grammars & Regular Languages

• 391 views

More in: Technology
• Comment goes here.
Are you sure you want to
Be the first to comment
Be the first to like this

Total Views
391
On Slideshare
0
From Embeds
0
Number of Embeds
19

Shares
0
0
Likes
0

No embeds

### Report content

No notes for slide

### Transcript

• 1. Theory of Computation Regular Grammars, Right Linear & Left Linear Grammars, Regular Grammars & Regular Languages Vladimir Kulyukin www.vkedco.blogspot.com
• 2. Outline ● Grammars & Derivations ● Regular Grammars ● Right Linear Grammars ● Left Linear Grammars ● Regular Grammars & Regular Languages
• 3. Grammars & Derivations
• 4. A Grammar: Formal Definition ( ) disjoint.areandi.e., .and overstringsareandwhere,formtheof isproductioneachs;productionofsetfiniteais symbol;starttheis alphabet;terminaltheis alphabet;lnonterminatheis where,,,,tuple-4aisgrammarA VΣVΣ εxVΣ yxyx P VS Σ V PSVGG ∅=∩ ≠∪ → ∈ Σ=
• 5. Grammar Example { } { } { }( ).,,,,,,,, :definitionFormal .| | :grammartheofformshorthandtheisHere . :languagefor this grammaraisHere.bydefinedlanguageheConsider t ** ε ε ε →→→→= → → → → → → XbXXXSaSSSbaXSG bXX XaSS X bXX XS aSS ba
• 6. Derivation Definition ( ) ( ) .withends andwithstartsthatstepsmoreorzeroof derivationaisthereifonlyandif .and,,2) ,,,,,stringsexistthere1) ifonlyandif)derives( * * z w zw Pyxuyvzuxvw xVvyxu zwzw ⇒ ∈→== ≠∪Σ∈ ⇒ ε
• 7. Derivation Example a a a b S
• 8. Derivation Example a a a b S S
• 9. Derivation Example a a a b S S S
• 10. Derivation Example a a a b S S S S
• 11. Derivation Example a a a b S S S S X
• 12. Derivation Example a a a b S S S S X X
• 13. Derivation Example a a a b ε S S S S X X
• 14. Language Generated by Grammar ( ) { }.|Then grammar.abeLet ** xSxGL G ⇒Σ∈=
• 15. Derivation Example Formalised { } { } { }( ) ( ) ( ) . 4)(Rule 3)(Rule 2)(Rule 1)(Rule 1)(Rule 1)(Rule .derivationafindtry tousLet .iff,definitionBy?Is 4);3);2);)1 :referenceofeaseforoutrulesthewriteusLet .,,,,,,,, Consider * aaab aaabX aaaX aaaS aaS aSS aaabSGLaaabGLaaab XbXXXSaSS XbXXXSaSSSbaXS G ⇒ ⇒ ⇒ ⇒ ⇒ ⇒ ⇒∈∈ →→→→ →→→→ = ε ε
• 16. Regular Grammars
• 17. Regular Grammars ● Two types of regular grammars: – Right linear grammars – Left linear grammars ● They are called regular, because they generate regular languages ● Regular grammars can be viewed as a bridge from regular expressions to context-free grammars (or from finite state automata to stack machines)
• 18. Regular Linear Grammars & Regular Languages
• 19. Right Linear Grammar ( ) .and,,where ;2. ;1. :formstwotheofonehasinproductionevery ifflinearrightis,,,grammarA * ΣzVYVX zX zYX P PSVG ∈∈∈ → → Σ=
• 20. Right Linear Grammars & Regular Languages Theorem: If G is a right linear grammar, L(G) is regular. Main Proof Insight: Construct an NFA M such that L(M) = L(G). How? For every type of production, add a corresponding path to the NFA.
• 21. Proof For every production of the form X → a1a2a3...akY, add the following transitions to the NFA: … a1 a2 a3 ak X Y
• 22. Proof If X → Y, then we add the following production: X Y ε
• 23. Proof For every production of the form X → a1 a2 a3 ...ak , add the following transitions to the NFA. F is the symbol that we use to designate the final state. … a1 a2 a3 ak X F
• 24. Proof For every production of the form X → ε, no additional transitions need to be added to the NFA. X ε Xis the same as
• 25. Example: Regular Grammar to NFA ● Consider the following regular grammar: – S  1S | 0T | ε – T  1T | 0U – U  1U | 0S
• 26. Example S 1 Modeling Production S  1S
• 27. Example S 1 Modeling Production S  0T 0 T
• 28. Example S 1 - Modeling Production S  ε - S becomes a final state 0 T
• 29. Example S 1 Modeling Production T  1T 0 T 1
• 30. Example S 1 Modeling Production T  0U 0 T 1 U 0
• 31. Example S 1 Modeling Production U  1U 0 T 1 U 0 1
• 32. Example S 1 Modeling Production U  0S 0 T 1 U 0 1 0
• 33. Left Linear Grammars
• 34. Left Linear Grammar ( ) .and,,where ;2. ;1. :formstwotheofonehasinproductionevery ifflinearleftis,,,grammarA * ΣzVYVX zX YzX P PSVG ∈∈∈ → → Σ=
• 35. Left Linear Grammars & Regular Languages
• 36. Left Linear Grammars & Regular Languages Theorem: If G is a left linear grammar, then L(G) is regular. Proof Insight: Convert G to a right linear grammar GR such that L(G) = [L(GR)]R .
• 37. Proof ● Given a left-linear grammar G, construct a right-linear grammar GR by reversing its rules ● L(GR) = LR ● Construct an NFA for GR with a single start state and a single final state (this is always possible with ε-moves) ● Reverse the NFA by reversing the direction of the arcs and making the old start state the new final state and the old final state the new start state
• 38. Language Reversals { } { } { } .01,01 ;110,011,010,10,011,110,010,01 :Examples .|thenlanguage,aisIf .110,011 ;, :Examples .ofreversaltheisthen,If ** * == == ∈= == == ∈ R R RR R R R LL LL LxxLL xx baxabx xxΣx
• 39. Back to Proof ( ) ( ) ( ) ( )[ ] .Thus, .then,ifThen.' productionahasthen,productiona hasIf.'productionahasthen ,formtheofproductionahasIf R R RR R RR R R R GLxx GLxGLxzX GzX GYzXG YzXG ∈= ∈∈→ → → →
• 40. Regular Grammars & Regular Languages LanguagesRegularGrammarsRegular Grammarslinear-RightGrammarslinear-Left ⊆ =
• 41. From Regular Languages to Regular Grammars
• 42. From Regular Languages to Regular Grammars Theorem: If L is a regular language, there is a right linear grammar G such that L(G) = L. Proof Insight: Take an NFA M such that L(M) = L and construct a right linear grammar G from its transitions such that L(G) = L.
• 43. Proof ( ) ( ) { } ( ) ( ).iff,oflengthon theinduction By.productiontheadd,statefinaleveryFor .|...|sproductiontheadd,1,,...,, formtheofsitionevery tranForfollows.aslinear rightaconstructWe.such thatNFAanbeLet 11 GLxMLxx qFq ararqnrraq G LMLM nn ∈∈ →∈ →≥= = ε δ
• 44. Example Q0 Q1 Q2 a c b ε NFA M Q0  aQ0 Q0  bQ1 Q1  cQ1 Q1  Q2 Q2  ε Right Linear Grammar G:
• 45. Regular Languages & Regular Grammars GrammarsRegularLanguagesRegular ⊆
• 46. Regular Languages = Regular Grammars languagesRegulargrammarsRegular languagesRegulargrammarsRegular grammarsRegularlanguagesRegular = ⊆ ⊆
• 47. References ● Davis, Weyuker, Sigal. Computability, Complexity, and Languages, 2nd Edition, Academic Press ● Brooks Webber. Formal Language: A Practical Introduction, Franklin, Beedle & Associates, Inc ● Hopcroft and Ullman. Introduction to Automata Theory, Languages, and Computation, Narosa Publishing House