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










share|improve this question
























  • 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















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










share|improve this question
























  • 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













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










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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












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.






share|improve this answer




























    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 as is the same as the number of bs), 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 cs is the number of as plus the number of bs), 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.






    share|improve this answer





















    • 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 as are added before all bs 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 suppose you could say that.
      – Ollin Boer Bohan
      Nov 11 at 0:13











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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.






    share|improve this answer

























      up vote
      0
      down vote



      accepted










      For L1 you can do it with



      S -> aSc
      S -> T
      T -> aTb
      T ->


      and analogous for L2.






      share|improve this answer























        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.






        share|improve this answer












        For L1 you can do it with



        S -> aSc
        S -> T
        T -> aTb
        T ->


        and analogous for L2.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 10 at 23:53









        Henning Koehler

        1,100610




        1,100610
























            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 as is the same as the number of bs), 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 cs is the number of as plus the number of bs), 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.






            share|improve this answer





















            • 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 as are added before all bs 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 suppose you could say that.
              – Ollin Boer Bohan
              Nov 11 at 0:13















            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 as is the same as the number of bs), 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 cs is the number of as plus the number of bs), 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.






            share|improve this answer





















            • 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 as are added before all bs 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 suppose you could say that.
              – Ollin Boer Bohan
              Nov 11 at 0:13













            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 as is the same as the number of bs), 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 cs is the number of as plus the number of bs), 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.






            share|improve this answer












            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 as is the same as the number of bs), 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 cs is the number of as plus the number of bs), 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.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 10 at 23:54









            Ollin Boer Bohan

            1,365310




            1,365310












            • 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 as are added before all bs 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 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












            • 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 as are added before all bs 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 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 as are added before all bs 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 as are added before all bs 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


















             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Full-time equivalent

            Bicuculline

            さくらももこ