1

During exercise, I am supposed to write a Context-Free Grammar for the following language:

enter image description here

I am not sure I fully understand the approach, but here is what I got.

Since we need at least 1 c surrounded by any equal numbers of a's and b's (could be zero) I came up with the following CFG:

T --> aCb | aTb | C
C --> cS | cC
S --> empty

From above, I can for instance never create a string, without atleast 1 c in it. But I can create a string with just a c and no a's or b's. Similar I can create strings with aa...c...bb (with any number of a's and b's with just 1 c in between) as well as any strings similar to the previous but with any number of c's as well.

However, I feel like this CTF is somewhat more complex that what needs be. Can anyone tell me how to improve if, or in the case it is wrong - what I am missing?

Edit: after some good inputs from rici what I arrive at are now:

T --> aTb | cC
C --> cC | empty

By removing any redundancy (such as aCb which could be achieved through aTb and C) as well as the non-terminal S.

NewDev90
  • 379
  • 2
  • 21

1 Answers1

1
  1. Eliminate S. It's not doing anything other than collecting a paycheque.

  2. T → a C b is redundant since you already have T → a T b and T → C, which obviously can do the same thing (by applying them in that order).

rici
  • 234,347
  • 28
  • 237
  • 341
  • Thank you for your feedback. However, I am not sure I believe that S are not needed. Since the language requires there be at least one 'c' I must somehow force the CFG to produce this 'c', before it can produce an empty result (usually given as epsilon). So if I remove the S, I would have to add the empty somewhere else, and in this case how can I still ensure that there is at least one 'c' in my expression? However, you're exactly right about the aCb being redundant thanks! :) – NewDev90 Apr 29 '20 at 20:29
  • `S` only appears in one place in the grammar, where it expands to nothing. (ε is just a way to write nothing so that it is visible.) Since you can always substitute a non-terminal with its derivation, you can rewrite `C → cS` with the precisely equivalent `C→c`. So `S` contributes nothing. Don't overcomplicate this stuff. They're called rewriting rules because they are just a way of saying "you can replace this with that". It's just a game with symbols, nothing more. – rici Apr 29 '20 at 20:35
  • Hmm I get what you mean, so if I rewrite the expression such that `T` produces `aTb` | `cC` and then have `C` produce `cC` | `empty` it is more clean - and I still have the property that I must produce at least one `c`? – NewDev90 Apr 29 '20 at 20:47
  • @ComSciStu: Why are you so fixated on C producing empty? `T→aTb|C; C→cC|c` will work just fine. But you can move the `c` from `C`'s base case to `T`'s base case if you want to; here, it makes little difference. – rici Apr 29 '20 at 21:28
  • I think it is probably due to what I have learned so far, where it seems important that you include the empty string somehow. But I see your point in terms of just having `c` produce a `c` and nothing else is valid, and produces the same result as if I just produced `cC` and `empty`. – NewDev90 Apr 30 '20 at 07:35
  • There is no need to include the empty string. In fact, it is often convenient to eliminate it from grammars. Either you misunderstood something your instructor said or they were mistaken. – rici Apr 30 '20 at 13:42