コンパイラ・期末テスト・解答例

  1. (2 × 10)

    (1) ⋖ (2) ⋖ (3) ⋗ (4) ⋗ (5) ✘
    (6) ⋗ (7) ⋖ (8) ⋗ (9) ✘ (10) ⋖

  2. (2 × 4, 5, 3, 3, 3, 6, 6, 6)

    (1-ⅰ) { a = a ; } など
    (1-ⅱ) a = a ; など
    (1-ⅲ) ✘
    (1-ⅳ) ✘
    (2) \(\begin{array}{lcl} E & \rightarrow & \textbf{a}\ E' & \cdots ⑦ \\ & | & \texttt{(}\ E\ \texttt{)}\ E' & \cdots ⑧ \\ \\ E' & \rightarrow & \texttt{(}\ E\ \texttt{)}\ E' & \cdots ⑨ \\ & | & \texttt{[}\ E\ \texttt{]}\ E' & \cdots ⑩\\ & | & \varepsilon & \cdots ⑪ \\ \\ \end{array}\)
    (3)\(\left\{\ \textbf{a}, \textbf{if}, \textbf{while}, “\texttt{\{}” \ \right\}\)
    (4)\(\left\{\ “\texttt{\}}”\ \right\}\ \)
    (5)\(\left\{\ “\texttt{;}”, “\texttt{)}”, “\texttt{]}”\ \right\}\ \)
    (6)
    a = ; if ( ) else while { } [ ] $
    \(S \rightarrow\)  ①  ✘  ✘  ②  ✘  ✘  ✘  ③  ④  ✘  ✘  ✘  ✘
    \(T \rightarrow\)  ⑤  ✘  ✘  ⑤  ✘  ✘  ✘  ⑤  ⑤  ⑥  ✘  ✘  ✘
    (7)
    a = ; if ( ) else while { } [ ] $
    \(E \rightarrow\)  ⑦  ✘  ✘  ✘  ⑧  ✘  ✘  ✘  ✘  ✘  ✘  ✘  ✘
    \(E' \rightarrow\)  ✘  ✘  ⑪  ✘  ⑨  ⑪  ✘  ✘  ✘  ✘  ⑩  ⑪  ✘
    (8)
    void E(void) {
        switch(token) {
          case A:    eat(A); E1(); break;
          case '(':  eat('('); E(); eat(')'); E1(); break;
          default:   reportError();
        }
    }
    
    void E1(void) {
        switch(token) {
          case '(': eat('('); E(); eat(')'); E1(); break;
          case '[': eat('['); E(); eat(']'); E1(); break;
          case ';': case ')': case ']': break;
          default:  reportError();   
        }
    }
    
          

  3. (5 × 4)

    (1) (B) (2) (C) (3) (E) (4) (C)