0% found this document useful (0 votes)
61 views

Compiler Construction CS-4207: Instructor Name: Atif Ishaq

The document discusses LL(1) parsing and how to construct an LL(1) parsing table. It covers topics like left factoring, LL(k) grammars, computing FIRST and FOLLOW sets, and using those sets to eliminate conflicts and construct an LL(1) parsing table for a given grammar. It provides an example of constructing an LL(1) parsing table for arithmetic expressions and discusses how top-down predictive parsing works using that table.

Uploaded by

Faisal Shehzad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views

Compiler Construction CS-4207: Instructor Name: Atif Ishaq

The document discusses LL(1) parsing and how to construct an LL(1) parsing table. It covers topics like left factoring, LL(k) grammars, computing FIRST and FOLLOW sets, and using those sets to eliminate conflicts and construct an LL(1) parsing table for a given grammar. It provides an example of constructing an LL(1) parsing table for arithmetic expressions and discusses how top-down predictive parsing works using that table.

Uploaded by

Faisal Shehzad
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Compiler Construction

CS-4207

Instructor Name: Atif Ishaq


Lecture 10
Today’s Lecture

 Left Factoring

 LL(k) Grammar

 LL(1) Parsing Table Creation

LL(1) Parsing of Arithmetic Expression

2
Left Factoring

 A grammar transformation - useful for producing grammar suitable for


predictive, or top-down parsing

 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

Replace the production

A   1 |  2 | … |  n | 
with
AZ|
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

 Eliminate Left recursion from grammar

 Eliminate Left Factoring Grammar

 Compute FIRST and FOLLOWS

 Two variants

 Recursive (Recursive decent parsing)

 Non Recursive (table driven parsing)

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

 Allows us to choose which production to apply, based on next input

 During panic mode error recovery set of tokens produced by FOLLOWS can be
used as synchronizing tokens

 Define FIRST ()

FIRST() = { the set of terminals that begin all


strings derived from  }
For every production A  
FIRST(A) = {} if   T
FIRST(A) = {} if  = 
FIRST(A) = A FIRST() if   N
12
FIRST and FOLLOWS

 A grammar G is LL(1) if it is not left recursive and for each collection of


productions

A  1 | 2 | … | n

for nonterminal A the following holds:

1. FIRST(i)  FIRST(j) =  for all i  j

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

 Compute FOLLOW (A) for all non terminals A

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 ,

a) if  is nonterminal then everything in First() except for  is placed in


Follow(B).

b) if  is terminal then it is included in Follow(B).

3. If there is a production A   B, or a production A   B  where First()


contains , then everything in Follow(A) is in Follow(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

You might also like