0

I am trying to build the CFG to $\{a^n b^m c^k \mid m=n+k\}$ but does not working.

I tried $S \to aSc$, $S \to S_1$, $S \to \epsilon$, $S_1 \to bS_1c$, $S_1 \to \epsilon$ but it does not work.

Erick Wong
  • 25,198
  • 3
  • 37
  • 91

1 Answers1

2

Observe that your language can be written as the product of two context-free languages $$ \{a^nb^{n+k}c^k \mid n, k \geqslant 0 \} = \{a^nb^n \mid n \geqslant 0\}\{b^kc^k \mid k \geqslant 0\} $$ which naturally leads to the grammar \begin{align} S &\to S_0S_1 \\ S_0 &\to aS_0b + 1 \\ S_1 &\to bS_1c + 1 \end{align}

J.-E. Pin
  • 40,163
  • The $+1$ seems like an odd notation. Don’t you mean $\mid \epsilon$ or something similar? – Erick Wong May 17 '21 at 04:27
  • @Erick_Wong This is just the algebraic notation, which seems appropriate for a Math site. $1$ denotes the identity of the free monoid, that is the empty word. The $+$ notation is natural when you associate an equation to the grammar. For instance, the language ${a^nb^n \mid n \geqslant 0}$ is the least solution of the equation $S = aSb + 1$. – J.-E. Pin May 17 '21 at 06:04
  • 1
    Interesting, thanks for the explanation (I hadn't seen Chomsky's paper on CFG algebras before). It still seems like a mixed metaphor to use the algebraic notation on the RHS in conjunction with the production rule arrow rather than a more conventional equals sign. – Erick Wong May 17 '21 at 06:52