Skip to content

Latest commit

 

History

History
76 lines (60 loc) · 2.62 KB

File metadata and controls

76 lines (60 loc) · 2.62 KB
    G中的规则r Frist(rhs(r)) Follow(lhs(r)) PS(r)
    $S\rightarrow P$ $(,a$ $\text#$ $(,a$
    $S\rightarrow\epsilon$ $\epsilon$ None $\text#$
    $P\rightarrow(P)P$ $($ $\text#,)$ $($
    $P\rightarrow a$ $a$ None $a$
    1. 是LL(1)文法.因为$PS(S\rightarrow P)\cap PS(S\rightarrow\epsilon)=\varnothing$,且$PS(P\rightarrow(P)P)\cap PS(P\rightarrow a)=\varnothing$.

    2. 预测分析表

      a ( ) #
      S $S\rightarrow P$ $S\rightarrow P$ $S\rightarrow\epsilon$
      P $P\rightarrow a$ $P\rightarrow(P)P$
    3. int ParseP(int P_y) {
        switch (lookahead) {
          case '(': {
            MatchToken('(');
            int P1_y = P_y;
            int P1_x = ParseP(P1_y);
            MatchToken(')');
            int P2_y = P1_x;
            int P2_x = ParseP(P2_y);
            return P1_x + P2_x;
          }
          case 'a': {
            return P_y + 1;
          }
          default:
            reportError();
        }
      }
      int ParseS() {
        switch (lookahead) {
          case '(':
          case 'a': {
            int P_y = 0;
            int P_x = ParseP(P_y);
            return P_x;
          }
          case '#': {
            return 0;
          }
          default: {
            reportError();
          }
        }
      }
      

    $S\rightarrow{switch} A of \text{{L.val=A.place,L.next=S.next} L end} \ \text{{S.code=A.code//L.code}}$

    $L\rightarrow case \text{{V.failed=newlabel}} V : \text{{S.next=L.next}}; \text{{L1.val=L.val, L1.next=L.next}}\\text{{L.code=gen(if V.val != L.val then goto V.failed)// S.code//gen(goto L.next)//gen(V.failed :)//L.code}}$

    $L\rightarrow default \text{{S.next=L.next}} S \text{{L.code=S.code//gen(goto L.next)}}$

    $V\rightarrow d \text{{V.val=d.lexval}}$

    $S\rightarrow while \text{{E.true=newlabel, E.false=S.next}} E do \ \text{{S1.next=E.newlabel, S1.break=S.next}} S1\ \text{{S.code=gen(S1.next :)//E.code//gen(E.true :)//S1.code//gen(goto S1.next)}}$

    $S\rightarrow \text{{S1.break=S.break, S1.next=newlabel}}S1\text{{S2.break=S.break, S2.next.S.next}}S2\ \text{{S.code=S1.code//gen(S1.next : )// S2.code}}$

    $S\rightarrow break \text{{gen(goto S.break)}}$