



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!




COP4020 Spring 2008 Homework-2 Solutions
1. Consider the following context-free grammar:
S → SASB S → d A → Aa A → e B → Bb B → Cc C → c
(a) Show that the grammar is not LL(1). (b) Modify the grammar so that it becomes LL(1). (c) Construct the corresponding LL(1) parse table. (d) Write the skeleton of the corresponding recursive descent parser.
Answer: (a) S → SASB {d} S → d {d} A → Aa {e} A → e {e} B → Bb {c} B → Cc {c} C → c {c} So this grammar is not LL(1).
(b) S → dS’ {d} S’ → ASBS’ {e} → { eof, c} A → eA’ {e} A’ → aA’ {a} → {d} B → CcB’ {c} B’ → bB’ {b} → {e, c, eof} C → c {c}
(c) Parse Table:
a b c d e end-of-file
S S → dS’ S’ S’→ S’→ASBS’ S’→ A bA’ A→eA’ A’ A’→aA’ A’→ B B→CcB’ B’ B’→bB’ B’→ B’→ B’→ C c
(d) proc S; {S → dS’} if Next Token = T_d then Read (T_d); S’; end
proc S’; {S’ → ASBS’ → } case Next Token of T_e: A; S; B; S’ T_c: ; otherwise Error end; end;
proc A; {A → eA’} if Next Token = T_e then Read (T_e); A’; end;
proc A’; {A’ → aA’ → } if Next Token = T_a then Read (T_a); A’; end;
proc B; {B → CcB’} C; Read (T_c); B’; end;
proc B’; {B’ → bB’ → } case Next Token of T_b: Read (T_b); B’; T_c, T_e: ; otherwise Error end; end;
proc C; {C → c} if Next Token = T_c then Read (T_c); end;