• 作业十一——LL(1)文法的判断


    1. 文法 G(S):

    (1)S -> AB

    (2)A ->Da|ε

    (3)B -> cC

    (4)C -> aADC |ε

    (5)D -> b|ε

    验证文法 G(S)是不是 LL(1)文法?

    答:

    证明:FIRST(Da) = {b, a}

       FIRST(ε) = {ε}

       FIRST(aADC) = {a}

       FIRST(b) = {b}

       FOLLOW(A) = {c, b, a, #}

       FOLLOW(C) = {#,}

       FOLLOW(D) = {a, #}

       SELECT(A -> Da) = FIRST(Da) = {b, a}

       SELECT(A -> ε) = FIRST(ε) - {ε}UFOLLOW(A) = FOLLOW(A) = {c, b, a, #}

       因为SELECT(A -> Da) ∩ SELECT(A -> ε) ≠ Ø

       所以G(S)不是 LL(1)文法。

    2.法消除左递归之后的表达式文法是否是LL(1)文法?

    答:文法为:(1)E -> TE'

          (2)E' -> +TE' | ε

          (3)T -> FT'

          (4)T' -> *FT' | ε

          (5)F -> (E) | i

      FIRST(+TE') = {+}

      FIRST(ε) = {ε}

      FIRST(*FT') = {*}

      FIRST((E)) = { ( }

      FIRST(i) = {i}

      FOLLOW(E') = { ), # }

      FOLLOW(T') = {+, ), #}

      FOLLOW(F) = {*, +, ), #}

      SELECT(E' -> +TE') = FIRST(+TE') = {+}

      SELECT(E' -> ε) = FIRST(ε) - {ε}UFOLLOW(E') = FOLLOW(E') = { ), # }

      SELECT(T' -> *FT') = FIRST(*FT') = {*}

      SELECT(T' -> ε) = FIRST(ε) - {ε}UFOLLOW(T') = FOLLOW(T') = { +, ), # }

      SELECT(F -> (E)) = FIRST((E)) = { ( }

      SELECT(F->i)=FIRST(i)={i}

      因为SELECT(E' -> +TE') ∩ SELECT(E' -> ε) = Ø

        SELECT(T' -> *FT') ∩ SELECT(T' -> ε) = Ø

        SELECT(F -> (E)) ∩ SELECT(F -> i) = Ø

      所以此表达式文法是LL(1)文法。

    3.接2,如果是LL(1)文法,写出它的递归下降语法分析程序代码。

    E()

        {T();

           E'();

         }

    E'()

    T()

    T'()

    F()

    答:SELECT集:

      SELECT(E->TE')=FIRST(TE')={ (, i }

      SELECT(E'->+TE')=FIRST(+TE')={+}

      SELECT(E'->ε)=FIRST(ε)-{ε}UFOLLOW(E')=FOLLOW(E')={ ),# }

      SELECT(T->FT')=FIRST(FT')={ (,i }

      SELECT(T'->*FT')=FIRST(*FT')={*}

      SELECT(T'->ε)=FIRST(ε)-{ε}UFOLLOW(T')=FOLLOW(T')={ +,),# }

      SELECT(F->(E))=FIRST((E))={ ( }

      SELECT(F->i)=FIRST(i)={i}

      递归下降语法分析程序:

      void ParseE(){

        switch(lookahead){

          case '(','i':

            ParseT();

            ParseE'();

            break;

          default:

            print("syntax error ");

            exit(0);

        }

      }

      void ParseE'(){

        switch(lookahead){

          case '+':

            MatchToken('+');

            ParseT();

            ParseE'();

            break;

          case ')','#':

            break;

          default:

            print("syntax error ");

            exit(0);

        }

      }

      void ParseT(){ 

        switch(lookahead){

          case '(','i':

            ParseF();

            ParseT'();

            break;

          default:

            print("syntax error ");

            exit(0);

        }

      }

      void ParseT'(){

        switch(lookahead){

          case '*':

            MatchToken('*');

            ParseF();

            ParseT'();

            break;

          case '+',')','#':

            break;

          default:

            print("syntax error ");

            exit(0);

        }

      }

      void ParseF(){

        switch(lookahead){

          case '(':

            MatchToken('(');

            ParseE();

            MatchToken(')');

            break;

          case 'i':

            MatchToken('i');

            break;

          default:

            print("syntax error ");

            exit(0);

        }

      }

     4.加上实验一的词法分析程序,形成可运行的语法分析程序,分析任意输入的符号串是不是合法的表达式。

  • 相关阅读:
    Day3----《Pattern Recognition and Machine Learning》Christopher M. Bishop
    Day2----《Pattern Recognition and Machine Learning》Christopher M. Bishop
    学习笔记-----《Pattern Recognition and Machine Learning》Christopher M. Bishop
    “数学之美”笔记
    win10下使用nodejs安装及webstorm创建express项目的指导
    deepin/ubuntu下搭建Jekyll环境
    struts2.3.23升级到struts2.3.32
    struts2.5能不能再恶心点
    线程通信、线程同步以及进城通信的真的搞懂了么
    WINFORM数据库操作,有点像安装里面的SQLITE
  • 原文地址:https://www.cnblogs.com/xiaolan-Lin/p/11887114.html
Copyright © 2020-2023  润新知