0

The language is defined as follows:

L = {a^m b^n c^k | k = |m-n| }

So a's followed by b's followed by c's, where the number of c's is the absolute value of the a's minus b's.

I can't figure out how to capture this behavior in a CFG. Any advice?

AnthonyG
  • 3
  • 1
  • Try splitting into two languages/CFGs, one that handles m >= n and one that handles m <= n. Then get the union (trivial in CFGs) – Chris Dodd May 09 '18 at 01:26

1 Answers1

0

Proceeding from Chris Dodd's suggestion:

L1 = {a^m b^n c^k | m >= n && k = m - n}
   = {a^m b^n c^k | m >= n && m = k + n}
   = {a^k a^n b^n c^k | m >= n}

By rearranging the condition we transform subtraction into addition. Then, we split a^m to make the structure of the grammar easier to see. The grammar is therefore:

S := aSc | X | e
X := aXb | e

The S rule handles the outside and the X rule handles the inside.

L2 = {a^m b^n c^k | m < n && k = n - m}
   = {a^m b^n c^k | m < n && n = m + k}
   = {a^m b^m b^k c^k | m < n}

Similar procedure, though the structure is a little different and we end up with something like:

S' := X'Y'
X' := aX'b | e
Y' := bY'c | e

Combining them gives a grammar for the union:

S   := S' | S''
S'  := aS'c | X | e
X   := aXb | e
S'' := X'Y'
X'  := aX'b | e
Y'  := bY'c | e
Patrick87
  • 27,682
  • 3
  • 38
  • 73