COMS W4115
Programming Languages and Translators
Homework Assignment #2
Submit solutions electronically on
     by 2:40pm, March 6, 2013



  1. Interactive desk calculator for boolean nor-expressions.
    1. Construct a grammar that generates boolean nor-expressions containing the logical constants true and false, the left-associative binary boolean operator nor [where p nor q means not (p or q)], and parentheses.
    2. Show the parse tree according to your grammar for the nor-expression
    3.    true nor true nor (false nor false)
    4. Implement an interpreter that takes as input newline-terminated lines of boolean nor-expressions and produces as output the truth value of each expression. You can use lex and yacc or their equivalents to implement your interpreter. Show the source code for your interpreter and the sequences of commands you used to test it.
    5. Run your interpreter on the following two inputs and show the outputs:
    6.    (a) (true nor false) nor (true nor false)
         (b) true nor true nor (false nor false)

  2. Infix to stack machine code translator
    1. Consider the Yacc specification in Fig. 4.59 of ALSU (p. 292). Modify this translator to produce stack machine code for each input line. For example, for the input "1*(2+3)" your translator should produce the instructions
    2. push 1
      push 2
      push 3
    3. Implement your translator in Yacc (or its equivalent) and show the stack machine code generated for each of the following inputs:
    4.     (i) 1+2*3
         (ii) 1+(2-3)
        (iii) 1+2-+3
        (iv) 1+2--3

  3. Let L be the language generated by the following grammar:
    S → a S b S | b S a S | ε
    1. What language does this grammar generate?
    2. Show that this grammar is ambiguous.
    3. Construct the predictive parsing table for this grammar.
    4. Construct an LL(1) grammar for L.
    5. Construct the predictive parsing table for your grammar.

  5. Let L be the language of pure lambda calculus expressions generated by the following grammar:
    E → ^ v . E  | E E | ( E ) | v
    The symbols ^, ., (, ) and v are tokens. ^ represents lambda and v represents a variable.
    An expression of the form ^v.E is a function definition where v is the formal parameter of the function and E is its body.
    If f and g are lambda expressions, then the lambda expression fg represents the application of the function f to the argument g.
    1. Show that this grammar is ambiguous.
    2. Construct an unambiguous grammar for L assuming that function application is left associative, e.g., fgh = (fg)h, and that function application binds tighter than ., e.g., (^x. ^y. xy) ^z.z = (^x. (^y. xy)) ^z.z.
    3. Using your grammar, construct a parse tree for the expression (^x. ^y. xy) ^z.z.
    4. Using the command yacc -v on a file containing your grammar, show the LALR(1) parsing action and goto table for your grammar.