Converting grammar to Chomsky Normal Form?

后端 未结 3 1508
伪装坚强ぢ
伪装坚强ぢ 2021-01-02 05:02

Convert the grammar below into Chomsky Normal Form. Give all the intermediate steps.

S -> AB | aB
A -> aab|lambda
B -> bbA

Ok so t

3条回答
  •  鱼传尺愫
    2021-01-02 05:34

    Alternative answer: The grammar can only produce a finite number of strings, namely 6.

     S -> aabbbaab | aabbb | bbaab | bb | abbaab | abb.
    

    You can now condense this back to Chomsky Normal Form by hand.


    By substitution, we can find the set of all strings produced. Your initial rules:

    S -> AB | aB.
    A -> aab | lambda.
    B -> bbA.
    

    First split up the S rule:

    S -> AB.
    S -> aB.
    

    Now substitute what A and B expand into:

    S -> AB
      -> (aab | lambda) bbA
      -> (aab | lambda) bb (aab | lambda).
    S -> aB
      -> abbA
      -> abb (aab | lambda).
    

    Expand these again to get:

    S -> aabbbaab.
    S -> aabbb.
    S -> bbaab.
    S -> bb.
    S -> abbaab.
    S -> abb.
    

    To change this finite set to Chomsky Normal Form, it suffices to do it by brute force without any intelligent factoring. First we introduce two terminal rules:

    X -> a.
    Y -> b.
    

    Now for each string, we consume the first letter with a terminal variable and the remaining letters with a new variables. For example, like this:

    S -> aabbb. (initial rule, not in Chomsky Normal Form)
    
    S -> XC, where X->a and C->abbb.
    C -> XD, where X->a and D->bbb.
    D -> YE, where Y->b and E->bb.
    E -> YY, where Y->b and Y->b.
    

    We just go through this process for all 6 strings, generating a lot of new intermediate variables.

提交回复
热议问题