0% found this document useful (0 votes)
74 views27 pages

LL(1) Predictive Parsing Explained

The document discusses LL(1) grammars and predictive parsing. Key points: - LL(1) grammars allow for predictive parsing where the next production can be uniquely determined based on 1 input symbol. - The parsing table construction algorithm adds productions to the table based on the FIRST and FOLLOW sets of productions. - A predictive parser uses the parsing table to deterministically parse input based on the top of the stack and next input symbol. It pops and pushes symbols according to the table.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
74 views27 pages

LL(1) Predictive Parsing Explained

The document discusses LL(1) grammars and predictive parsing. Key points: - LL(1) grammars allow for predictive parsing where the next production can be uniquely determined based on 1 input symbol. - The parsing table construction algorithm adds productions to the table based on the FIRST and FOLLOW sets of productions. - A predictive parser uses the parsing table to deterministically parse input based on the top of the stack and next input symbol. It pops and pushes symbols according to the table.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

LL(1) Grammar

Predictive parsers can be constructed


for a class of grammars called LL(1).
L->Left
L->Leftmost derivation
1->One input symbol at each step
No left recursive or ambiguous
grammar can be LL(1)

Conditions of LL(1)
Grammar G is called LL(1) if and only if
whenever,
If A->| are two distinct productions of G,
the following conditions hold :1. (a) FIRST() , FIRST() must be disjoint.
This is to be able to deterministically
guess the production.
(b) At most one of the strings or can
derive (Since FIRST(), FIRST()
are disjoint.
2. If -> then FIRST() and FOLLOW(A)
must be disjoint

Algorithm for construction of


parsing table
INPUT :- Grammar G
OUTPUT:- Parsing table M
For each production A -> , do the
following :
1.For each terminal a in FIRST(A), add
A-> to M[A,].
2.If is in FIRST() then for each terminal b
in FOLLOW(A). A -> to M[A,b]. If b is $
then also add A -> to M[A,$].
3.If there is no production in M[A,a] , then
set M[A,a] to error.

Table driven predictive


parsing
A predictive parser can be built by
maintaining a stack explicitly.
The table driven parser has an input buffer,
stack containing sequence of grammar
symbols, parsing table and an output stream.
The input buffer contains the string to be
parsed followed by $.
Initially the stack contains start symbol of the
grammar on the top followed by $.
The parsing table deterministically guesses
the correct production to be used.

Flow Chart

Procedure of predictive
parser

The current symbol of the input string is


maintained by a pointer say ip.
In every step consider the set {a,} where
a is the symbol pointed by the ip and is
the top of the stack.
If is a Non Terminal ,then see the table cell
M{,a} for the production.
1.If M{,a} is a valid production then pop
the
stack , push the production into the stack.
2.If M{,a} is error or blank then report an
error

.If is a terminal then pop it from the


stack and also increment the input pointer
ip to point the next symbol in the input
string.
The output will be the set of productions
The following example illustrates the topdown predictive parser using parser table .
String: id + id * id
Grammar: Mentioned LL(1) Grammar in
Previous slides

Example of
Constructing a
Predictive Parsing
Table

Start with an empty parsing table; the


rows are
non-terminals and the columns are
NonInput Symbol
terminals.
Termin
x
y
+
*
(
)
$
al

e
e'
t
t'
f

for each production n

for each a
first()
add n to T[n , a]
if first() then
for each b
follow(n)
add n to T[n , a]
NonTermin
al
e
e'
t
t'
f

Current
production:
e

t e'

first( t e' ( x, y
)=
,

Input Symbol
+
*
(
e t e'

for each production n

for each a
first()
add n to T[n , a]
if first() then
for each b
follow(n)
add n to T[n , a]
NonTermin
al
e
e'
t
t'
f

x
e t e'

Current
production:
e

t e'

first( t e' ) x , y
= (,

Input Symbol
+
*
(
e t e'

for each production n

for each a
first()
add n to T[n , a]
if first() then
for each b
follow(n)
add n to T[n , a]
NonTermin
al
e
e'
t
t'
f

e t e' e t e'

Current
production:
e

t e'

first( t e' ) =
(, x,

Input Symbol
+
*
(
e t e'

for each production n

for each a
first()
add n to T[n , a]
if first() then
for each b
follow(n)
add n to T[n , a]
NonTermin
al
e
e'
t
t'
f

Current
production:
e'

first( + t
e' ) =

Input Symbol
+
*
(

e t e' e t e'

e t e'
e' + t e'

+ t e'
+

for each production n

for each a
first()
add n to T[n , a]
if first() then
for each b
follow(n)
add n to T[n , a]
NonTermin
al
e
e'
t
t'
f

Current
production:
e'

follow( e' $, )
)=

Input Symbol
+
*
(

e t e' e t e'

e t e'
e' + t e'

e'

for each production n

for each a
first()
add n to T[n , a]
if first() then
for each b
follow(n)
add n to T[n , a]
NonTermin
al
e
e'
t
t'
f

Current
production:
e'

follow( e' ) )
= $,

Input Symbol
+
*
(

e t e' e t e'

e'

e'

e t e'
e' + t e'

for each production n

for each a
first()
add n to T[n , a]
if first() then
for each b
follow(n)
add n to T[n , a]
NonTermin
al
e
e'
t
t'
f

Current
production:
t

first( f
t' ) =

Input Symbol
+
*
(

e t e' e t e'

f t'

( x, y
,
)

e'

e'

e t e'
e' + t e'
t f t'

for each production n

for each a
first()
add n to T[n , a]
if first() then
for each b
follow(n)
add n to T[n , a]
NonTermin
al
e
e'
t
t'
f

Current
production:
t

first( f t' )
= (,

Input Symbol
+
*
(

e t e' e t e'

x y
,
)

e'

e'

e t e'
e' + t e'

t f t'

f t'

t f t'

for each production n

for each a
first()
add n to T[n , a]
if first() then
for each b
follow(n)
add n to T[n , a]
NonTermin
al

e
e'

e t e' e t e'

t
t'
f

t f t'

Current
production:
t

first( f t' ) =
(, x,

Input Symbol
+
*
(

e'

e'

e t e'
e + t
e'

t f t'

f t'

t f t'

for each production n

for each a
first()
add n to T[n , a]
if first() then
for each b
follow(n)
add n to T[n , a]
NonTermin
al

e
e'

e t e' e t e'

t
t'
f

t f t'

Current
production:
t'

follow( t' , $, )
)=
+

Input Symbol
+
*
(

e'

e'

e t e'
e + t
e'

t f t'

t f t'
t'

t' * f t'

for each production n

for each a
first()
add n to T[n , a]
if first() then
for each b
follow(n)
add n to T[n , a]
NonTermin
al
e
e'
t
t'
f

Current
production:
t'

follow( t' ) $, )
= +,

Input Symbol
+
*
(

e t e' e t e'

e'

e'

e t e'
e' + t e'

t f t'

t f t'

t f t'
t'

t' * f t'

t'

for each production n

for each a
first()
add n to T[n , a]
if first() then
for each b
follow(n)
add n to T[n , a]
NonTermin
al
e
e'
t
t'
f

Current
production:
t'

follow( t' ) = )
+, $,

Input Symbol
+
*
(

e t e' e t e'

e'

e'

t'

t'

e t e'
e' + t e'

t f t'

t f t'

t f t'
t'

t' * f t'

for each production n

for each a
first()
add n to T[n , a]
if first() then
for each b
follow(n)
add n to T[n , a]
NonTermin
al
e
e'
t
t'
f

Current
production:
f

first( ( e ) (
)=

Input Symbol
+
*
(

e t e' e t e'

e'

e'

t'

t'

e t e'
e' + t e'

t f t'

(e)

t f t'

t f t'
t'

t' * f t'
f(e)

for each production n

for each a
first()
add n to T[n , a]
if first() then
for each b
follow(n)
add n to T[n , a]
NonTermin
al
e
e'
t
t'
f

Current
production:
f

first( x x
)=

Input Symbol
+
*
(

e t e' e t e'
t f t'

e'

e'

t'

t'

t f t'
t'

fx

e t e'
e' + t e'

t f t'

t' * f t'
f(e)

for each production n

for each a
first()
add n to T[n , a]
if first() then
for each b
follow(n)
add n to T[n , a]
NonTermin
al
e
e'
t
t'
f

Current
production:
f

first( y y
)=

Input Symbol
+
*
(

e t e' e t e'
t f t'
fy

e'

e'

t'

t'

t f t'
t'

fx

e t e'
e' + t e'

t f t'

t' * f t'
f(e)

Gramm
ar:
E

E'
|

TE'
+ TE'

T
T'
|

F T'
* F T'

F
|

ID

(E)

first and follow


sets:
Nonfirst
follow
Termina
l
E(, x, y
$, )
E' +,
$, )
T(, x, y +, $, )
T' *,
+, $, )
F(, x, y *, +, ),
$

MATCHED

STACK

INPUT

ACTION

id+id * id$
E$
id+id * id$

E->TE

id+id * id$

T->FT

id+id * id$

F->id

TE$
FTE$
id
TE$
id

+id * id$

Match id

+id * id$

T->

+id * id$

E-> +TE

TE$
id
E$
id
+TE$
id+

id * id$

Match +

id * id$

T-> FT

id * id$

F-> id

TE$
id+
FTE$
id+
idTE$

You might also like