Describe the language generated by this context-free grammar
up vote
1
down vote
favorite
I have the following grammar,
S -> SbS -> aaSbS -> 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
context-free-grammar regular-language formal-languages context-free-language
|
show 2 more comments
up vote
1
down vote
favorite
I have the following grammar,
S -> SbS -> aaSbS -> 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
context-free-grammar regular-language formal-languages context-free-language
Maybe this is new to me, but is this a software programming question?
– stealththeninja
Nov 9 at 2:31
Not really, this is more like Computer Science question.
– Storage Lenovo
Nov 9 at 2:35
The grammar can produceaaaaaabbbbbwhich 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 9 at 3:04
You are right the grammar producesaaaaaabbbbbifn = 3only if b isb^(n+2). So my assumption thatb^(2n)orb^(2n+1)is wrong.
– Storage Lenovo
Nov 9 at 3:15
Production 1 means that you can add an arbitrary number ofbs at the end. That makes an equality relationship pretty unlikely.
– rici
Nov 9 at 4:51
|
show 2 more comments
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have the following grammar,
S -> SbS -> aaSbS -> 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
context-free-grammar regular-language formal-languages context-free-language
I have the following grammar,
S -> SbS -> aaSbS -> 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
context-free-grammar regular-language formal-languages context-free-language
context-free-grammar regular-language formal-languages context-free-language
edited Nov 9 at 6:51
asked Nov 9 at 2:30
Storage Lenovo
153
153
Maybe this is new to me, but is this a software programming question?
– stealththeninja
Nov 9 at 2:31
Not really, this is more like Computer Science question.
– Storage Lenovo
Nov 9 at 2:35
The grammar can produceaaaaaabbbbbwhich 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 9 at 3:04
You are right the grammar producesaaaaaabbbbbifn = 3only if b isb^(n+2). So my assumption thatb^(2n)orb^(2n+1)is wrong.
– Storage Lenovo
Nov 9 at 3:15
Production 1 means that you can add an arbitrary number ofbs at the end. That makes an equality relationship pretty unlikely.
– rici
Nov 9 at 4:51
|
show 2 more comments
Maybe this is new to me, but is this a software programming question?
– stealththeninja
Nov 9 at 2:31
Not really, this is more like Computer Science question.
– Storage Lenovo
Nov 9 at 2:35
The grammar can produceaaaaaabbbbbwhich 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 9 at 3:04
You are right the grammar producesaaaaaabbbbbifn = 3only if b isb^(n+2). So my assumption thatb^(2n)orb^(2n+1)is wrong.
– Storage Lenovo
Nov 9 at 3:15
Production 1 means that you can add an arbitrary number ofbs at the end. That makes an equality relationship pretty unlikely.
– rici
Nov 9 at 4:51
Maybe this is new to me, but is this a software programming question?
– stealththeninja
Nov 9 at 2:31
Maybe this is new to me, but is this a software programming question?
– stealththeninja
Nov 9 at 2:31
Not really, this is more like Computer Science question.
– Storage Lenovo
Nov 9 at 2:35
Not really, this is more like Computer Science question.
– Storage Lenovo
Nov 9 at 2: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 9 at 3:04
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 9 at 3: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 9 at 3:15
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 9 at 3:15
Production 1 means that you can add an arbitrary number of
bs at the end. That makes an equality relationship pretty unlikely.– rici
Nov 9 at 4:51
Production 1 means that you can add an arbitrary number of
bs at the end. That makes an equality relationship pretty unlikely.– rici
Nov 9 at 4:51
|
show 2 more comments
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
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.
add a comment |
up vote
0
down vote
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.
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
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.
add a comment |
up vote
0
down vote
accepted
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.
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
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.
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.
answered Nov 9 at 18:27
Patrick87
17.6k32659
17.6k32659
add a comment |
add a comment |
up vote
0
down vote
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.
add a comment |
up vote
0
down vote
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.
add a comment |
up vote
0
down vote
up vote
0
down vote
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.
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.
answered Nov 9 at 18:38
Michael Dyck
838611
838611
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53219010%2fdescribe-the-language-generated-by-this-context-free-grammar%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Maybe this is new to me, but is this a software programming question?
– stealththeninja
Nov 9 at 2:31
Not really, this is more like Computer Science question.
– Storage Lenovo
Nov 9 at 2:35
The grammar can produce
aaaaaabbbbbwhich 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 9 at 3:04
You are right the grammar produces
aaaaaabbbbbifn = 3only if b isb^(n+2). So my assumption thatb^(2n)orb^(2n+1)is wrong.– Storage Lenovo
Nov 9 at 3:15
Production 1 means that you can add an arbitrary number of
bs at the end. That makes an equality relationship pretty unlikely.– rici
Nov 9 at 4:51