2

I have this language L = {a^n b^m c^k: m = |n - k|}.

I know m = |n - k| can be expressed in two ways 1) m = n - k for n >= k or n = m + k 2) m = k - n for k >= n or k = m + n
Therefore, I get two languages where
L1 = {a^n b^m c^k: n = m + k} and L2 = {a^n b^m c^k: k = m + n}.
Then I claimed L is the union of the two, L = L1 U L2.

I don't quite understand how to generate a grammar where one exponent of a terminal is the summation of the other two terminals. i.e, in L1 you have n = m + k.
Also L1 can be simplified further to
a^n => a^(m+k) => a^(m)a^(k) so L1 becomes
L1 = {a^m a^k b^m c^k: m, k >= 0}

Attempt answer for L1 = {a^m a^k b^m c^k: m, k >= 0}
A grammar G1
S -> A|B
A -> aAb|lambda
B -> aBc|lambda

Storage Lenovo
  • 85
  • 1
  • 1
  • 6

2 Answers2

1

a^n b^n:

Consider the CFG:

S ::= aSb | <empty string>

This generates all strings a^n b^n, with correctly matching exponents. The reason this works is that adding an a using this grammar requires adding an additional b as well; by making sure that every production preserves the desired property (that the number of as is the same as the number of bs), we've ensured (by induction, since the property holds initially, and every production preserves it) that it will hold for every sentence we generate from the grammar.

a^n b^m c^(n+m):

If we want to make a grammar to generate the slightly more complex a^n b^m c^(n+m), we can apply similar reasoning: we encode in the structure of the grammar that adding an a or a b requires adding a c:

S ::= aSc | T | <empty string>
T ::= bTc | <empty string>

Again, since every production preserves our desired property (that the number of cs is the number of as plus the number of bs), it will hold for any sentence we generate in the grammar.

You can apply similar reasoning to figure out grammars that will preserve the other mathematical properties you mentioned in the OP.

Ollin Boer Bohan
  • 2,296
  • 1
  • 8
  • 12
  • I understand the `S ::= aSc` and `T ::= bTc` part but can you explain what is `S -> T` in your first production [`S ::= aSc | T | `]. – Storage Lenovo Nov 10 '18 at 23:58
  • The `|` is shorthand for showing different possible right-hand sides for `S`. The grammar is equivalent to: `S ::= aSc; S ::= T; S ::= ; T ::= bTc; T ::= ` Where `S` is the start symbol, and `T` is just another nonterminal symbol in the grammar, like `A` and `B` in the original post. The reason we need another nonterminal is to ensure that all `a`s are added before all `b`s are added; if we only used `S`, we would end up generating strings like `bacc`. – Ollin Boer Bohan Nov 11 '18 at 00:00
  • I see, so nonterminal `T` is used to connect `S` and `T` together. – Storage Lenovo Nov 11 '18 at 00:07
  • I suppose you could say that. – Ollin Boer Bohan Nov 11 '18 at 00:13
0

For L1 you can do it with

S -> aSc
S -> T
T -> aTb
T -> 

and analogous for L2.

Henning Koehler
  • 2,456
  • 1
  • 16
  • 20