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

  1. (1 × 15)

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

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

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

  3. (5 × 4)

    (1)(E) (2)(J) (3)(L) (4)(Q)