1

I have the following grammar,

  1. S -> Sb
  2. S -> aaSb
  3. S -> b

The typical derivations in this grammar are
S => Sb => [aaSb]b => [aa[b]b]b => aabbb for n = 1
S => Sb => [aaSb]b => [aa[aaSb]b]b => [aa[aabb]b]b => aaaabbbb for n = 2

Edit: So I claimed that this grammar generates the language
L = {a^(2n)b^(n+2) : n >= 1}

I am pretty sure that my a goes a^(2n) since there's two a before S, but what about b. There is no lambda here so my n goes from n >= 1?.

Edit:
b^(n+1) and b^(2n+1) are both wrong assumptions because the grammar can derive a string aaaaaabbbbb if n = 3.
I modified my b to be b^(n+2). so that L becomes L = {a^(2n)b^(n+2) : n >= 1}

Storage Lenovo
  • 85
  • 1
  • 1
  • 6
  • Maybe this is new to me, but is this a software programming question? – stealththeninja Nov 09 '18 at 02:31
  • Not really, this is more like Computer Science question. – Storage Lenovo Nov 09 '18 at 02:35
  • The grammar can produce `aaaaaabbbbb` which doesn't match the form you're proposing. (I'm pretty sure a CFG _can't_ produce a language with an exponential number of symbols, though I can't prove that in this comment.) – David Maze Nov 09 '18 at 03:04
  • You are right the grammar produces `aaaaaabbbbb` if `n = 3` only if _b_ is `b^(n+2)`. So my assumption that `b^(2n)` or `b^(2n+1)` is wrong. – Storage Lenovo Nov 09 '18 at 03:15
  • Production 1 means that you can add an arbitrary number of `b`s at the end. That makes an equality relationship pretty unlikely. – rici Nov 09 '18 at 04:51
  • When they say describe _L(G)_, you have to provide a language _L_ for that grammar? Or you just explain the grammar given each production? – Storage Lenovo Nov 09 '18 at 04:56
  • You can't link the number of occurrences of `a` and `b` so tightly: for any derivation from S, you can modify it to add arbitrarily many applications of production 1, which will add arbitrarily many occurrences of `b` without changing the number of `a`s. – Michael Dyck Nov 09 '18 at 18:13

2 Answers2

0

The language generated by this grammar is a^(2n) b^(n+m+1) where n and m are natural numbers. To show this, (a) we see that any string derived using the grammar's productions matches the above and (b) any string matching the above can be derived using the grammar's productions.

(a) The grammar can and must use rule (3) exactly once. This gives the +1 in the number of bs. Execution of rule (2) can happen some number of times n, putting 2n as on the front and n bs on the back, hence the 2n and n terms. Rule (1) can be executed any number of times m, hence the term.

(b) Given a string a^(2n) b^(n+m+1) for natural numbers n and m: use rule (1) a number of times equal to m; then, use rule (2) a number of times equal to n; then, user rule (3) once. Thus, the grammar generates the string.

Another way to write the same answer is a^2n b^m with m > n.

Patrick87
  • 27,682
  • 3
  • 38
  • 73
0

One way to tackle this is to rewrite the grammar. Note that productions 1 and 2 both end in Sb, so we can left-factor them:

S -> ASb
S -> b
A -> 
A -> aa

From the first two productions, it's fairly easy to see that S generates A^n b^{n+1} for n >= 0.

From the last two productions, A^n generates a^{2k} for 0 <= k <= n.

So the language is a^{2k} b^{n+1} for n >= 0, 0 <= k <= n.

Or equivalently, let m = n - k to obtain a^{2k} b^{k+m+1} for k,m >= 0.

Michael Dyck
  • 2,153
  • 1
  • 14
  • 18