1 | # Rick van der Zwet
|
---|
2 | # StudentID: 0433373
|
---|
3 | # $Id: lecture3.txt 248 2007-10-04 19:37:40Z rick $
|
---|
4 |
|
---|
5 | All quotes are based on the 7th edition of Concepts of programming
|
---|
6 | languages from Robert W. Sebesta, cause the 8th edition has not come in
|
---|
7 | yet
|
---|
8 |
|
---|
9 | *** Chapter 3 Problem Set ***
|
---|
10 |
|
---|
11 | 24)
|
---|
12 | Q: Write an attribute grammar whose base BNF is that of Example 3.2 and
|
---|
13 | whose type rules are the same as for the assignment statement examples
|
---|
14 | of Section 3.4.5.
|
---|
15 |
|
---|
16 | A:
|
---|
17 | Syntax Rule: <assign> -> <id> = <expr>
|
---|
18 | Semantic Rule: <expr>.expected_type <- <id>.actual_type
|
---|
19 |
|
---|
20 | Syntax Rule: <id> -> A | B | C
|
---|
21 | Semantic Rule: <id>.actual_type <- look-up(<id>.string)
|
---|
22 |
|
---|
23 | Syntax Rule: <expr> -> <id>[2] + <expr>[3]
|
---|
24 | Semantic Rule: <expr>.actual_type <-
|
---|
25 | if (<id>[2].actual_type = int) and
|
---|
26 | (<expr[3].actual_type = int)
|
---|
27 | than int
|
---|
28 | else real
|
---|
29 | end if
|
---|
30 | <expr>[3].expected_type <-
|
---|
31 | if (<id>[2].actual_type = int) and
|
---|
32 | (<expr>.expected_type = int)
|
---|
33 | than int
|
---|
34 | else real
|
---|
35 | end if
|
---|
36 | Predicate: <expr>.actual_type == <expr>.expected_type
|
---|
37 |
|
---|
38 | Syntax Rule: <expr> -> <id>[2] * <expr>[3]
|
---|
39 | Semantic Rule: <expr>.actual_type <-
|
---|
40 | if (<id>[2].actual_type = int) and
|
---|
41 | (<expr[3].actual_type = int)
|
---|
42 | than int
|
---|
43 | else real
|
---|
44 | end if
|
---|
45 | <expr>[3].expected_type <-
|
---|
46 | if (<id>[2].actual_type = int) and
|
---|
47 | (<expr>.expected_type = int
|
---|
48 | than int
|
---|
49 | else real
|
---|
50 | end if
|
---|
51 | Predicate: <expr>.actual_type == <expr>.expected_type
|
---|
52 |
|
---|
53 | Syntax Rule: <expr> -> ( <expr>[2] )
|
---|
54 | Semantic Rule: <expr>.actual_type <- <expr>[2].actual_type
|
---|
55 | <expr>[2].expected_type <- <expr>.expected_type
|
---|
56 | Predicate: <expr>.actual_type == <expr>.expected_type
|
---|
57 |
|
---|
58 | Syntax Rule: <expr> -> <id>
|
---|
59 | Semantic Rule: <expr>.actual_type <- <id>.actual_type
|
---|
60 | Predicate: <expr>.actual_type == <expr>.expected_type
|
---|
61 |
|
---|
62 |
|
---|
63 |
|
---|
64 | 25)
|
---|
65 | Q: Prove the following program is correct:
|
---|
66 | {n > 0}
|
---|
67 | count = n;
|
---|
68 | sum = 0;
|
---|
69 | while count <> 0 do
|
---|
70 | sum = sum + count;
|
---|
71 | count = count - 1;
|
---|
72 | end
|
---|
73 | {sum = 1 + 2 + ... + n}
|
---|
74 | A: (with some help from http://www.cs.unb.ca/profs/wdu/cs4613/a5ans.htm
|
---|
75 | cause I was looking at the wrong I)
|
---|
76 | * First step is linear,
|
---|
77 | {n > 0} count = n {Q}
|
---|
78 | Q: {n > 0 AND count = n}
|
---|
79 | * Second as well
|
---|
80 | {n > 0 AND count = n} sum = 0 {Q}
|
---|
81 | Q: {n > 0 AND count = n AND sum = 0}
|
---|
82 |
|
---|
83 | * Loop invariant vality checks:
|
---|
84 | A) P => I
|
---|
85 | B) {I and B} S {I}
|
---|
86 | C) {I and not(B)} => Q
|
---|
87 | D) Check is loops terminates
|
---|
88 | * count is liniary decreased with 1 and starts with a number than 0.
|
---|
89 | logically it will always becomes 0, which ends the loop
|
---|
90 |
|
---|
91 |
|
---|
92 |
|
---|
93 |
|
---|
94 |
|
---|
95 |
|
---|
96 |
|
---|
97 |
|
---|
98 |
|
---|
99 |
|
---|
100 |
|
---|
101 |
|
---|
102 |
|
---|