Regular Grammars
description
Transcript of Regular Grammars
![Page 1: Regular Grammars](https://reader036.fdocument.org/reader036/viewer/2022082821/56815013550346895dbdf8ac/html5/thumbnails/1.jpg)
Regular Grammars
Lecture 11Naveen Z Quazilbash
![Page 2: Regular Grammars](https://reader036.fdocument.org/reader036/viewer/2022082821/56815013550346895dbdf8ac/html5/thumbnails/2.jpg)
Overview
• Examples• Definition• Language defined by the grammar• Linearity of grammar• Equivalence with NFAs
![Page 3: Regular Grammars](https://reader036.fdocument.org/reader036/viewer/2022082821/56815013550346895dbdf8ac/html5/thumbnails/3.jpg)
Examples
• Example 1:S aAbAcAAε– The corresponding language is ac*b
• Example 2:S aAAbBBaABε– The corresponding language is (ab)+
S=>aAb=>acAb=>accAb=>accbS=>aAb=>ab
S=>aA=>abB=>abS=>aA=>abB=>abaA=>ababB=>abab
![Page 4: Regular Grammars](https://reader036.fdocument.org/reader036/viewer/2022082821/56815013550346895dbdf8ac/html5/thumbnails/4.jpg)
Some Definitions
• The substitution rules that form a grammar are called “Productions”.
• The left side of a rule is a “variable”.• The right side is a mixture of variables and
symbols of the alphabet (a.k.a “Terminal symbols”).
![Page 5: Regular Grammars](https://reader036.fdocument.org/reader036/viewer/2022082821/56815013550346895dbdf8ac/html5/thumbnails/5.jpg)
Some Definitions Contd. (2)
• A grammar defines the language of all strings that can be derived from the “Start variable, S”, by successive applications of productions.
• Note that the strings of this language do not contain variables.
![Page 6: Regular Grammars](https://reader036.fdocument.org/reader036/viewer/2022082821/56815013550346895dbdf8ac/html5/thumbnails/6.jpg)
Some Definitions Contd. (3)
• A sequence of substitutions that leads from S to some string of the language is called a “Derivation”.
• Example:– S=>aA=>abB=>ab is a derivation; we may write it
in a shorter form as S=>*ab, – where “=>*” means “several steps”.
![Page 7: Regular Grammars](https://reader036.fdocument.org/reader036/viewer/2022082821/56815013550346895dbdf8ac/html5/thumbnails/7.jpg)
Formal Definition of Grammar
• Formally, a grammar is a four tuple:G= {V, T, S, P}, where
– V= set of variables, e.g. V={S, A, B}– T= set of terminal symbols, e.g. T={a, b}– S= start variable (analogous to the start state in
automaton).– P= set of productions, e.g. P={SaA, AbB, BaA, Bε}
• Note that V and T must be non-empty and disjoint.
![Page 8: Regular Grammars](https://reader036.fdocument.org/reader036/viewer/2022082821/56815013550346895dbdf8ac/html5/thumbnails/8.jpg)
Language defined by the Grammar
• The set of all strings derivable from S is the “language defined (or generated) by the grammar”.
![Page 9: Regular Grammars](https://reader036.fdocument.org/reader036/viewer/2022082821/56815013550346895dbdf8ac/html5/thumbnails/9.jpg)
Linearity of Grammar
• A Grammar is “Linear” if all productions are of the form Aw1Bw2 or Aw3, where A and B are variables and w1, w2
and w3 are strings (with no variables in them).• Example of a linear grammar:
SaBSεBSb– Shorter notation: Sab | ε– Language: L={an bn: n≥0}, (not a regular language)
• Thus, a linear grammar may generate a language that is not accepted by any finite automaton.
S=>εS=>aB=>aSb=>abS=>aB=>aSb=>aaBb=>aaSbb=>aabb
![Page 10: Regular Grammars](https://reader036.fdocument.org/reader036/viewer/2022082821/56815013550346895dbdf8ac/html5/thumbnails/10.jpg)
NON-LINEAR ???
![Page 11: Regular Grammars](https://reader036.fdocument.org/reader036/viewer/2022082821/56815013550346895dbdf8ac/html5/thumbnails/11.jpg)
Linearity of Grammar-Contd..
• Right linear grammar: VT*V VT*
• Left linear grammar: VVT* VT*
![Page 12: Regular Grammars](https://reader036.fdocument.org/reader036/viewer/2022082821/56815013550346895dbdf8ac/html5/thumbnails/12.jpg)
ASSIGNMENT NO.2
1. For each of the following three languages on ∑={a,b}, draw a DFA that accepts it: (6 points)– All strings that have no b’s (note that it includes ε).– All strings with atleast two a’s and any number of
b’s.– All strings with atmost two a’s and any number of
b’s.
• Due Date: 28th March 2013.