CSCI 4627
Spring 2005
Practice questions for exam 2

  1. When a (top-down) LL(1) parser needs to decide whether to use a production, how much of the sequence of tokens generated by the right-hand side of the production can the parser see? When a (bottom-up) SLR(1) parser needs to make the same decision, how much does it see?

  2. What is the advantage of a LALR(1) parser over an SLR(1) parser?

  3. Show the DFA of LR(0) items for the following grammar. The terminals are s and c.

        A -> A s B
        A -> B
        B -> c
      

  4. What is the difference between a synthesized attribute and an inherited attribute? Define each of those terms.

  5. Sometimes it is desirable to use only synthesized attributes in your parser. Is it always possible to do that, even if your semantics requires inherited attributes? If so, how can you deal with complex semantics while only using synthesized attributes in the parser. If not, what prevents you from using only synthesized attributes?

  6. Exercise 6.6 of the text contains a grammar that describes binary trees where each node of the tree has an integer label. It discusses an ordering requirement that is the same one required by binary search trees. Solve that exercise.

  7. You are given the following (ambiguous) grammar for expressions.

          expr -> expr + expr
          expr -> expr * expr
          expr -> NUM
          expr -> VAR
      
    where NUM and VAR are tokens. The lexer provides an attribute NUM.val that is the (integer) value of a NUM token. It also provides an attribute VAR.name that is the name of a variable. You would like to translate these expressions into instructions for a stack machine. The stack machine has the following instructions.
    PUSH_INT k Push integer k onto the stack
    PUSH_VAR k Push the value of the variable at offset k onto the stack
    ADD Pop the top two numbers from the stack and push their sum
    MULT Pop the top two numbers from the stack and push their product
    The PUSH_INT instruction can handle any integer that the lexer will produces as an attribute of a NUM token. You have access to three support functions: get_var_offset(v) returns the offset where the variable named v is stored; gen1(I) generates single-part instruction I, and gen2(I,k) generates two-part instruction I, with parameter k.

    Write semantic actions to be performed at each production that will generate code to compute a given expression and leave its value on the top of the stack. Do not worry that the grammar is ambiguous. That is a parsing problem, not a semantic one.

  8. This is the same as the preceding exercise, but instead of performing actions to generate the code, you would like to create the code sequence as an attribute of an expression nonterminal. Suppose that, in addition to get_var_offset(v), the following functions are available. single(I) produces, as its value, a sequence that represents the single-part instruction I. doub(I,k) produces a code sequence for a two-part instruction. Operator + can be used to compute the concatenation of two code sequences.