Professional Documents
Culture Documents
Midtermreduxversion 01
Midtermreduxversion 01
Midtermreduxversion 01
Version 1.0
Table of Contents
Table of Contents.........................................................................................................................1 Problem # 1 Ambiguity..............................................................................................................2 Problem # 2 Unambiguous grammar for if-then-else................................................................2 Problem # 3 Convert EBNF to BNF...........................................................................................3 Problem # 4 Precondition..........................................................................................................3 Problem # 5 Derivations And Parse Tree..................................................................................4 Problem # 6 Parser Trace.........................................................................................................5 Problem # 7 Phrases, Simple Phrases, And The Handle.........................................................6 Problem # 8 Static Scoping.......................................................................................................7 Problem # 9 Dynamic Scoping..................................................................................................7 Problem # 10 Dynamic Type Binding Vs Implicit Heap-Dynamic Variables.............................8 Problem # 11 Scoping...............................................................................................................9
1 of 9
CS464 MidtermRedux
Version 1.0
Problem # 1 Ambiguity
What is an ambiguous grammar? From page 132, 3.3.1.7 Ambiguity A grammar that generates a sentential form for which there are two or more distinct parse trees is said to be ambiguous. From page 171 problem # 8 Prove that the following grammar is ambiguous: <S> <A> <A> <A> + <A> | <id> <id> a | b | c Create the parse trees for a + b + c:
<S> | <A> <A> | <id> | a <S> | <A> <A> | <id> | c
+ <A> | <id> | b
<A> +
+ <A> | <id> | b
<matched> | <unmatched> if <logic_expr> then <matched> else <matched> | any non-if statement if <logic_expr> then <stmt> else <matched> | if <logic_expr> then <matched> else <unmatched>
2 of 9
CS464 MidtermRedux
Version 1.0
Problem # 4 Precondition
From page 172 problem # 19d Compute the weakest precondition for each of the following assignment statements and postconditions: Substitute for x; its equivalent value. x = 2 * y + x 1 { x > 11 } Then simplify algebraically .
{ x > 11 }
For an example, see page 153, (middle of the page). It starts with Note that the appearance of the left side and x=x+y3{x> 10 }.
3 of 9
CS464 MidtermRedux
Version 1.0
Leftmost derivation E E+T T+T F+T 2+T 2+F 2+(E) 2+(E+T) 2+(T+T) 2+(T*F+T) 2+(F*F+T) 2+(3*F+T) 2+(3*7+T) 2+(3*7+F) 2+(3*7+6)
Rightmost derivation E E+T E+F E+(E) E+(E+T) E+(E+F) E+(E+6) E+(T*F+6) E+(T*F+6) E+(T*7+6) E+(F*7+6) E+(3*7+6) T+(3*7+6) F+(3*7+6) 2+(3*7+6)
Parse tree
E T | F | E | )
Derivation tree
E | T | F | 2
+ ( 2 + T | F | 6 *
E | T | T | F | 3
F | 7
4 of 9
CS464 MidtermRedux
Version 1.0
] ] ]
[ returns +
[ returns c
[ returns )
[ returns end ]
5 of 9
CS464 MidtermRedux
Version 1.0
b a B c B b b c S b A a B c A b B B c B b b c
a B c B b b
This is my BEST guess based on Figure 4.3 on page 194 and the 1st four paragraphs on page 195. Please let me know if you agree or disagree. NOTE that Im concluding a handle is a simple phrase is a phrase.
a B c B b
6 of 9
CS464 MidtermRedux
Version 1.0
7 of 9
CS464 MidtermRedux
Version 1.0
8 of 9
CS464 MidtermRedux
Version 1.0
Problem # 11 Scoping
From page 245 problem # 9 Assume the following Ada program was compiled and executed using static scoping rules. What value of X is printed in procedure Sub1? Under dynamic scoping rules, what value of X is printed in procedure Sub1?
procedure Main is X : Integer; procedure Sub1 is begin Put(X); end; procedure Sub2 is X : Integer; begin X := 10; Sub1 end; begin X := 5; Sub2 end; CAUTION! If you do not give some explanation of how static and dynamic scope works you will not get full credit. Static scoping is so named because the scope of a variable can be statically determined; that is, prior to execution. Page 228 5.8.1 Static Scope
With static scoping, first, a variables declaration is sought in the subprogram in which it is referenced. In this case the declaration of X is not found in subprogram, Sub1; therefore, its declaration is sought in the subprogram that declares Sub1. In this case that subprogram is Main and indeed Xs declaration is found there. The value of Mains X was assigned in the statement, X := 5; when Main started executing. Dynamic scoping is Therefore, with static scoping X has a value of 5.
based on the calling sequence of subprograms, not on their spatial relationship to each other. Thus the scope can be determined only at run time. Page 235 5.8.4 Dynamic Scope
With dynamic scoping, first, a variables declaration is sought in the subprogram in which it is referenced. As before, the declaration of X is not found in subprogram, Sub1; therefore, its declaration is sought in the subprogram that called it. Sub2 called Sub1. Sub2 declared X and assigned it the value, 10. Therefore, with dynamic scoping X has a value of 10.
9 of 9