I need help with constructing a left-linear and right-linear grammar for the languages below?
a) (0+1)*00(0+1)*
b) 0*(1(0+1))*
c) (((01+10)*11)*00)*
For a) I have the following:
Left-linear
S --> B00 | S11
B --> B0|B1|011
Right-linear
S --> 00B | 11S
B --> 0B|1B|0|1
Is this correct? I need help with b & c.
Constructing an equivalent Regular Grammar from a Regular Expression
First, I start with some simple rules to construct Regular Grammar(RG) from Regular Expression(RE).
I am writing rules for Right Linear Grammar (leaving as an exercise to write similar rules for Left Linear Grammar)
NOTE: Capital letters are used for variables, and small for terminals in grammar. NULL symbol is
^. Term ‘any number’ means zero or more times that is * star closure.[BASIC IDEA]
SINGLE TERMINAL: If the RE is simply
e (e being any terminal), we can writeG, with only one production ruleS --> e(whereS is the start symbol), is an equivalent RG.UNION OPERATION: If the RE is of the form
e + f, where bothe and f are terminals, we can writeG, with two production rulesS --> e | f, is an equivalent RG.CONCATENATION: If the RE is of the form
ef, where bothe and f are terminals, we can writeG, with two production rulesS --> eA, A --> f, is an equivalent RG.STAR CLOSURE: If the RE is of the form
e*, wheree is a terminaland* Kleene star closureoperation, we can write two production rules inG,S --> eS | ^, is an equivalent RG.PLUS CLOSURE: If the RE is of the form e+, where
e is a terminaland+ Kleene plus closureoperation, we can write two production rules inG,S --> eS | e, is an equivalent RG.STAR CLOSURE ON UNION: If the RE is of the form (e + f)*, where both
e and f are terminals, we can write three production rules inG,S --> eS | fS | ^, is an equivalent RG.PLUS CLOSURE ON UNION: If the RE is of the form (e + f)+, where both
e and f are terminals, we can write four production rules inG,S --> eS | fS | e | f, is an equivalent RG.STAR CLOSURE ON CONCATENATION: If the RE is of the form (ef)*, where both
e and f are terminals, we can write three production rules inG,S --> eA | ^, A --> fS, is an equivalent RG.PLUS CLOSURE ON CONCATENATION: If the RE is of the form (ef)+, where both
e and f are terminals, we can write three production rules inG,S --> eA, A --> fS | f, is an equivalent RG.Be sure that you understands all above rules, here is the summary table:
[ANSWER]
Now, we can come to you problem.
a)
(0+1)*00(0+1)*Right Linear Grammar:
S –> 0S | 1S | 00A
A –> 0A | 1A | ^
String can start with any string of
0s and1s thats why included ruless --> 0S | 1Sand Because at-least one pair of00,there is no null symbol.S --> 00Ais included because0,1can be after00. The symbolAtakes care of the 0’s and 1’s after the00.Left Linear Grammar:
S –> S0 | S1 | A00
A –> A0 | A1 | ^
b)
0*(1(0+1))*Right Linear Grammar:
S –> 0S | A | ^
A –> 1B
B –> 0A | 1A | 0 | 1
String starts with any number of
0so ruleS --> 0S | ^are included, then rule for generating10and11for any number of times usingA --> 1B and B --> 0A | 1A | 0 | 1.Other alternative right linear grammar can be
S –> 0S | A | ^
A –> 10A | 11A | 10 | 11
Left Linear Grammar:
S –> A | ^
A –> A10 | A11 | B
B –> B0 | 0
An alternative form can be
S –> S10 | S11 | B | ^
B –> B0 | 0
c)
(((01+10)*11)*00)*Left Linear Grammar:
S –> A00 | ^
A –> B11 | S
B –> B01 | B10 | A
S --> A00 | ^because any string is either null, or if it’s not null it ends with a00. When the string ends with00, the variableAmatches the pattern((01 + 10)* + 11)*. Again this pattern can either be null or must end with11. If its null, thenAmatches it withSagain i.e the string ends with pattern like(00)*. If the pattern is not null,Bmatches with(01 + 10)*. WhenBmatches all it can,Astarts matching the string again. This closes the out-most * in((01 + 10)* + 11)*.Right Linear Grammar:
S –> A | 00S | ^
A –> 01A | 10A | 11S
Second part of you question:
(answer)
You solution are wrong for following reasons,
Left-linear grammar is wrong Because string
0010not possible to generate.Right-linear grammar is wrong Because string
1000is not possible to generate. Although both are in language generated by regular expression of question (a).EDIT
Adding DFA’s for each regular expression. so that one can find it helpful.
a)
(0+1)*00(0+1)*b)
0*(1(0+1))*c)
(((01+10)*11)*00)*Drawing DFA for this regular expression is trick and complex.
For this I wanted to add DFA’s
To simplify the task, we should think the kind formation of RE
to me the RE
(((01+10)*11)*00)*looks like(a*b)*Actually in above expression
ait self in the form of(a*b)*that is
((01+10)*11)*RE
(a*b)*is equals to(a + b)*b + ^. The DFA for (ab) is as belows:DFA for
((01+10)*11)*is:DFA for
(((01+10)*11)* 00 )*is:Try to find similarity in construction of above three DFA. don’t move ahead till you don’t understand first one