hw2Ans1 - 1. Consider the following grammar. Note that the...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
1. Consider the following grammar. Note that the terminals include x, [, ], (, and ). S [ST] | T T (TF) | F F x | ε (a) Compute the First set for all nonterminals in the grammar. First(S) = { [, (, x, ε } First(T) = { (, x, ε } First(F) = { x, ε } (b) Compute the First set for each production (i.e., the right side of the production). First(S [ST]) = { [ } First(S T) = { (, x, ε } First(T (TF)) = { ( } First(T F) = { x, ε } First(F x) = { x } First(F ε ) = { ε } (c) Compute the Follow set for all non-terminals in the grammar. Follow(S) = { ], (, x, $} Follow(T) = { ], (, ), x, $ } Follow(F) = { ], (, ), x, $ } (d) Based on the First and Follow sets you obtained in (a), (b), and (c), determine whether the grammar is LL(1). Explain how your decision is made. The grammar is not LL(1). Because there is an overlap between the first set of one nonempty production and the follow set of the left side non-terminal of that production. F ε , First(F x) = { x }, Follow(F) = { ], (, ), x, $ } b First(F x) Follow(F) φ (e) Build an LL(1) parsing table for the grammar. x [ ] ( ) $ S S T S [ST] S T S T S T T T F T F T (TF) T F T F T F F F x F ε F ε F ε F ε F ε (f) Parse the string [[x()](((xx))x)]. When there are conflicts, choose the one you know that would work. Show the stack, the input, and the action taken.
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Stack Input action S$ [[x()](((xx))x)]$ S [ST] ST]$ [x()](((xx))x)]$ S [ST] ST]T]$ x()](((xx))x)]$ S T TT]T]$ x()](((xx))x)]$ T F FT]T]$ x()](((xx))x)]$ F x T]T]$ ()](((xx))x)]$ T (TF) TF)]T]$ )](((xx))x)]$ T F FF)]T]$ )](((xx))x)]$ F ε F)]T]$ )](((xx))x)]$ F ε T]$ (((xx))x)]$ T (TF) TF)]$ ((xx))x)]$ T (TF) TF)F)]$ (xx))x)]$ T (TF) TF)F)F)]$ xx))x)]$ T F FF)F)F)]$ xx))x)]$ F x F)F)F)]$ x))x)]$ F x F)F)]$ )x)]$ F ε F)]$ x)]$ F x $ $ Accept (g) Build the parse tree while you are parsing. Show your parse tree.
Background image of page 2
S [ S T ] [ S T ] ( T F ) T ( T F ) ( T F ) x F F ε ( T F ) ε x ε F x x 2. Consider the following grammar G. Note that T = {a, b, (, )}. S AB | ab A (a) | ε B (b) | ε (a) Show that G is not LL(1) First (A (a)) = { ( } Follow(A) = { (, $ } There is an overlap between the first set of one nonempty production and the follow set of the left side non-terminal of that production. A ε , First (A (a)) = { ( }, Follow(A) = { (, $ } b First(A (a)) Follow(A) φ
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
(b) Build an LL(2) parse table for G. You can skip steps to quickly build the parse table. ab (a (b $ S S ab S AB S AB S AB A A (a) A ε A ε B B (b) B ε 3. Consider the following grammar. Note that the terminals include id, +, [, ], and “,”. E
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/25/2012 for the course CS 6413 taught by Professor Ye during the Spring '07 term at University of Texas at Dallas, Richardson.

Page1 / 14

hw2Ans1 - 1. Consider the following grammar. Note that the...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online