What is the difference between a synthesized attribute and an inherited attribute? Define each of those terms.
When you see a definition of an attribute, how can you know whether the attribute is synthesized or inherited just by looking at the definition and the production?
You are given the following (ambiguous) grammar for expressions.
expr -> expr + expr expr -> expr * expr expr -> NUM expr -> VARwhere 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 (a string). 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 |
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. Write the actions to generate the code as the parsing is done. That is, write a narrow translator.
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, so that you end up with a wider translator. 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.
Exercise 6.6.1(a) (p. 408 of the text). That is, give translation rules using the BE.false and BE.true attributes for translating repeat S while B, which means the same thing as C++ or Java statement do S while (B).
Suppose that your parser works by building abstract syntax trees for expressions and statements, generating code for them later. How can you handle exercise 6.6.1(b) without writing any extra rules for generating code?
Using a and b as type variables, what is the most general type of function f defined as follows in sfl?
def f x = [x,x] end
Using a and b as type variables, what is the most general type of function f defined as follows in sfl?
def f x y = [x, y+1] end