source: liacs/cvp/assignments/lecture3.txt@ 144

Last change on this file since 144 was 2, checked in by Rick van der Zwet, 15 years ago

Initial import of data of old repository ('data') worth keeping (e.g. tracking
means of URL access statistics)

File size: 2.8 KB
Line 
1# Rick van der Zwet
2# StudentID: 0433373
3# $Id: lecture3.txt 248 2007-10-04 19:37:40Z rick $
4
5All quotes are based on the 7th edition of Concepts of programming
6languages from Robert W. Sebesta, cause the 8th edition has not come in
7yet
8
9*** Chapter 3 Problem Set ***
10
1124)
12Q: 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
16A:
17Syntax Rule: <assign> -> <id> = <expr>
18Semantic Rule: <expr>.expected_type <- <id>.actual_type
19
20Syntax Rule: <id> -> A | B | C
21Semantic Rule: <id>.actual_type <- look-up(<id>.string)
22
23Syntax Rule: <expr> -> <id>[2] + <expr>[3]
24Semantic 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
36Predicate: <expr>.actual_type == <expr>.expected_type
37
38Syntax Rule: <expr> -> <id>[2] * <expr>[3]
39Semantic 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
51Predicate: <expr>.actual_type == <expr>.expected_type
52
53Syntax Rule: <expr> -> ( <expr>[2] )
54Semantic Rule: <expr>.actual_type <- <expr>[2].actual_type
55 <expr>[2].expected_type <- <expr>.expected_type
56Predicate: <expr>.actual_type == <expr>.expected_type
57
58Syntax Rule: <expr> -> <id>
59Semantic Rule: <expr>.actual_type <- <id>.actual_type
60Predicate: <expr>.actual_type == <expr>.expected_type
61
62
63
6425)
65Q: 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}
74A: (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:
84A) P => I
85B) {I and B} S {I}
86C) {I and not(B)} => Q
87D) 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
Note: See TracBrowser for help on using the repository browser.