CSci532 Programming Languages and Paradigms: Homework 2 Solutions

Due date: Friday, October 03, 2008 in class
Each homework may have a different weight, which depends on its level of difficulty.
Absolutely no copying others' work


  1. In old versions of Fortran that did not have the character data type, character strings were expressed in the following format:
         <string literal> ::= <numeral> H <string>
    where the <numeral> is a base-ten integer (≥ 1), H is a keyword, and <string> is a sequence of characters. The semantics of this string literal is correct if the numeric value of the base-ten numeral matches the length of the string. Write an attribute grammar using only synthesized attributes for the nonterminals in the definition of <string literal>.     (25%)
    Ans>

  2. Repeat Question 1, using a synthesized attribute for <numeral> and an inherited attribute for <string>.     (25%)
    Ans>

  3. Construct an attribute grammar that translates arithmetic expressions from infix notation into prefix notation in which an operator appears before its operands; e.g., –xy is the prefix notation for x–y. Give annotated parse trees for the inputs 9–5+2 and 9–5*2.     (50%)
    Ans>