Midtermreduxversion 01

You might also like

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 9

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

CS464 MidtermRedux

Version 1.0

Steve Bazinet 11/19/05

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> | c <A> | <id> | a

<A> +

+ <A> | <id> | b

The sentential form yields to distinct parse trees; hence, ambiguity.

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

CS464 MidtermRedux

Version 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, you must create an additional rule. [ ] 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; its equivalent value. 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, 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

Steve Bazinet 11/19/05

Problem # 5 Derivations And Parse Tree


I couldnt find in book! E E+T | ET | 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 *

E | T | T | F | 3

F | 7

4 of 9

CS464 MidtermRedux

Version 1.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.1 for the string a * ( b + c ) For an example, see page 188, (bottom of the page). 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

CS464 MidtermRedux

Version 1.0

Steve Bazinet 11/19/05

Problem # 7 Phrases, Simple Phrases, 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, as well as the handle. 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. 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

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, see 5.8.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. All procedures can end up being nested at the same level , in the main program, using globals instead of deeper levels of nesting. Moreover, the final design may be awkward and contrived, and it may not reflect the underlying conceptual design. For example, 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. First, the variable may be move globally or at least one nested level up. Then the original programs integrity needs to reassessed and the modification will also need test. And, finally, that variable will have to examined to determine whether it is cross-talking to other subprograms; perhaps causing unforeseen side-effects. 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. With many subprograms touching them in very complex ways. This sounds like a programmers nightmare.

Problem # 9 Dynamic Scoping


From page 243 problem # 22 What are the advantages and disadvantages of dynamic scoping? For further explanation, see 5.8.5 Evaluation of Dynamic Scoping on page 236 The text states many disadvantages and only one advantage. This advantage is that in many cases a caller does not have to pass parameters to its callees. They can simply be local variables in the caller. None of these need to be passed in a dynamically scoped language, because they are implicitly visible in the called program. One disadvantage with dynamic scoping is that from the beginning of execution of a subprogram until its end, all of its local variables are visible to all directly or indirectly called subprograms. This makes programs less reliable. Dynamic scoping also makes programs much more difficult to read, because one must know possibly many calling sequences. This task can be virtually impossible for a human reader. Finally, accesses to nonlocal variables in dynamic-scoped languages take far longer than accesses to nonlocals when static scoping is used. It is not difficult to understand why dynamic scoping is not as widely used as static scoping. Programs in static-scoped languages are easier to read, more reliable, and execute faster than equivalent programs in dynamic-scoped languages.

7 of 9

CS464 MidtermRedux

Version 1.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. Explain this relationship. For further explanation, see 5.4.2.2 Dynamic Type Binding on page 214 In dynamic type binding, the type of a variable is not specified by a declaration statement, nor can it be determined by the spelling of its name. Instead, the variable is bound to a type when it is assigned a value in an assignment statement. When the assignment statement is executed, the variable being assigned is bound to the type of the value of the expression on the right side of the assignment. For further explanation, see 5.4.3.4 Implicit Heap-Dynamic Variables on page 222 As the question implies, a similar concept occurs with implicit heap-dynamic variables. These variables are bound to heap storage only when they are assigned a values. In fact, all their attributes are bound every time they are assigned. In a sense, they are just names that adapt to whatever use they are asked to serve. The advantage of such variables is that they have the highest degree of flexibility, allowing highly generic code to be written. The relationship the question asks about is that in general the act of binding occurs when an assignment is made. In the first case only the type is bound; in the second all attributes are bound but the point is that this occurs during an assignment.

8 of 9

CS464 MidtermRedux

Version 1.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. 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

-- of Sub1 -- of Sub1 -- of Sub2 -- of Sub2 -- of Main -- of Main

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

You might also like