Answers

  1. From the text question 5.1
    (4*7+1)*2
                            L
                            |
                            E val= 58
                            |
                            T val= 58
                         /  |  \
                 val=29 T   *   F val = 2
                        |       | 
                 val=29 F      digit =2
                      / |  \
                    /   |      \
                   (    E val=29 )
                      / | \
                    /   |   \
            val=28 E    +     T val=1
                   |           \
            val=28 T            F val= 1
                 / | \          |
          val=4 T  *   F val=7  digit=1
               |       |
          val=4F      digit=7
               |
             digit=4
    

  2. From the text question 5.2 (a)
    ((a)+(b))
    The parse tree:                           The syntax tree constructed:
                              E                
                              |                 First mkleaf(id,a'entry)
                              T                 Then mkleaf(id,b'entry)
                           /  |  \              Finally, mknode(+) points at
                         (    E    )                 the two leaves
                           /  |  \
                         E    +     T
                         |        / | \
                         T      (   E   )
                       / | \        |                     +
                     (   E   )      T                    /  \
                         |          |                    |    \
                         T         id=b                  |   (id,b'entry)
                         |                               |
                        id=a                      (id,a'entry)
    
    

  3. From the text question 5.8(a)
    (a)
    
    S -> L1 . L2  {S.val = L1.val + L2.val/(2**L2.bits)    **exponentiation
      |  L        {S.val = L.val; S.bits = L.bits}
    L -> L1 B     {L.val = L1.val*2 + B.val; L.bits = L1.bits + 1}
      |  B        {L.val = B.val; L.bits = 1}
    B -> 0        {B.val = 0}
      |  1        {B.val = 1}
    

  4. The following grammar describes (at a high level) part of the syntax of a typical ALGOL-like language. (Notation: non-terminals are in CAPITALS and terminals are in lower case The details of ASSIGNMENT are uninteresting.)
    PROGRAM      --> procedure STMT_LIST   
    STMT_LIST    --> STMT STMT_LIST   
                  |    STMT   
    STMT         --> ASSIGNMENT   
                  |     do var = constant to constant begin STMT_LIST end   
                  |     if var then  STMT_LIST  else  STMT_LIST endif
    
    As a first step toward understanding a program, it is interesting to understand the number of times that each statement in the program is executed. It is equally important to understand the total execution time. To estimate these numbers for the above language, we can use the following simple rules:
    1. Write a set of syntax--directed tranlation rules (using YACC--like notation or the notation in the text) that annotates each non--terminal node with the estimated cost (in terms of execution units) using the rules above. Be clear about what assumptions you are making with respect to how you compute these values and what attributes are associated with the different symbols.

      First, associate attribute Count with STMT, STMT_LIST 
      Also, assume constant returns the value of the constant.
      
      PROGRAM      --> procedure STMT_LIST  {print $2.Count}   
      STMT_LIST    --> STMT STMT_LIST        
                                   {$$.Count = $1.Count + $2.COUNT}
                    |    STMT      {$$.Count = $1.Count}
      STMT         --> ASSIGNMENT  {$$.Count = 1}
                    |     do var = constant to constant begin STMT_LIST end   
      			     {$$.COUNT = ($6-$4+1)*$8.Count}
                    |     if var then  STMT_LIST  else  STMT_LIST endif
      			     {$$.COUNT = 0.5*$4.Count = 0.5*$6.Count} 
      
    2. Draw and annotate (with your attributes) this parse tree using your rules from part (a):
      procedure
        do i = 1 to 100 begin
          if x then
            do j = 1 to 50 begin
              ASSIGNMENT
            end
          else
            ASSIGNMENT
          endif
          ASSIGNMENT
        end
      
      c = Count
      
                             PROGRAM
                            /      \
                     procedure     STMT_LIST c = 2660
                                       | 
                                     STMT c = 2660
                                      |
                       ----------------------------------
                      |  | | |  |  |   |     |  c = 26.6 |                 
                      do i = 1 to 100 begin STMT_LIST  end
                                           /       \ 
                                         STMT      STMT_LIST c = 1
                                       ___|_c = 25.5    \
                                      /   | \            \
                   if x then STMT_LIST else STMT_LIST     STMT c = 1 
                               |c = 50            \c =1      \
                             STMT                  \          \
                               | c = 50           STMT       ASSIGNMENT
              ----------------------------------    \ c =1             
              |  | | |  |  |   |     |          |   ASSIGNMENT 
              do j = 1 to 50 begin STMT_LIST end       
                                     | c = 1      
                                     |
                                   STMT c =1
                                     | 
                                  ASSIGNMENT