patternMinor
Context Free Grammar for language L
Viewed 0 times
freelanguagegrammarforcontext
Problem
Can someone help with this:
$L=\{a^ib^j \mid i,j \ge 1 \text{ and } i \ne j \text{ and } i<2j\}$
I'm trying to write a grammar for this language?
I tried this:
$S \to S_1 \mid S_2 \\
S_1 \to aXb \\
X \to aXb \mid aaXb \mid aab \\
S_2 \to aYb \\
Y \to aYb \mid Yb \mid b \\
$
What do you think?
$L=\{a^ib^j \mid i,j \ge 1 \text{ and } i \ne j \text{ and } i<2j\}$
I'm trying to write a grammar for this language?
I tried this:
$S \to S_1 \mid S_2 \\
S_1 \to aXb \\
X \to aXb \mid aaXb \mid aab \\
S_2 \to aYb \\
Y \to aYb \mid Yb \mid b \\
$
What do you think?
Solution
The solution in the question is correct.
The constraint $i\ne j$ is the one that gives us trouble. To get around it we have to split into two cases: (i) $ij$ (but still $i<2j$)
thus
$S\to S_{(i)} \mid S_{(ii)}$
as the first production splits into this two cases.
Now, divide and conquer: case (i) is very simple, since we only need $i<j$,
$S_{(i)} \to aS_{(i)}b \mid B$
$ B \to Bb \mid b$
This is your $S_2$.
For case (ii), we need to $j< i < 2j$, so for every single $a$, the variable $C$ will generate exactly one $b$, and at a certain point we switch to the variable $D$ that will generate two $a$'s for each $b$.
$S_{(ii)} \to aCb $
$ C \to aCb \mid D$
$ D \to aaDb \mid aab$
This is your $S_1$.
The constraint $i\ne j$ is the one that gives us trouble. To get around it we have to split into two cases: (i) $ij$ (but still $i<2j$)
thus
$S\to S_{(i)} \mid S_{(ii)}$
as the first production splits into this two cases.
Now, divide and conquer: case (i) is very simple, since we only need $i<j$,
$S_{(i)} \to aS_{(i)}b \mid B$
$ B \to Bb \mid b$
This is your $S_2$.
For case (ii), we need to $j< i < 2j$, so for every single $a$, the variable $C$ will generate exactly one $b$, and at a certain point we switch to the variable $D$ that will generate two $a$'s for each $b$.
$S_{(ii)} \to aCb $
$ C \to aCb \mid D$
$ D \to aaDb \mid aab$
This is your $S_1$.
Context
StackExchange Computer Science Q#9831, answer score: 3
Revisions (0)
No revisions yet.