Questions

  1. Using the syntax directed definition for does expression evaluation (see slides), draw and annotate the parse tree for the input: (4*7+1)*2
  2. Draw and annotate a tree for input ((a)+(b)) assuming the rules below.
    Production Semantic Rule
    E --> E + T $$ = mknode("-",$1,$3)
    E --> E - T $$ = mknode("-",$1,$3)
    E --> T $$=$1;
    T --> ( E ) $$ = $2;
    T --> id $$ = mkleaf(id,$1)
    T --> num $$ = mkleaf(num,$1)
    All of the attributes are pointer to a given node type. Assume the lexer returns a string for both num and id. mkleaf creates a leaf node with a type (num or id) and an associated string (from the lexer). mknode(op,left,right) creates an operator node with the label op and two pointers: left and right
  3. Let synthesized attribute val give the value of the binary number generated by S in the following grammar. For example, on input 101.101, S.val = 5.625
    S --> L . L
    S --> L
    L --> L B
    L --> B
    B --> 0
    B --> 1
    Use synthesized attributes to determine S.val
  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.
    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
      
    Solutions