Context-free grammar for the language L = {a^(n)b^(m)c^(k): m = |i - k|}
up vote
1
down vote
favorite
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
context-free-grammar regular-language formal-languages context-free-language
add a comment |
up vote
1
down vote
favorite
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
context-free-grammar regular-language formal-languages context-free-language
I'm voting to close this question as off-topic because it is not about computer programming.
– Raymond Chen
Nov 11 at 0:47
Is there a way to move this to another site? In CS stack exchange or Math perhaps.
– Storage Lenovo
Nov 11 at 1:05
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
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
context-free-grammar regular-language formal-languages context-free-language
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
context-free-grammar regular-language formal-languages context-free-language
context-free-grammar regular-language formal-languages context-free-language
edited Nov 11 at 0:03
asked Nov 10 at 23:28
Storage Lenovo
153
153
I'm voting to close this question as off-topic because it is not about computer programming.
– Raymond Chen
Nov 11 at 0:47
Is there a way to move this to another site? In CS stack exchange or Math perhaps.
– Storage Lenovo
Nov 11 at 1:05
add a comment |
I'm voting to close this question as off-topic because it is not about computer programming.
– Raymond Chen
Nov 11 at 0:47
Is there a way to move this to another site? In CS stack exchange or Math perhaps.
– Storage Lenovo
Nov 11 at 1:05
I'm voting to close this question as off-topic because it is not about computer programming.
– Raymond Chen
Nov 11 at 0:47
I'm voting to close this question as off-topic because it is not about computer programming.
– Raymond Chen
Nov 11 at 0:47
Is there a way to move this to another site? In CS stack exchange or Math perhaps.
– Storage Lenovo
Nov 11 at 1:05
Is there a way to move this to another site? In CS stack exchange or Math perhaps.
– Storage Lenovo
Nov 11 at 1:05
add a comment |
2 Answers
2
active
oldest
votes
up vote
0
down vote
accepted
For L1 you can do it with
S -> aSc
S -> T
T -> aTb
T ->
and analogous for L2.
add a comment |
up vote
0
down vote
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 a
s is the same as the number of b
s), 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 c
s is the number of a
s plus the number of b
s), 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.
I understand theS ::= aSc
andT ::= bTc
part but can you explain what isS -> T
in your first production [S ::= aSc | T | <empty string>
].
– Storage Lenovo
Nov 10 at 23:58
The|
is shorthand for showing different possible right-hand sides forS
. The grammar is equivalent to:S ::= aSc; S ::= T; S ::= <empty string>; T ::= bTc; T ::= <empty string>
WhereS
is the start symbol, andT
is just another nonterminal symbol in the grammar, likeA
andB
in the original post. The reason we need another nonterminal is to ensure that alla
s are added before allb
s are added; if we only usedS
, we would end up generating strings likebacc
.
– Ollin Boer Bohan
Nov 11 at 0:00
I see, so nonterminalT
is used to connectS
andT
together.
– Storage Lenovo
Nov 11 at 0:07
I suppose you could say that.
– Ollin Boer Bohan
Nov 11 at 0:13
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
For L1 you can do it with
S -> aSc
S -> T
T -> aTb
T ->
and analogous for L2.
add a comment |
up vote
0
down vote
accepted
For L1 you can do it with
S -> aSc
S -> T
T -> aTb
T ->
and analogous for L2.
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
For L1 you can do it with
S -> aSc
S -> T
T -> aTb
T ->
and analogous for L2.
For L1 you can do it with
S -> aSc
S -> T
T -> aTb
T ->
and analogous for L2.
answered Nov 10 at 23:53
Henning Koehler
1,100610
1,100610
add a comment |
add a comment |
up vote
0
down vote
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 a
s is the same as the number of b
s), 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 c
s is the number of a
s plus the number of b
s), 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.
I understand theS ::= aSc
andT ::= bTc
part but can you explain what isS -> T
in your first production [S ::= aSc | T | <empty string>
].
– Storage Lenovo
Nov 10 at 23:58
The|
is shorthand for showing different possible right-hand sides forS
. The grammar is equivalent to:S ::= aSc; S ::= T; S ::= <empty string>; T ::= bTc; T ::= <empty string>
WhereS
is the start symbol, andT
is just another nonterminal symbol in the grammar, likeA
andB
in the original post. The reason we need another nonterminal is to ensure that alla
s are added before allb
s are added; if we only usedS
, we would end up generating strings likebacc
.
– Ollin Boer Bohan
Nov 11 at 0:00
I see, so nonterminalT
is used to connectS
andT
together.
– Storage Lenovo
Nov 11 at 0:07
I suppose you could say that.
– Ollin Boer Bohan
Nov 11 at 0:13
add a comment |
up vote
0
down vote
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 a
s is the same as the number of b
s), 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 c
s is the number of a
s plus the number of b
s), 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.
I understand theS ::= aSc
andT ::= bTc
part but can you explain what isS -> T
in your first production [S ::= aSc | T | <empty string>
].
– Storage Lenovo
Nov 10 at 23:58
The|
is shorthand for showing different possible right-hand sides forS
. The grammar is equivalent to:S ::= aSc; S ::= T; S ::= <empty string>; T ::= bTc; T ::= <empty string>
WhereS
is the start symbol, andT
is just another nonterminal symbol in the grammar, likeA
andB
in the original post. The reason we need another nonterminal is to ensure that alla
s are added before allb
s are added; if we only usedS
, we would end up generating strings likebacc
.
– Ollin Boer Bohan
Nov 11 at 0:00
I see, so nonterminalT
is used to connectS
andT
together.
– Storage Lenovo
Nov 11 at 0:07
I suppose you could say that.
– Ollin Boer Bohan
Nov 11 at 0:13
add a comment |
up vote
0
down vote
up vote
0
down vote
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 a
s is the same as the number of b
s), 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 c
s is the number of a
s plus the number of b
s), 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.
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 a
s is the same as the number of b
s), 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 c
s is the number of a
s plus the number of b
s), 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.
answered Nov 10 at 23:54
Ollin Boer Bohan
1,365310
1,365310
I understand theS ::= aSc
andT ::= bTc
part but can you explain what isS -> T
in your first production [S ::= aSc | T | <empty string>
].
– Storage Lenovo
Nov 10 at 23:58
The|
is shorthand for showing different possible right-hand sides forS
. The grammar is equivalent to:S ::= aSc; S ::= T; S ::= <empty string>; T ::= bTc; T ::= <empty string>
WhereS
is the start symbol, andT
is just another nonterminal symbol in the grammar, likeA
andB
in the original post. The reason we need another nonterminal is to ensure that alla
s are added before allb
s are added; if we only usedS
, we would end up generating strings likebacc
.
– Ollin Boer Bohan
Nov 11 at 0:00
I see, so nonterminalT
is used to connectS
andT
together.
– Storage Lenovo
Nov 11 at 0:07
I suppose you could say that.
– Ollin Boer Bohan
Nov 11 at 0:13
add a comment |
I understand theS ::= aSc
andT ::= bTc
part but can you explain what isS -> T
in your first production [S ::= aSc | T | <empty string>
].
– Storage Lenovo
Nov 10 at 23:58
The|
is shorthand for showing different possible right-hand sides forS
. The grammar is equivalent to:S ::= aSc; S ::= T; S ::= <empty string>; T ::= bTc; T ::= <empty string>
WhereS
is the start symbol, andT
is just another nonterminal symbol in the grammar, likeA
andB
in the original post. The reason we need another nonterminal is to ensure that alla
s are added before allb
s are added; if we only usedS
, we would end up generating strings likebacc
.
– Ollin Boer Bohan
Nov 11 at 0:00
I see, so nonterminalT
is used to connectS
andT
together.
– Storage Lenovo
Nov 11 at 0:07
I suppose you could say that.
– Ollin Boer Bohan
Nov 11 at 0:13
I understand the
S ::= aSc
and T ::= bTc
part but can you explain what is S -> T
in your first production [S ::= aSc | T | <empty string>
].– Storage Lenovo
Nov 10 at 23:58
I understand the
S ::= aSc
and T ::= bTc
part but can you explain what is S -> T
in your first production [S ::= aSc | T | <empty string>
].– Storage Lenovo
Nov 10 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 ::= <empty string>; T ::= bTc; T ::= <empty string>
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 at 0:00
The
|
is shorthand for showing different possible right-hand sides for S
. The grammar is equivalent to: S ::= aSc; S ::= T; S ::= <empty string>; T ::= bTc; T ::= <empty string>
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 at 0:00
I see, so nonterminal
T
is used to connect S
and T
together.– Storage Lenovo
Nov 11 at 0:07
I see, so nonterminal
T
is used to connect S
and T
together.– Storage Lenovo
Nov 11 at 0:07
I suppose you could say that.
– Ollin Boer Bohan
Nov 11 at 0:13
I suppose you could say that.
– Ollin Boer Bohan
Nov 11 at 0:13
add a comment |
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%2f53244426%2fcontext-free-grammar-for-the-language-l-anbmck-m-i-k%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
I'm voting to close this question as off-topic because it is not about computer programming.
– Raymond Chen
Nov 11 at 0:47
Is there a way to move this to another site? In CS stack exchange or Math perhaps.
– Storage Lenovo
Nov 11 at 1:05