Computer Science 4627
Written Assignment 6

  1. Exercise 6.4, page 339. Assume that the number token has an attribute that is the vale of the expression, and that numbers are integers. Define a val attribute for each of the nonterminals.

    Notice that an exp' is only part of an expression, and it needs more information before it can know its value. Give it an inherited attribute, lval, that is the value of the expression to the left of it. Do a similar thing for term'.

    Write each production separately, with its attribute equations. Do not use bars.

  2. Exercise 6.20, page 341. Keep C syntax, except that integer division becomes div and conversion from integer to real must be explcit, as function FLOAT. Do not allow implicit conversion from real numbers to integers. Keep in mind that, for this problem, you are not evaluating the expression, but only translating it to a different form.

    For example, if the input is 13/4, then the translation is 3 div 4. If the input is 1.5*2, then the translation is 1.5*FLOAT(2).

    Just write attribute equations. Do not change the grammar. Leave it in its present form, even though it is ambiguous. You will need to know both the type (integer or real) and the translation (a string) of each expression. Assume for this problem that the attribute of num is the lexeme of the number, a string of one or more decimal digits. You can use operator + on strings as concatenation of strings.

    Please answer the following question as well. For each attribute say whether it is synthesized or inherited.

  3. Exercise 6.22, page 342. Give short and clear explanations. For part (a), assume that the A is an identifier token, regardless of whether it is a type definition or a variable. For part (b), you have additional flexibility since you are modifying the lexical analyzer.