Left-Linear and Right-Linear Grammars
Asked Answered
E

2

22

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.

Eurythmic answered 11/12, 2012 at 8:38 Comment(0)
J
99

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 write G, with only one production rule S --> e (where S is the start symbol), is an equivalent RG.

  • UNION OPERATION: If the RE is of the form e + f, where both e and f are terminals, we can write G, with two production rules S --> e | f, is an equivalent RG.

  • CONCATENATION: If the RE is of the form ef, where both e and f are terminals, we can write G, with two production rules S --> eA, A --> f, is an equivalent RG.

  • STAR CLOSURE: If the RE is of the form e*, where e is a terminal and * Kleene star closure operation, we can write two production rules in G, S --> eS | ^, is an equivalent RG.

  • PLUS CLOSURE: If the RE is of the form e+, where e is a terminal and + Kleene plus closure operation, we can write two production rules in G, 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 in G, 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 in G, 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 in G, 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 in G, S --> eA, A --> fS | f, is an equivalent RG.

Be sure that you understands all above rules, here is the summary table:

+-------------------------------+--------------------+------------------------+
| TYPE                          | REGULAR-EXPRESSION | RIGHT-LINEAR-GRAMMAR   |
+-------------------------------+--------------------+------------------------+
| SINGLE TERMINAL               | e                  | S --> e                |
| UNION OPERATION               | e + f              | S --> e | f            |
| CONCATENATION                 | ef                 | S --> eA, A --> f      |
| STAR CLOSURE                  | e*                 | S --> eS | ^           |
| PLUS CLOSURE                  | e+                 | S --> eS | e           |
| STAR CLOSURE ON UNION         | (e + f)*           | S --> eS | fS | ^      |
| PLUS CLOSURE ON UNION         | (e + f)+           | S --> eS | fS | e | f  |
| STAR CLOSURE ON CONCATENATION | (ef)*              | S --> eA | ^, A --> fS |
| PLUS CLOSURE ON CONCATENATION | (ef)+              | S --> eA, A --> fS | f |
+-------------------------------+--------------------+------------------------+

note: symbol e and f are terminals, ^ is NULL symbol, and S is the start variable

[ANSWER]

Now, we can come to you problem.

a) (0+1)*00(0+1)*

Language description: All the strings consist of 0s and 1s, containing at-least one pair of 00.

  • Right Linear Grammar:

    S --> 0S | 1S | 00A
    A --> 0A | 1A | ^

    String can start with any string of 0s and 1s thats why included rules s --> 0S | 1S and Because at-least one pair of 00 ,there is no null symbol. S --> 00A is included because 0, 1 can be after 00. The symbol A takes care of the 0's and 1's after the 00.

  • Left Linear Grammar:

    S --> S0 | S1 | A00
    A --> A0 | A1 | ^

b) 0*(1(0+1))*

Language description: Any number of 0, followed any number of 10 and 11.
{ because 1(0 + 1) = 10 + 11 }

  • Right Linear Grammar:

    S --> 0S | A | ^
    A --> 1B
    B --> 0A | 1A | 0 | 1

    String starts with any number of 0 so rule S --> 0S | ^ are included, then rule for generating 10 and 11 for any number of times using A --> 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)*

Language description: First is language contains null(^) string because there a * (star) on outside of every thing present inside (). Also if a string in language is not null that defiantly ends with 00. One can simply think this regular expression in the form of ( ( (A)* B )* C )* , where (A)* is (01 + 10)* that is any number of repeat of 01 and 10. If there is a instance of A in string there would be a B defiantly because (A)*B and B is 11.
Some example strings { ^, 00, 0000, 000000, 1100, 111100, 1100111100, 011100, 101100, 01110000, 01101100, 0101011010101100, 101001110001101100 ....}

  • 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 a 00. When the string ends with 00, the variable A matches the pattern ((01 + 10)* + 11)*. Again this pattern can either be null or must end with 11. If its null, then A matches it with S again i.e the string ends with pattern like (00)*. If the pattern is not null, B matches with (01 + 10)*. When B matches all it can, A starts 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:

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

(answer)
You solution are wrong for following reasons,

Left-linear grammar is wrong Because string 0010 not possible to generate. Right-linear grammar is wrong Because string 1000 is 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)*

DFA

b) 0*(1(0+1))*

DFA

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)*

(((01+10)*11)* 00 )*
(          a*   b )*

Actually in above expression a it 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

DFA for ((01+10)*11)* is:

DFA

DFA for (((01+10)*11)* 00 )* is:

DFA

Try to find similarity in construction of above three DFA. don't move ahead till you don't understand first one

Joacima answered 19/12, 2012 at 5:13 Comment(14)
Using DFA one can easily write grammars: Converting a DFA to a Regular Grammar and A regular grammar is either a right-linear grammar or a left-linear grammar.Joacima
thanks for the great answer, helped me a lot +1. Is there any tools or programs you are using for drawing or verifying the language description.In addition, if you recommend a book, i'll appreciate it.Rothwell
@Rothwell Thanks! To draw diagrams I use dia:. In comments: to my answer I suggested some source of learning formal theory. To draw ASCII diagrams I use ascii-flow.Joacima
I've seen a lot of answers on stack and this one by far is the most helpful I've seen, thanks!Floria
Adding related question link: converting context free grammer into regular expressionJoacima
Can the 2nd example be written as: S --> 0S | A | ^, A --> 1B | ^, B --> 0 | 1. If we replace 0A|1A with the closure, we get the same thingDisputable
@GrijeshChauhan honestly I find those rules of converting basic regexes to RLGs intuitive, but converting complex regexes makes me fuzzy. Consider 3rd example (((01+10)*11)*00)* you gave. Its closure of concatenation. Basic rule says: (ef)* = S → eA | ϵ, A → fS. This does not directly map to S → A | 00S | ϵ. There is definitely something more going on here. Some rules / concepts / steps / observations are missing here which should make it more intuitive. Can you point me to that? (I feel there should be more guidelines about how to use those rules in table.)Finny
@Finny I did not write it in single step. It took time to analyse you have to read again and again with copy-pencilJoacima
Maybe I'm not reading it correctly, but it seems to me that the first (a) grammar is wrong? The regex (0+1) means if 0 is to appear, then 1 must appear together as well. However, A --> 0A | 1A | ^ means both 0 and 1 can appear independently! This can't be right? e.g. 001 shouldn't be a correct input but only 0001 or 00001 should be. However, in this grammar 001 would be accepted as a correct input. Do brackets not mean "grouping" instead of "one of"?Cacique
@JIXiang "The regex (0+1) means if 0 is to appear, then 1 must appear together as well." No it means either 0 or 1 , in formal languages the binary operator + stands for union.Joacima
No explanation at all. All you show is how to transform from one form to another with the help of a table. You should explain the idea of how I come to this table, otherwise, nobody is learning anything. "practice without theory is blind"Martinamartindale
@Martinamartindale If things are not clean to you even after given description you should pick a good book and read "regular expression" and "grammar" and "finite automata" separately then try to understand this answer. - yes, this is just an answer to the question not a book ....... I guess you are at wrong place instead pick a good book on formal languagesJoacima
For third regular expression, I came up with somewhat different NFA and grammar as can be seen in this pic. Is it also correct? Also I am not able to see how exactly you were able to directly apply those rules to get left linear grammar. In fact applying rules to get right linear grammar was also difficult / requiring consideration of more facts / not so straight forward. I found regex to NFA to grammar-route easy / straight forward instead of directly preparing grammar from regex.Sou
@anir you should ask a separate question. Instead of asking in comment.Joacima
Z
5

Rules to convert regular expressions to left or right linear regular grammar Cheat sheet

Zita answered 20/1, 2018 at 12:43 Comment(1)
Copied from hereFinny

© 2022 - 2024 — McMap. All rights reserved.