Exercise 1        Name __________________        Score __/18

  1. (9) Draw the AST (similar to FIGURE 1.4, p. 9) for the straight-line program of:
    1. a := 46
                         AssignStm
                           /       \
                          a     NumExp
                                     |
                                    46
       
    2. a :=46; print (a)

                                      CompoundStm
                                      /                   \
                         AssignStm               PrintStm
                           /       \                        |
                          a     NumExp            LastExpList
                                     |                      |
                                    46                  IdExp
                                                            |
                                                            a
       
    3. a :=46; b := 4 * a; print (a, b)

                               CompoundStm
                               /                   \
                              /                   CompoundStm
                             /                     /                  \
                    AssignStm             AssignStm         PrintStm
                      /       \                 /         \                      |
                     a     NumExp       a       OpExp                 PairExpList
                                 |                     /   |   \                /              \
                                46          NumExp  *  IdExp        IdExp       LastExpList
                                                   |             |              |                |
                                                   4            a              a             IdExp
                                                                                                  |
                                                                                                  b
       
  2. (9) Give the Java (similar to p. 12) for the straight-line program of:
    1. a := 46;

      Stm program = new AssignStm( "a",
                                    new NumExp(46)
                              );
       
    2. a :=46; print (a)

      Stm program = new CompoundStm(
                                new AssignStm( "a",
                                    new NumExp(46)
                                ),
                                new PrintStm(
                                    new LastExpList(
                                          new IdExp("a")
                                    )
                                )
                             );
       
    3. a :=46; b := 4 * a; print (a, b)

      Stm program =
                          new CompoundStm(
                                new AssignStm( "a",
                                    new NumExp(46)
                                ),
                                new CompoundStm(
                                     new AssignStm( "b",
                                          new OpExp(
                                              new NumExp(4),
                                              Times,
                                              new IdExp("a")
                                          )
                                      ),
                                      new PrintStm(
                                          new PairExp(
                                              new IdExp("a"),
                                              new LastExpList(
                                                  new IdExp("b")
                                              )
                                          )
                                      )
                                 )
                             );