Give a context-free grammar

84 Views Asked by At

I'm struggling to understand how to construct a context-free grammar for the following language.

$\{b^nw|w∈\{a,b\}^∗,|w|=n,n≥0\}$

The thing that is confusing me is the beginning of the statement. I would understand a statement like the one below to mean $b$ can be the letter $(a,b)$.

$\{b^n ∈\{a,b\}^*\}$

However I don't understand how to interpret something like the following.

$\{b^nw|w∈\{a,b\}^*\}$

What is $w$ and what is $b$? How would I convert this to a context-free grammar?

2

There are 2 best solutions below

0
On

For the given language, we can put out some words:
$\varepsilon, ba, bb, bbaa, bbab$..

That is, length of the word is $2n$.

Notice that the first half of the word is consisted only of $b$'s of length $n$, the other half is consisted of $\{a, b\}^*$, that is either empty word ($\varepsilon$) (iff $n = 0$) or a mixture of $a$'s and/or $b$'s.
Let us construct the production rules: $$S \rightarrow W | E$$ $$W \rightarrow bWa | bWb | ba | bb$$ $$E \rightarrow \varepsilon$$

Do not forget to construct the grammar as a tuple of 4, defined here.

0
On

Your language can be generated by the grammar $$ S \to bSa + bSb + 1 $$ where $1$ denotes the empty word. Can you see why?