Compiler Construction CS-4207: Instructor Name: Atif Ishaq
Compiler Construction CS-4207: Instructor Name: Atif Ishaq
CS-4207
Left Factoring
LL(k) Grammar
2
Left Factoring
Choice between the two productions is not clear – rewrite the production to
defer decision until enough input has been seen to make right choice
When a nonterminal has two or more productions whose right-hand sides start
with the same grammar symbols, the grammar is not LL(1) and cannot be used
for predictive parsing
A 1 | 2 | … | n |
with
AZ|
Z 1 | 2 | … | n
3
Left Factoring - Example
Here, i, t, and e stand for if, then, and else; E and S stand for "conditional
expression" and "statement." Left-factored, this grammar becomes
4
Top Down (Predictive) Parsing
Two variants
5
Recursive Decent Parsing
6
LL(k) Grammars
7
LL(k) Grammars - Example
8
Constructing LL(1) Tables
9
Computing First(X)
10
Computing Follow(X)
11
FIRST and FOLLOWS
Top-down parsing and bottom-up parsing aided by two methods FRIST and
FOLLOWS
During panic mode error recovery set of tokens produced by FOLLOWS can be
used as synchronizing tokens
A 1 | 2 | … | n
2. if i * then
2.a. j * for all i j
2.b. FIRST(j) FOLLOW(A) =
for all i j
13
FIRST and FOLLOWS
1. Place $ in Follow(S), where S is the start symbol and $ is the input buffer end-
marker.
2. If there is a production A B ,
14
Constructing an LL(1) Parsing Table
15
Example : Constructing LL(1) Parsing Table
16
Top-Down Parsing
17
LL(1) Parsing of Arithmetical Expression
18
19