context free grammars

104 Views Asked by At

I'm having trouble to find context free grammars which generate following languages:

  1. $L_1 = \{a^nb^{2n}∣n\ge1\}$

  2. $L_2 = \{a^pb^{p+q}a^q∣p,q\ge1\}$

1

There are 1 best solutions below

6
On BEST ANSWER

HINT: For $L_1$ you need a very small modification of a context-free grammar for $\{a^nb^n:n\ge 1\}$, and I suspect that you’ve seen such a grammar: it has just two productions, $S\to aSb$ and $S\to ab$. That same grammar contains the essential idea needed for a grammar for $L_2$ as well. Start with a production $S\to XY$, let $X$ produce strings of the form $a^pb^p$, and let $Y$ do the rest.