• edited by
24,909 views
92 92 votes

Consider the following grammars. Names representing terminals have been specified in capital letters.
$$\begin{array}{|llll|}\hline G1 :  &  \text{stmnt} & \rightarrow & \text{WHILE (expr) stmnt} \\%\hline 
\text{}  &  \text{stmnt} & \rightarrow & \text{OTHER} \\%\hline 
\text{}  &  \text{expr} & \rightarrow & \text{ID} \\\hline 
G2 :  &  \text{stmnt} & \rightarrow & \text{WHILE (expr) stmnt} \\%\hline
\text{}  &  \text{stmnt} & \rightarrow & \text{OTHER} \\%\hline
\text{}  &  \text{expr} & \rightarrow & \text{expr} + \text{expr} \\%\hline
\text{}  &  \text{expr} & \rightarrow & \text{expr} * \text{expr} \\%\hline
\text{}  &  \text{expr} & \rightarrow & \text{ID} \\\hline  \end{array}$$

Which one of the following statements is true?

  1. $G_1$ is context-free but not regular and $G_2$ is regular
  2. $G_2$ is context-free but not regular and $G_1$ is regular
  3. Both $G_1$ and $G_2$ are regular
  4. Both $G_1$ and $G_2$ are context-free but neither of them is regular

3 Answers

Best answer
96 96 votes

Regular grammar is either right linear or left linear. A left linear grammar is one in which there is at most $1$ non-terminal on the right side of any production, and it appears at the left most position. Similarly, in right linear grammar non-terminal appears at the right most position.

Here, we can write a right linear grammar for $G1$ as

$S \rightarrow w(E$
$E \rightarrow id)S$
$S \rightarrow o$

(w - WHILE, o - OTHER)

So, $L(G1)$ is regular.

Now for $G2$ also we can write a right linear grammar:

$S \rightarrow w(E$
$E \rightarrow id)S$
$E \rightarrow id+E$
$E \rightarrow id*E$
$S \rightarrow o$

making its language regular. 

So, both $G1$ and $G2$ have an equivalent regular grammar. But given in the question both these grammars are neither right linear nor left linear and hence not a regular grammar. So, D must be the answer. 

http://www.cs.odu.edu/~toida/nerzic/390teched/regular/grammar/reg-grammar.html

• edited by
6 6 votes
1 flag:
✌ Low quality (js__)
0 0 votes

G1: 

stmnt -> WHILE (ID) stmnt

stmnt-> OTHER   

[S-> AS/B 

B-> b ]
Language: L(G1) = (WHILE(ID))* OTHER
Regular expression: (A)*B .
Thus G1 is regular.

G2:
- Because of productions expr → expr + expr 

and expr → expr * expr (non-linear), 

G2 is context-free but not regular.

Answer: B 

1 flag:
✌ Edit necessary (Ashutosh_Patel_74 “A grammar is regular if and only if is a single nonterminal and is a single terminal or a single terminal followed by a single nonterminal, that is a production is of the form X -> a or X -> aY, where X and Y are nonterminals and a is a terminal.”)
Answer:
Position:
Show:

Related questions

40 40 votes
2 answers 2 answers
12.9k
12.9k views
Ishrat Jahan asked Oct 30, 2014
12,929 views
Consider the grammar given below:$S \rightarrow x \ B \mid y \ A$$A \rightarrow x \mid x \ S \mid y \ A \ A$$B \rightarrow y \mid y \ S \mid...
35 35 votes
6 answers 6 answers
10.7k
10.7k views
Ishrat Jahan asked Oct 29, 2014
10,702 views
The two grammars given below generate a language over the alphabet $\{x, y, z\}$$G1 : S \rightarrow x \mid z \mid x \ S \mid z \ S \mid y \ B$$\qquad B \rightarrow y \...
71 71 votes
8 answers 8 answers
23.5k
23.5k views
Ishrat Jahan asked Oct 30, 2014
23,509 views
Consider the regular expression $R = (a + b)^* \ (aa + bb) \ (a + b)^*$Which one of the regular expressions given below defines the same language as defined by the regula...
52 52 votes
8 answers 8 answers
13.5k
13.5k views
Ishrat Jahan asked Oct 30, 2014
13,453 views
Consider the regular expression $R = (a + b)^* (aa + bb) (a + b)^*$Which deterministic finite automaton accepts the language represented by the regular expression $R$?