-2

I recently started learning context free grammars and was working on a couple of exercise problems and couldn't really figure out how would this exactly look like.

I started with:

$$\begin{align}S&\to S_1S_2\\ S_1&\to aS_1\mid\epsilon\\ S_2&\to bS_2\mid\epsilon\\ \end{align}$$

But I'm not sure how to create transitions for the reverse… Can someone give me some pointers or direct me in the right direction to find the solution?

Yuval Filmus
  • 276,994
  • 27
  • 311
  • 503
laura
  • 1
  • 1

2 Answers2

1

Context free grammars are basically the combination of four things:

  • Alternation: Choose one of two or more alternative sub-grammars.
  • Concatenation: Make one sub-grammar follow another one.
  • Repetition: Let a sub-grammar repeat any number of times.
  • Recursion: Nest a sub-grammar inside itself, like parentheses or palindromes. There are actually slightly more complicated possibilities for recursion, but the above two are a good start.

Consider the language which just consists of nested parentheses: $\epsilon$, $()$, $(())$, $((()))$, etc. One way of describing this language is $(^j)^j$, which is essentially the same thing as $a^jb^j$ (which you'll see in your task). Another way of describing that is to say that a valid sentence is either empty or a valid sentence with parentheses around it. The second description is precisely what this grammar is trying to convey: $$\begin{align}S_2&\to(S_2)\\ S_2&\to\epsilon\\ \end{align}$$ That's a very simple example of recursion. Two things are important here:

  • Although it doesn't look like a self-reflection with symbols $a$ and $b$, writing the symbols as parentheses makes that a bit clearer. Grammars don't care what symbols look like, so it's akin to a reflection in both cases. That might not look like a palindrome either, but that's because all the symbols in the right half are the same. Recursion matches a sequence followed by the reflection of the same sequence, but in a more general sense: you don't have to spell the elements in the same way in both halves.
  • You can nest another sub-grammar at the reflection point simply by using it as the non-recursive alternative. (Think about how to write a grammar for the language $x$, $(x)$, $((x))$, $(((x)))$, etc. It just requires replacing $S_2\to\epsilon$ with $S_2\to x$.)

The key to writing context-free grammars for simple languages is to look for compositions of the four building blocks listed above. Recursion is probably the least familiar pattern, but it's at the core of this particular exercise.

In this case, there's a clear inner sub-grammar which generates $\epsilon$, $ba$, $bbaa$, … . And there's an outer nesting which puts that grammar in the middle of the forms $\epsilon$, $a S_2 b$, $aa S_2 bb$, … .

rici
  • 12,020
  • 21
  • 38
0

You are lucky, it’s quite easy for this case.

Your language is equal to the language $b^ja^j$ withe equal numbers of a’s and b’s in both sides. So the generate the $a^i$ and $b^i$ we use the rule S -> aSb.

At some point we switch to generating $b^ja^j$. Starting with a non-terminal T, we have a rule T -> bTa and another rule T -> eps to finish. And at last one rule S -> T to switch from one part to the other.

gnasher729
  • 29,996
  • 34
  • 54