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

This preview shows pages 1–6. Sign up to view the full content.

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, \$ } bright 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.

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

View Full Document
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.
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) = { (, \$ } bright First(A (a)) Follow(A) φ

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

View Full Document
(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 E + T | T T id | id[] | id[X] X E , E | E (a) Construct the LR(0) automata for the grammar. Step 1: augment G S E E E + T E T T id T id[] T id[X] X E , E X E Step 2: initial state Construct closure (I 0 ), add S E E E + T E T T id T id[] T id[X] Step 3: From state I 0 , for all grammar symbol X Construct J = Goto(I, X) Compute Closure(J) Repeat Step 3 till no new states can be derived
I 0 S E E E + T E T T id T id[] T id[X] I 1 S E E E + T I 2 E T I 3 T id T id [] T id [X] I 4 E E + T T id T id[] T id[X] I 5 T id[ ] T id[ X] X E , E X E E E + T E T T

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern