The document discusses LL(1) parsers and table-driven parsing. It defines LL(1) parsers as parsers that can be constructed to parse LL(1) grammars by looking ahead 1 symbol. Table-driven parsers use parsing tables generated from the grammar to parse inputs. The document provides details on computing FIRST and FOLLOW sets, building LL(1) parsing tables from grammars, and provides an example of a table-driven parse of an expression grammar.
Unleash Your Potential - Namagunga Girls Coding Club
LL(1) Parser Table
1. LL(1) PARSER
Mr.Sameer Prembabu Mamadapure
Information Technology Department
International Institute of Information Technology, I²IT
www.isquareit.edu.in
2. LL(1) Parsers
• The class of grammars for which we can construct
predictive parsers looking k symbols ahead in the
input is called the LL(k) class.
• Predictive parsers, that is, recursive-descent parsers
without backtracking, can be constructed for the LL(1)
class grammars.
• The first “L” stands for scanning input from left to
right. The second “L” for producing a leftmost
derivation. The “1” for using one input symbol of look-
ahead at each step to make parsing decisions.
3. Table-driven parsing
• Parsing performed by a finite state machine.
• Parsing algorithm is language-independent.
• FSM driven by table (s) generated automatically from
grammar.
• Language generator tables
parser
Input
tables
stack
5. Computing FIRST (N)
• If N ε First (N) includes ε
• if N aABC First (N) includes a
• if N X1X2 First (N) includes First (X1)
• if N X1X2… and X1 ε, First (N) includes First (X2)
Computing Follow (N)
• Follow (N) is computed from productions in which N appears
on the rhs
• For the sentence symbol S, Follow (S) includes $
• if A α N β, Follow (N) includes First (β)
– because an expansion of N will be followed by an expansion from β
• if A α N, Follow (N) includes Follow (A)
– because N will be expanded in the context in which A is expanded
• if A α N B , B ε, Follow (N) includes Follow (A)
6. Building LL (1) parse tables
Table indexed by non-terminal and token. Table entry is a production:
for each production P: A α loop
for each terminal a in First (α) loop
T (A, a) := P;
end loop;
if ε in First (α), then
for each terminal b in Follow (α) loop T (A, b) := P; end loop;
end if;
end loop;
• All other entries are errors.
• If two assignments conflict, parse table cannot be built.
7. Construction of a predictive parsing
table
• The following rules are used to construct the
predictive parsing table:
– 1. for each terminal a in FIRST(α),
add A → α to matrix M[A,a]
– 2. if λ is in FIRST(α), then
for each terminal b in FOLLOW(A),
add A → α to matrix M[A,b]
8. • Grammar for expressions, without left-recursion:
E → TE’ | T
E’ → +TE’ | ε
T → FT’ | F
T’ → *FT’ | ε
F → id | (E)
• First (F) = { id, ( }
• First (T’) = { *, ε} First (T) = { id, ( }
• First (E’) = { +, ε} First (E) = { id, ( }
• Follow (E) = { ), $ } Follow (E’) = { ), $ }
• Follow (T) = First (E’ ) + Follow (E’) = { +, ), $ }
• Follow (T’) = Follow (T) = { +, ), $ }
• Follow (F) = First (T’) + Follow (T’) = { *, +, ), $ }
9. + * ( ) id $
E -- -- E → TE’ E → TE’
E’ E’ → +TE’ E’ → ε E’ → ε
T T → FT’ T → FT’
T’ T’ → ε T’ → *FT’ T’ → ε T’ → ε
F F → (E) F → id
Construction of a predictive parsing table (Cont…)
10. Stack I/P O/P
$E Id + id * id $
$E’T Id + id * id $
$E’T’F Id + id * id $ E → TE’
$E’T’id Id + id * id $ F → id
$E’T’ + id * id $
$E’ + id * id $ T’ → ε
$E’T+ + id * id $ E’ → +TE’
$E’T id * id $
$E’T’F id * id $ T → FT’
$E’T’id id * id $ F → id
$E’T’ * id $
$E’T’F* * id $ T’ → *FT’
$E’T’F id $ F → id
$E’T’ $
$E’ $ T’ → ε
$ $ E’ → ε
11. International Institute of Information
Technology (I²IT),
P-14, Rajiv Gandhi Infotech Park, MIDC
Phase – 1, Hinjawadi, Pune – 411057,
India
info@isquareit.edu.in
http://www.isquareit.edu.in/