CS464 MidtermRedux

Version 1.0

Steve Bazinet 11/19/05

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

0 Steve Bazinet 11/19/05 Problem # 1 – Ambiguity What is an ambiguous grammar? From page 132. 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> | c <A> | <id> | a <A> + + <A> | <id> | b The sentential form yields to distinct parse trees.3. hence. ambiguity.CS464 MidtermRedux Version 1. 3. Problem # 2 – Unambiguous grammar for if-then-else Show an unambiguous grammar of an if-then-else statement. From page 138 <stmt> < matched > < unmatched > → → → <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 .7 Ambiguity A grammar that generates a sentential form for which there are two or more distinct parse trees is said to be ambiguous.1.

0 Steve Bazinet 11/19/05 Problem # 3 – Convert EBNF to BNF From page 172 problem # 17 Convert the following EBNF to BNF: S → A { bA } { } repeat – 0 or indefinitely A → a [b] A BNF S → A | AC C → bA | bAC A → aA | abA or if you like D S → A | AD D → bA | bAD A → aA | abA The point is you can use any non-terminal except S and A to create the additional rule. But. (middle of the page). see page 153.CS464 MidtermRedux Version 1. x = 2 * y + x – 1 { x > 11 } Then simplify algebraically . { 2 * y + x – 1 > 11 } { 2 * y + x > 12 } { 2 * y > 12 – x } { 2 * y > 12 – x } x = 2 * y + x – 1 Answer ! { x > 11 } For an example. its equivalent value. 3 of 9 . [ ] optional – 0 or once Problem # 4 – Precondition From page 172 problem # 19d Compute the weakest precondition for each of the following assignment statements and postconditions: Substitute for x. It starts with “Note that the appearance of the left side …” and x=x+y–3{x> 10 }. you must create an additional rule.

0 Steve Bazinet 11/19/05 Problem # 5 – Derivations And Parse Tree I couldn’t find in book! E→ E+T | E–T | T→ T*F | T/F | T F F → number | name | ( E ) 2+(3*7+6) 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 * ) ( 6 E | T | T | F | 3 + 3 7 * F | 7 4 of 9 .CS464 MidtermRedux Version 1.

(bottom of the page). see page 188.CS464 MidtermRedux Version 1. It starts with “Following is a trace of the parse of a + b…” The sentence: a*(b+c) Call lex Enter <expr> Enter <term> Enter <factor> Call lex Exit <factor> Call lex Enter <factor> Call lex Enter <expr> Enter <term> Enter <factor> Call lex Exit <factor> Exit <term> Call lex Enter <term> Enter <factor> Call Exit Exit Exit Call Exit Exit Exit lex <factor> <term> <expr> lex <factor> <term> <expr> [ returns a ] [ returns * [ returns ( [ returns b ] ] ] [ returns + ] [ returns c ] [ returns ) ] [ returns end ] The sentence's syntax is good! 5 of 9 .1 for the string a * ( b + c ) For an example.0 Steve Bazinet 11/19/05 Problem # 6 – Parser Trace From page 203 problem # 4 Show the trace of the recursive descent parser given in Section 4.4.

And The Handle From page 203 problem # 6c Given the following grammar and the right sentential form. draw a parse tree and show the phrases and simple phrases.CS464 MidtermRedux Version 1. S→ AbB | bAc A→ B→ S bAc bAbc baBBbc baBcBbbc S b A a B c A b B B b a c b A B c Ab Ac | aBB | cBb | c S A b A a B c A b B B S A b B B b a c b A B c b b a B c B S A b B B b c b b c c A B S 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 phrase simple phrase handle This is my BEST guess based on Figure 4.3 on page 194 and the 1st four paragraphs on page 195. as well as the handle. Please let me know if you agree or disagree. NOTE that I’m concluding a handle is a simple phrase is a phrase. a B c B b 6 of 9 . Simple Phrases.0 Steve Bazinet 11/19/05 Problem # 7 – Phrases.

This may not be a big problem with smaller utility programs but if one is building a significantly large application then one can imagine many many global variables. finally. more reliable. First. And. This sounds like a programmer’s nightmare.CS464 MidtermRedux Version 1. that variable will have to examined to determine whether it is cross-talking to other subprograms. All procedures can end up being nested at the same level . Finally. 7 of 9 . With many subprograms touching them in very complex ways. perhaps causing unforeseen side-effects. For example. One disadvantage with dynamic scoping is that from the beginning of execution of a subprogram until its end. and execute faster than equivalent programs in dynamic-scoped languages. This advantage is that in many cases a caller does not have to pass parameters to its callees.8. Then the original program’s integrity needs to reassessed and the modification will also need test. the variable may be move globally or at least one nested level up. because they are implicitly visible in the called program. see 5. Problem # 9 – Dynamic Scoping From page 243 problem # 22 What are the advantages and disadvantages of dynamic scoping? For further explanation.8. This makes programs less reliable.0 Steve Bazinet 11/19/05 Problem # 8 – Static Scoping From page 243 problem # 18 What is the general problem with static scoping? For further explanation. in the main program. Dynamic scoping also makes programs much more difficult to read. It is not difficult to understand why dynamic scoping is not as widely used as static scoping. using globals instead of deeper levels of nesting. the final design may be awkward and contrived. Moreover. They can simply be local variables in the caller. and it may not reflect the underlying conceptual design. it a subprogram due to some modification now needs to see a variable local to another subprogram on the same level then a lengthy change may be need.3 Evaluation of Static Scoping on page 232 The most general problem with static scoping is that it encourages designer to use far more global variables than are necessary. None of these need to be passed in a dynamically scoped language. see 5. accesses to nonlocal variables in dynamic-scoped languages take far longer than accesses to nonlocals when static scoping is used. because one must know possibly many calling sequences. all of its local variables are visible to all directly or indirectly called subprograms.5 Evaluation of Dynamic Scoping on page 236 The text states many disadvantages and only one advantage. This task can be virtually impossible for a human reader. Programs in static-scoped languages are easier to read.

4 Implicit Heap-Dynamic Variables on page 222 As the question implies. Instead. The relationship the question asks about is that in general the act of binding occurs when an assignment is made. nor can it be determined by the spelling of its name. the type of a variable is not specified by a declaration statement. the variable being assigned is bound to the type of the value of the expression on the right side of the assignment. see 5. allowing highly generic code to be written. in the second all attributes are bound but the point is that this occurs during an assignment. a similar concept occurs with implicit heap-dynamic variables.CS464 MidtermRedux Version 1.4. In a sense.2.2 Dynamic Type Binding on page 214 In dynamic type binding. The advantage of such variables is that they have the highest degree of flexibility. For further explanation. Explain this relationship. see 5.3. These variables are bound to heap storage only when they are assigned a values. In fact. For further explanation. they are just names that adapt to whatever use they are asked to serve. When the assignment statement is executed.0 Steve Bazinet 11/19/05 Problem # 10 – Dynamic Type Binding Vs Implicit Heap-Dynamic Variables From page 244 problem # 5 Dynamic type binding is closely related to implicit heap-dynamic variables.4. the variable is bound to a type when it is assigned a value in an assignment statement. In the first case only the type is bound. 8 of 9 . all their attributes are bound every time they are assigned.

CS464 MidtermRedux Version 1. Thus the scope can be determined only at run time. end. a variable’s declaration is sought in the subprogram in which it is referenced.8. 9 of 9 . with static scoping X has a value of 5. begin X := 5.of Sub1 -. Page 235 – 5. first. therefore.of Main -. What value of X is printed in procedure Sub1? Under dynamic scoping rules. its declaration is sought in the subprogram that declares Sub1. its declaration is sought in the subprogram that called it. procedure Sub1 is begin Put(X). X := 5. what value of X is printed in procedure Sub1? procedure Main is X : Integer. the declaration of X is not found in subprogram. Therefore.of Sub1 -.of Sub2 -. Static scoping is so named because the scope of a variable can be statically determined. begin X := 10. In this case that subprogram is Main and indeed X’s declaration is found there. procedure Sub2 is X : Integer. when Main started executing. 10. Sub1. Sub1. CAUTION! If you do not give some explanation of how static and dynamic scope works you will not get full credit. prior to execution. In this case the declaration of X is not found in subprogram. that is.0 Steve Bazinet 11/19/05 Problem # 11 – Scoping From page 245 problem # 9 Assume the following Ada program was compiled and executed using static scoping rules. first. Sub2 called Sub1. Sub1 end. Page 228 – 5.of Main With static scoping.8. Dynamic scoping is Therefore.1 Static Scope -. therefore. not on their spatial relationship to each other. a variable’s declaration is sought in the subprogram in which it is referenced. with dynamic scoping X has a value of 10. based on the calling sequence of subprograms.of Sub2 -. Sub2 end. As before. Sub2 declared X and assigned it the value. The value of Main’s X was assigned in the statement.4 Dynamic Scope With dynamic scoping.