assign 4

2
- 1 - 1. (3 points) Using the virtual machine instructions as described in your textbook on pages 149 and 150, give an operational semantic meaning for: a) The C/C++/Java while loop. b) The C/C++/Java do-while loop. c) Are the virtual machine instructions for parts 1 a) and 1 b) the same? Explain. 2. (3 points) Suppose you wished to change the condition so that the loops in Question #1 above would end when the condition is true (rather than false). In other words, the condition would have to be changed to the opposite condition. Using the virtual machine instructions as described in your textbook, give an operational semantic meaning for: a) A C/C++/Java while loop modified to take the opposite condition. b) A C/C++/Java do-while loop modified to take the opposite condition. c) Are the virtual machine instructions for parts 2 a) and 2 b) the same? Explain. 3. (2 points) Give the operational semantic meaning for the C/C++/Java if- statement. 4. (5 points) Given the post conditions and the statements below, find the weakest preconditions for each sequence of statements below and for every assignment statement within each sequence. a) x = z - 3; { x < 0 } b) y = 3 * z + 1; x = 2 - y; { x < 1 } c) x = y - 1; x = x + y + 2; { x < 2 } d) if( x == 0 ) x = x + 1; { x >= 0 } e) if( x > y ) x--; else y--; { x == y }

Upload: farrukhsharifzada

Post on 10-Apr-2016

213 views

Category:

Documents


1 download

DESCRIPTION

Assign 4

TRANSCRIPT

- 1 -

1. (3 points) Using the virtual machine instructions as described in your textbook on pages 149 and 150, give an operational semantic meaning for: a) The C/C++/Java while loop. b) The C/C++/Java do-while loop. c) Are the virtual machine instructions for parts 1 a) and 1 b) the same? Explain. 2. (3 points) Suppose you wished to change the condition so that the loops in Question #1 above would end when the condition is true (rather than false). In other words, the condition would have to be changed to the opposite condition. Using the virtual machine instructions as described in your textbook, give an operational semantic meaning for: a) A C/C++/Java while loop modified to take the opposite condition. b) A C/C++/Java do-while loop modified to take the opposite condition. c) Are the virtual machine instructions for parts 2 a) and 2 b) the same? Explain. 3. (2 points) Give the operational semantic meaning for the C/C++/Java if-statement. 4. (5 points) Given the post conditions and the statements below, find the weakest preconditions for each sequence of statements below and for every assignment statement within each sequence. a) x = z - 3; { x < 0 } b) y = 3 * z + 1; x = 2 - y; { x < 1 } c) x = y - 1; x = x + y + 2; { x < 2 } d) if( x == 0 ) x = x + 1; { x >= 0 } e) if( x > y ) x--; else y--; { x == y }

- 2 -

4. (5 points) Show, using the technique for finding loop invariants via the table approach described in class, whether the following program is correct. { n > 0 } index = 0; sum = 0; while (index < n) { sum = sum + A[index]; index = index + 1; } { sum = A[0] + A[1] + ... + A[n-1] } 5. (5 points) Write a denotational semantics mapping function for a C/C++/Java if-statement: Mif(if(B) S1; else S2;, s). 6. (1 point) Question #3 on Page 202 of your textbook. 7. (1 point) In what way are reserved keywords better than non-reserved keywords?