# Rick van der Zwet # StudentID: 0433373 # $Id: lecture3.txt 248 2007-10-04 19:37:40Z rick $ All quotes are based on the 7th edition of Concepts of programming languages from Robert W. Sebesta, cause the 8th edition has not come in yet *** Chapter 3 Problem Set *** 24) Q: Write an attribute grammar whose base BNF is that of Example 3.2 and whose type rules are the same as for the assignment statement examples of Section 3.4.5. A: Syntax Rule: -> = Semantic Rule: .expected_type <- .actual_type Syntax Rule: -> A | B | C Semantic Rule: .actual_type <- look-up(.string) Syntax Rule: -> [2] + [3] Semantic Rule: .actual_type <- if ([2].actual_type = int) and ([3].expected_type <- if ([2].actual_type = int) and (.expected_type = int) than int else real end if Predicate: .actual_type == .expected_type Syntax Rule: -> [2] * [3] Semantic Rule: .actual_type <- if ([2].actual_type = int) and ([3].expected_type <- if ([2].actual_type = int) and (.expected_type = int than int else real end if Predicate: .actual_type == .expected_type Syntax Rule: -> ( [2] ) Semantic Rule: .actual_type <- [2].actual_type [2].expected_type <- .expected_type Predicate: .actual_type == .expected_type Syntax Rule: -> Semantic Rule: .actual_type <- .actual_type Predicate: .actual_type == .expected_type 25) Q: Prove the following program is correct: {n > 0} count = n; sum = 0; while count <> 0 do sum = sum + count; count = count - 1; end {sum = 1 + 2 + ... + n} A: (with some help from http://www.cs.unb.ca/profs/wdu/cs4613/a5ans.htm cause I was looking at the wrong I) * First step is linear, {n > 0} count = n {Q} Q: {n > 0 AND count = n} * Second as well {n > 0 AND count = n} sum = 0 {Q} Q: {n > 0 AND count = n AND sum = 0} * Loop invariant vality checks: A) P => I B) {I and B} S {I} C) {I and not(B)} => Q D) Check is loops terminates * count is liniary decreased with 1 and starts with a number than 0. logically it will always becomes 0, which ends the loop