


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



In grammars, capital letters represent nonterminals, lower case letters represent terminals. One sentence answers are sufficient for the “essay” questions.
(a) What is the primary function of the scanner and what computational mechanism is used to accom- plish it? Break the input stream up into tokens using deter- ministic finite automata (DFA). (b) What is the primary function of the parser and what computational mechanism is used to accom- plish it? Provide syntactic structure to token stream using push-down automata (PDA = DFA + stack). (c) How do you decide what should be handled by the scanner versus the parser? (Hint: think of the com- plexity of languages) Scanner handles simple constructs that can be eas- ily described by regular expressions, parser handles more complicated syntax described by context-free grammars.
(a) Construct a regular expression for recognizing all non-empty strings composed of the letters a and b that do not end in b. (a | b)∗a (b) Convert the regular expression to an NFA using the construction algorithm given in class.
e
e
e e e e
a
b 10 a 11 e
6 8 9
2
3 4
1 (^75)
e
e
(c) Convert the NFA to a DFA (show the sets of NFA states for each DFA state).
S0: 1,3,5,6,7,8, b
b
b
a
a
a
S2: 1,3,4,5,6,8,
S1: 1,2,3,5,6,8,9,10,
(d) Minimize the DFA (show the sets of DFA states for each minimized DFA state).
E → E + E | a
(a) When is a grammar ambiguous? When there are multiple left-most (or right-most) derivations of the same string. (b) Show that the grammar is ambiguous for the string a + a + a
(leftmost derivation 1) E ⇒ E + E ⇒ a + E ⇒ a + E + E ⇒ a + a + E ⇒ a + a + a (leftmost derivation 2) E ⇒ E + E ⇒ E + E + E ⇒ a + E + E ⇒ a + a + E ⇒ a + a + a
(rightmost derivation 1) E ⇒ E + E ⇒ E + a ⇒ E + E + a ⇒ E + a + a ⇒ a + a + a (rightmost derivation 2) E ⇒ E + E ⇒ E + E + E ⇒ E + E + a ⇒ E + a + a ⇒ a + a + a
(c) What happens if you write a recursive-descent parser for this grammar? May infinite loop trying to match E. (d) Fix the grammar to avoid this problem. E → a E′ E′^ → + E E′^ | ≤
S → ABd A → aA | ≤ B → b | cA
(a) Calculate FIRST, FOLLOW for S, A, B: FIRST FOLLOW S { a, b, c } { $ } A { ≤, a } { b, c, d } B { b, c } { d }
(b) Construct its recursive-descent parser (with looka- head), given the following functions:
tok; // current token
match(x) { // matches token if (tok != x) // if wrong token error(); // exit with error tok = getToken(); // get new token }
parser() { tok = getToken(); // initialize S( ); // start symbol match("$"); // match EOF } Answer S() { A(); B(); match("d"); }
A() { if (tok == "a") { match("a"); A(); } else if (tok == "b") || (tok == "c") || (tok == "d")) { ; } else error(); }
B() { if (tok == "b") { match("b"); } else if (tok == "c") { match("c"); A(); } else error(); }
Action Goto State a $ A B 0 shift 1 reduce 2 3 B → ≤ 1 shift 3 reduce 0 A → a 2 accept 3 0 3 shift 1 accept 1
Show the contents of the stack and input buffer for the shift-reduce parse of ”a”, assuming State 0 is the start state:
Stack Input Action $ 0 a $ shift 1 $ 0 a 1 $ reduce A → a $ 0 A $ goto[0,A] = 2 $ 0 A 2 $ accept
P1 S → E P2 E → E + E P3 | a
(a) Derive the canonical sets of LR(1) items
State 0: State 3: [ S → • E , {$ } ] [ E → E + • E , {$,+} ] [ E → • E + E , {$,+} ] [ E → • E + E , {$,+} ] [ E → • a , {$,+} ] [ E → • a , {$,+} ]
State 1: State 4: [ S → E • , {$ } ] [ E → E + E • , {$,+} ] [ E → E • + E , {$,+} ] [ E → E • + E , {$,+} ]
State 2: [ E → a • , {$,+} ]
GOTO(0, E) = 1 GOTO(0, a) = 2 GOTO(1, +) = 3
GOTO(3, a) = 2 GOTO(3, E) = 4 GOTO(4, +) = 3
(b) Build the LR(1) parse table
Action Goto State a + $ E 0 shift 2 1 1 shift 3 accept 2 reduce P3 reduce P (E → a) (E → a) 3 shift 2 4 4 shift 3 reduce P2 reduce P (E → E + E) (E → E + E)
Use the following 3-address and Java stack code instruc- tions for answering questions on the midterm.
3-addr Instruction Effect load R1 x R1 ← x store x R1 x ← R add R1 R2 R3 R1 ← R2 + R sub R1 R2 R3 R1 ← R2 - R mult R1 R2 R3 R1 ← R2 * R neg R1 R2 R1 ← -(R2)
Java Stack Code Effect nop none ldc int c push constant c onto stack iload index(x) push local variable X onto stack istore index(x) pop stack, store in local variable X iadd pop 2 elems off stack, add, push isub pop 2 elems off stack, subtract, push imult pop 2 elems off stack, multiply, push ineg pop stack, negate, push goto L jump to handle L ifeq L pop stack, jump to handle L if zero if icmpeq L pop 2 elems, jump to L if equal if icmpgt L pop 2 elems, jump to L if 1st greater dup duplicate top of stack pop pop top of stack swap swap top two positions of stack
Consider the code y = (x-y)+(x*z)
(a) Translate it into an AST.
/ \ /
/ | |
x y x z
(b) Translate it into 3-address code. load R1 x load R2 y sub R3 R1 R load R4 z mult R5 R1 R add R6 R3 R store y R (c) Translate it into Java stack code. iload index(x) iload index(y) isub iload index(x) iload index(z) imult iadd istore index (y) (d) What representation(s) are more compact? Why? Stack code is the most compact since addresses are implicit. (e) What representation(s) are easier for program trans- formations? Why? ASTs are easier for source-level transformations. 3-address code is easier for low-level transforma- tions. Stack code is difficult for transformations since the order of stack instructions is critical.