80 likes | 89 Views
Tutorial-4. LL(1) Parsing. First Sets. Recall that we construct the first sets using the following algorithm Algorithm : If t is a terminal First (t) = { t } If X is non-terminal, then ξ ϵ First(X) If X → ξ
E N D
Tutorial-4 LL(1) Parsing
First Sets • Recall that we construct the first sets using the following algorithm • Algorithm : • If t is a terminal First (t) = { t } • If X is non-terminal, then ξϵ First(X) • If X →ξ • Or if X → A1,…An and ξϵ First(Ai) for 1 ≤ i ≤ ni.e. if A1,…An can disappear by producing ξ • First (α) is a subset of First(X) if X → A1,…Anα and ξϵ First(Ai) for 1 ≤ i ≤ n (i.e. A1,…An can all disappear)
Follow Sets • Recall that we construct the follow sets using this algorithm • $ ϵ Follow(S), where S is the start symbol • For each production A → α X β First(β) – { ξ } is a subset of Follow(X) (notice that we exclude ξ , because ξ is never in a follow set) 3. For each production A → α X β if ξ ϵ First(β) (i.e., β can completely disappear) then whatever is in Follow(A) is also in Follow(X) i.e., Follow(A) is a subset of Follow(X)
Exercise • Consider the following grammar over the alphabet ∑ = {u; v; w; x; y; z}: S -> UVW U -> u | Wv | ξ V -> w | xU | ξ W -> y | z • Give the first sets of the non-terminals and the follow sets of the terminals and non-terminals.
Give the parsing table for this grammar. • Recall that the LL parsing table is constructed as follows • For each production A→ α in G do • For each terminal t ϵ First(α) do • T[A,t]= αbecause obviously would is useful here • If ξϵ First(α), for each t ϵ Follow(A) do • T[A,t]= αbecause α can completely disappear and consequently A disappears. • If ξϵ First(α) and $ ϵ Follow(A) do • T[A,$]= αThis is useful when we ran out of input because the only hope would be is to get rid of whatever is on the stack.
Soltion • Is this grammar LL(1)? Explain.