Homework1Solution

advertisement
CSCI 355 Spring 2008
Homework 1 Solution
Chapter 3
Each problem is worth 10 points. Total 100 points.
2c. <switch_stmt>  switch ( <expr> ) {case <literal> : <stmt_list>
{case <literal> : <stmt_list> } [default : <stmt_list>] }
8. The following two distinct parse trees for the same string prove that the grammar is
ambiguous.
<S>
<S>
<A>
<A>
<A>
+
+
<A>
<A>
a
<id>
|
a
<A>
b<id> c<id>
|
|
b
c
<A> +
<A> +
13. S  a S b | a b
16. <assign>  <id> = <expr>
<id>  A | B | C
<expr>  <expr> (+ | -) <expr>
| <id>
<A><A>
a <id> b <id> c
|
|
a
b
11. a, d
| (<expr>)
<A>
<id>
c
|
c
19.
(d)
x = 2 * y + x - 1 {x > 11}
2 * y + x - 1 > 11
2 * y + x > 12
20.
(a)
a=2*b+1
b = a - 3 {b < 0}
a-3<0
a<3
Now, we have:
a = 2 * b + 1 {a < 3}
2*b+1<3
2*b+1<3
2*b<2
b<1
22. The value of an intrinsic attribute is supplied from outside the attribute evaluation
process. The value of a synthesized attribute is computed from the child(ren) node(s) by
an attribute evaluation function.
23.
1. Syntax rule: <assign> - > <var> = <expr>
(no Semantic rule here, since the problem does not define the type match
between <var> and <expr> )
2. Syntax rule: <expr> - > <var> [2] + <var> [3]
Semantic rule: <expr>.actual-type <- <var>[2].actual-type
Predicate: <var>[2].actual-type == <var>[3].actual-type
3. Syntax rule: <expr> - > <var>
Semantic rule: <expr>.actual-type <- <var>.actual-type
4. Syntax rule: <var> A | B | C
Semantic rule: <var>.actual-type <- look-up(<var>.string)
Download