COSC450/550 HW 2 Solved

1. Write EBNF description for the following:
a. A Java class definition header statement
b. A C switch statement
2. Rewrite the BNF of Example 3.4 (page120) to give + precedence over * and force + to be right associative.
3. Using the grammar in Example 3.2 (page117), show a parse tree and a leftmost derivation for the following statement: A = A * (B + (C * A))
4. Prove that the following grammar is ambiguous:
<S à <A <B <C
<A à <A + <A | <id
<id à a | b | c
5. Write an attribute grammar whose BNF basis is that of Example 3.6 (page131) but whose language rules are as follows: Data types cannot be mixed in expressions, but assignment statements need not have the same types on both sides of the assignment operator.
6. Using the virtual machine instructions given in Section 3.5.1.1, give an operational semantic definition of the following:
a. Java do-while
b. C++ if-then-else
c. C for
7. Compute the weakest precondition for each of the following assignment statements and postconditions:
a. a = 2 * (b -1) – 1 {a 0}
b. b = (c + 10) / 3 {b 6}
c. x = 2 * y + x – 1 {x 11}
8. Compute the weakest precondition for each of the following sequences of assignment statements and their postconditions:
a = 2 * b + 1;
b = a – 3
{b < 0}
sellfy