コンパイラ・期末テスト解答例(2024 年 02 月 13 日)

  1. (1 × 15)

    (1) (2) (3) (4) (5)
    (6) (7) (8) (9) (10)
    (11) (12) (13)

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

    (1-ⅰ) { id ! id : id , id : id } など (1-ⅱ) { id ! id : { id ! id : id } } など
    (1-ⅲ) (1-ⅳ)
    (2) \(\begin{array}{lcll} E & \rightarrow & {\color{red}\textbf{id}\ E'} & {\color{red}\cdots ①}\\ & | & {\color{red}\texttt{\{}\ E\ \texttt{!}\ L\ \texttt{\}}\ E'} & {\color{red}\cdots ②}\\ \\ E' & \rightarrow & {\color{red}\texttt{(}\ E\ \texttt{)}\ E'} & {\color{red}\cdots ③}\\ & | & {\color{red}\varepsilon} & {\color{red}\cdots ④}\\ \\ \\ L & \rightarrow & {\color{red}S\ L'} & {\color{red}\cdots ⑤} \\ \\ L' & \rightarrow & {\color{red}\texttt{,}\ S\ L'}& {\color{red}\cdots ⑥} \\ \ & | & {\color{red}\varepsilon} & {\color{red}\cdots ⑦} \\ \\ \end{array}\)
    (3)\(\textbf{id}\)
    (4)“\(\texttt{\}}\)”
    (5)$, “\(\texttt{!}\)”, “\(\texttt{)}\)”, “\(\texttt{\}}\)”, “\(\texttt{,}\)”
    (6)
    \(\textbf{id}\) \(\texttt{\{}\) \(\texttt{!}\) \(\texttt{\}}\) \(\texttt{(}\) \(\texttt{)}\) \(\texttt{,}\) \(\texttt{:}\) $
    \(S \rightarrow\)  Ⓐ  ✘  ✘  ✘  ✘  ✘  ✘  ✘  ✘
    (7)
    \(\textbf{id}\) \(\texttt{\{}\) \(\texttt{!}\) \(\texttt{\}}\) \(\texttt{(}\) \(\texttt{)}\) \(\texttt{,}\) \(\texttt{:}\) $
    \(E \rightarrow\)  ①  ②  ✘  ✘  ✘  ✘  ✘  ✘  ✘
    \(E' \rightarrow\)  ✘  ✘  ④  ④  ③  ④  ④  ✘  ④
    \(L \rightarrow\)  ⑤  ✘  ✘  ✘  ✘  ✘  ✘  ✘  ✘
    \(L' \rightarrow\)  ✘  ✘  ✘  ⑦  ✘  ✘  ⑥  ✘  ✘
    (8)
    void L(void) {
        switch (token) {
          case ID:
            S(); L1(); break;
          default:
            reportError();
        }
    }
    
    void L1(void) {
        switch (token) {
          case ',':
            eat(','); S(); L1(); break;
          case '}':
            break;
          default:
            reportError();
        }
    }
          

  3. (5 × 4)

    (1)(A) (2)(C) (3)(A) (4)(C)