0% found this document useful (0 votes)
61 views73 pages

LL 1

Uploaded by

huzafa zaheer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
61 views73 pages

LL 1

Uploaded by

huzafa zaheer
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 73

LL(1)

PARSER
PARSING

When the parser starts constructing


Where bottom-up parsing starts
the parse tree from the start symbol
with the input symbols and tries
and then tries to transform the start
to construct the parse tree up to
symbol to the input, it is called top-
the start symbol.
down parsing.
TOP DOWN PERSER

Predictive parser is a recursive


descent parser, which has the
capability to predict which
production is to be used to replace
the input string. The predictive
parser does not suffer from
backtracking.
PREDICTIVE PARSER

 Predictive parsing uses a stack


and a parsing table to parse
the input and generate a parse
tree.

 Both the stack and the input


contains an end symbol $ to
denote that the stack is empty
and the input is consumed.

 The parser refers to the parsing


table to take any decision on
the input and stack element
combination.
LL(1) PARSER

An LL parser is called an LL(k)


parser if it uses k tokens of look
ahead when parsing a sentence.

LL grammars, particularly LL(1)


grammars, as parsers are easy to
construct, and many computer
languages are designed to be LL(1)
for this reason.

The 1 stands for using one input


symbol of look ahead at each step
to make parsing action decision.
CONTINUE…
LL(k) parsers must predict which production replace a non-terminal with as
soon as they see the non-terminal. The basic LL algorithm starts with a
stack containing [S, $] (top to bottom) and does whichever of the following
is applicable until done:

▶ If the top of the stack is a non-terminal, replace the top of the stack
with one of the productions for that non-terminal, using the next k
input symbols to decide which one (without moving the input
cursor), and continue.

▶ If the top of the stack is a terminal, read the next input token. If it is
the same terminal, pop the stack and continue. Otherwise, the parse
has failed and the algorithm finishes.

▶ If the stack is empty, the parse has succeeded and the algorithm
finishes.

So look ahead meaning is - looking at input tokens without moving the input
cursor.
PRIME REQUIREMENT OF
LL(1)

 The grammar must


be - 
no left
factoring

no left
recursion
 FIRST() &
FOLLOW()
 Parsing Table
 Stack
Implementation
 Parse Tree
STEP: LEFT FACTORING
LEFT FACTORING
▶ A grammar is said to be left factored
when it is of the form – A -> αβ1 | αβ2 |
αβ3 | αβ4 …| αβn | Y
▶ The productions start with the same
terminal (or set of terminals).
▶ When the choice between two alternative A-productions is not
clear, we may be able to rewrite the productions to defer the
decision until enough of the input has been seen to make the
right choice.

For the grammar


A -> αβ1 | αβ2 | αβ3 | αβ4 …| αβn | Y

The equivalent left factored grammar


will be – A -> αA’ | γ
A’ -> β1 | β2 | β3 | β4 …| βn
CONTINUE…

▶ For example :
the input string is - aab &
grammar is
S ->aAb|aA|
ab A ->bAc|
ab
After removing left factoring
-
S-
>aA’
A’ ->Ab|
A|b A -
>ab|bAc
STEP: LEFT RECURSION
RECURSION
RECURSION:

The process in which a function calls itself directly or indirectly is called


recursion and the corresponding function is called as recursive function.

TYPES OF RECURSION

LEFT RECURSION RIGHT RECURSION


LEFT
Right
RECURSION
Recursion
For For
grammar: grammar:
A -> A | β A ->  A| β
A  A
A A   A
A   A
β  A
β

This parse tree generate β This parse tree generate 


* *β
RIGHT
RECURSION-
 A production of grammar is said to have right
recursion if the right most variable same as
variable of its LHS. e.g. A ->  A| β

 A grammar containing a production having right


recursion is called as a right recursive
grammar.
 Right recursion does not create any problem for
the top down parsers.

 Therefore, there is no need of eliminating right


recursion from the grammar.
LEFT
RECURSION-
 A production of grammar is said to have left
recursion if the leftmost variable of its RHS
is same as variable of its LHS. e.g. A -> A |

β
A grammar containing a production
having left recursion is called as a left

recursive grammar.
Left recursion is eliminated because top
down parsing method can not handle
left recursive grammar.
LEFT
RECURSION
A grammar is left recursive if it has a nonterminal A such that there is
a derivation
A -> A | β for some string .
Immediate/direct left recursion:
A production is immediately left recursive if its left hand side and the
head of its right hand side are the same symbol, e.g. A ->A  ,
where α is  sequence of non terminals and terminals.
Indirect left recursion:
Indirect left recursion occurs when the definition of left recursion is
satisfied via
several substitutions. It entails a set of rules following the pattern
. A → Br
B→
Cs C
→ At
Here, starting with a, we can derive A -
> Atsr
ELIMINATION OF LEFT-
RECURSION
 Suppose the grammar were
A  A | 

How could the parser decide how many times to use the
production A 
A before using the production A -->  ?

 Left recursion in a production may be removed by


transforming the grammar in the following way.
Replace
A  A | 
With
A  A'
A'  A' | 
EXAMPLE OF IMMEDIATE LEFT RECURSION:

Consider the left recursive


grammar
Now the
EE+T|T grammar is
E  T E'
TT*F|
F E'  + T E' |

F  (E) | id T  F T'
Apply the transformation
to E: T'  * F T' |

E  T E' F  (E) |
id
E'  + T E' | 
Then apply the transformation
to T:
T  F T'
EXAMPLE:
Consider the left recursive grammar
S → SX | SSb| XS |
a

X → Xb | Now the
Sa grammar is
S → XS′S′ | aS′
Apply the transformation to S:
S′ → XS′ | SbS′ | ε
S → XS′S′ | aS′ X → SaX’
S′ → XS′ | SbS′ | ε X′ → bX′ | ε

Apply the transformation to X:


X → SaX’
X′ → bX′ | ε
STEP:FIRST &
FOLLOW
WHY USING FIRST AND FOLLOW
During parsing FIRST and FOLLOW help us to
choose which production to apply , based on the next
input signal.

We know that we need of backtracking in


syntax analysis, which is really a complex process to
implement. There can be easier way to sort out this
problem by using FIRST AND FOLLOW.
If the compiler would have come to know in
advance, that what is the “first character of the string
produced when a production rule is applied”, and comparing
it to the current character or token in the input string it sees,
it can wisely take decision on which production rule to
RULES OF
FIRST
FIRST always find out the terminal symbol from the
grammar. When we check out FIRST for any symbol
then if we find any terminal symbol in first place then
we take it. And not to see the next symbol.
 If a grammar is

A → a then FIRST ( A )={ a }


 If a grammar is
A → a B then FIRST ( A )={ a }
RULES OF
FIRST
 If a grammar is
A → aB ǀ ε hen FIRST ( A )={ a, ε }
 If a grammar is

A → BcD ǀ ε

B → eD ǀ ( A
)
Here B is non terminal. So, we check the
transition of B and find the FIRST of A.
then FIRST ( A )={ e,( , ε }
RULES OF
FOLLOW
For doing FOLLOW operation we need FIRST operation mostly. In
FOLLOW we use a $ sign for the start symbol. FOLLOW always
check the right
portion of the symbol.
 If a grammar is

A → BAc ; A is start symbol.


Here firstly check if the selected symbol stays in right side of the
grammar.
We see that c is right in A.
then FOLLOW (A) = {c , $ }
RULES OF
FOLLOW
 If a grammar is
A → BA’

A' →*Bc
Here we see that there is nothing at the right side
of A' . So
FOLLOW ( A' )= FOLLOW ( A )= { $ }
Because A' follows the start symbol.
RULES OF
FOLLOW
 If a grammar
is A → BC
 B → Td
 C →*D ǀ ε
When we want to find FOLLOW (B),we see that B follows by
C . Now put the FIRST( C) in the there.
FIRST(C)={*, ε }.
EXAMPLE OF FIRST AND
FOLLOW

GRAMMAR:
E -> TE'
E'-> +TE' | Symbol FIRST FOLLOW
ε
T -> FT' E
T' -> *FT' |
ε F -> E’
(E) |id
T

T’

F
EXAMPLE OF FIRST AND
FOLLOW

GRAMMAR:
E -> TE'
E'-> +TE'|ε Symbol FIRST FOLLOW
T -> FT'
T' -> *FT'| E { ( , id }
ε F ->
(E)|id E'

T'

F
EXAMPLE OF FIRST AND
FOLLOW

GRAMMAR:
E -> TE'
E'-> +TE'|ε Symbol FIRST FOLLOW
T -> FT'
T' -> *FT'| E { ( , id }
ε F ->
(E)|id E' {+,ε}

T'

F
EXAMPLE OF FIRST AND
FOLLOW

GRAMMAR:
E -> TE'
E'-> +TE'|ε Symbol FIRST FOLLOW
T -> FT'
T' -> *FT'| E { ( , id }
ε F ->
(E)|id E' {+,ε}

T { id , ( }

T'

F
EXAMPLE OF FIRST AND
FOLLOW

GRAMMAR:
E -> TE'
E'-> +TE'|ε Symbol FIRST FOLLOW
T -> FT'
T' -> *FT'| E { ( , id }
ε F ->
(E)|id E' {+,ε}

T { id , ( }

T' {*,ε }

F
EXAMPLE OF FIRST AND
FOLLOW

GRAMMAR:
E -> TE'
E'-> +TE'|ε Symbol FIRST FOLLOW
T -> FT'
T' -> *FT'| E { ( , id }
ε F ->
(E)|id E' {+,ε}

T { id , ( }

T' {*,ε}

F { id , ( }
EXAMPLE OF FIRST AND
FOLLOW

GRAMMAR:
E -> TE'
E'-> +TE'|ε Symbol FIRST FOLLOW
T -> FT'
T' -> *FT'| E { ( , id } {$,)}
ε F ->
(E)|id E' {+,ε}

T { id , ( }

T' {*,ε}

F { id , ( }
EXAMPLE OF FIRST AND
FOLLOW

GRAMMAR:
E -> TE'
E'-> +TE'|ε Symbol FIRST FOLLOW
T -> FT'
T' -> *FT'| E { ( , id } {$,)}
ε F ->
(E)|id E' {+,ε} {$,)}

T { id , ( }

T' {*,ε }

F { id , ( }
EXAMPLE OF FIRST AND
FOLLOW

GRAMMAR:
E -> TE'
E'-> +TE'|ε Symbol FIRST FOLLOW
T -> FT'
T' -> *FT'| E { ( , id } {$,)}
ε F ->
(E)|id E' {+,ε} {$,)}

T { id , ( } { $ , ) ,+ }

T' {*,ε }

F { id , ( }
EXAMPLE OF FIRST AND
FOLLOW

GRAMMAR:
E -> TE'
E'-> +TE'|ε Symbol FIRST FOLLOW
T -> FT'
T' -> *FT'| E { ( , id } {$,)}
ε F ->
(E)|id E' {+,ε} {$,)}

T { id , ( } { $ , ) ,+ }

T' {*,ε} {$,),+}

F { id , ( }
EXAMPLE OF FIRST AND
FOLLOW

GRAMMAR:
E -> TE'
E'-> +TE'|ε Symbol FIRST FOLLOW
T -> FT'
T' -> *FT'| E { ( , id } {$,)}
ε F ->
(E)|id E' {+,ε} {$,)}

T { id , ( } { $ , ) ,+ }

T' {*,ε } {$,),+}

F { id , ( } {$,),+,*}
STEP: PARSING TABLE
EXAMPLE OF LL(1)
GRAMMAR
E -> TE’
E’ -> +TE’ | ε
T -> FT’
T’ -> *FT’ | ε
F -> (E) | id
Production FIRST FOLLOW
E -> TE’ { ( , id } {$,)}

E’ -> +TE’|ε {+,ε} {$,)}

T -> FT’ { ( , id } {+,$,)}

T’ -> *FT’|ε {*, ε} {+,$,)}

F -> (E)|id { ( , id } { *, + , $ , ) }

Non
Termin TAB
INPUT SYMBOLS
al
LE:
TABLE: id + * ( ) $

PARSING E FIRST
TABLE E’
&
T
T’ FOLLO
F W
Production FIRST FOLLOW
E -> TE’ { ( , id } {$,)}

E’ -> +TE’|ε {+,ε} {$,)}

T -> FT’ { ( , id } {+,$,)}

T’ -> *FT’|ε {*, ε} {+,$,)}

F -> (E)|id { ( , id } { *, + , $ , ) }

TAB
Non
INPUT SYMBOLS
Termin
al
LE:
TABLE: id + * ( ) $

PARSING E E -> TE’ FIRST


TABLE E’ &
FOLLO
T
T’
F W
Production FIRST FOLLOW
E -> TE’ { ( , id } {$,)}

E’ -> +TE’|ε {+,ε} {$,)}

T -> FT’ { ( , id } {+,$,)}

T’ -> *FT’|ε {*, ε} {+,$,)}

F -> (E)|id { ( , id } { *, + , $ , ) }

TAB
Non
INPUT SYMBOLS
Termin
al
LE:
TABLE: id + * ( ) $

PARSING E E -> TE’ FIRSTE -> TE’


TABLE E’ &
FOLLO
T
T’
F W
Production FIRST FOLLOW
E -> TE’ { ( , id } {$,)}

E’ -> +TE’|ε {+,ε} {$,)}

T -> FT’ { ( , id } {+,$,)}

T’ -> *FT’|ε {*, ε} {+,$,)}

F -> (E)|id { ( , id } { *, + , $ , ) }

TAB
Non
INPUT SYMBOLS
Termin
al

id + LE:
* ( ) $
TABLE:
PARSING E E -> TE’ FIRSTE -> TE’
TABLE E’ E’-> +TE’ &
T
T’
FOLLO
F W
Production FIRST FOLLOW
E -> TE’ { ( , id } {$,)}

E’ -> +TE’|ε {+,ε} {$,)}

T -> FT’ { ( , id } {+,$,)}

T’ -> *FT’|ε {*, ε} {+,$,)}

F -> (E)|id { ( , id } { *, + , $ , ) }

TAB
Non
INPUT SYMBOLS
Terminal

id + LE:
* ( ) $
TABLE:
PARSING E E -> TE’ FIRSTE -> TE’
TABLE E’ E’-> +TE’ & E’ -> ε E’ -> ε

T
T’
FOLLO
F
W
Production FIRST FOLLOW
E -> TE’ { ( , id } {$,)}

E’ -> +TE’|ε {+,ε} {$,)}

T -> FT’ { ( , id } {+,$,)}

T’ -> *FT’|ε {*, ε} {+,$,)}

F -> (E)|id { ( , id } { *, + , $ , ) }

TAB
Non
INPUT SYMBOLS
Terminal

id + LE:
* ( ) $
TABLE:
PARSING E E -> TE’ FIRSTE -> TE’
TABLE E’ E’-> +TE’ & E’ -> ε E’ -> ε

T
T’
T -> FT’
FOLLO
F
W
Production FIRST FOLLOW
E -> TE’ { ( , id } {$,)}

E’ -> +TE’|ε {+,ε} {$,)}

T -> FT’ { ( , id } {+,$,)}

T’ -> *FT’|ε {*, ε} {+,$,)}

F -> (E)|id { ( , id } { *, + , $ , ) }

TAB
Non
INPUT SYMBOLS
Termin
al

id + LE:* ( ) $
TABLE:
PARSING
E E -> TE’
FIRST E -> TE’

TABLE
E’ E’-> +TE’
& E’ -> ε E’ -> ε

FOLLO
T T -> FT’ T -> FT’

T’
F
W
Production FIRST FOLLOW
E -> TE’ { ( , id } {$,)}

E’ -> +TE’|ε {+,ε} {$,)}

T -> FT’ { ( , id } {+,$,)}

T’ -> *FT’|ε {*, ε} {+,$,)}

F -> (E)|id { ( , id } { *, + , $ , ) }

Non
Termin
al
TAB
INPUT SYMBOLS

id + LE: * ( ) $
TABLE:
PARSING
E E -> TE’
FIRST E -> TE’

E’ E’-> +TE’ E’ -> ε E’ -> ε


TABLE
T T -> FT’
& T -> FT’

T’ FOLLO
T’ -> *FT’

F
W
Production FIRST FOLLOW
E -> TE’ { ( , id } {$,)}

E’ -> +TE’|ε {+,ε} {$,)}

T -> FT’ { ( , id } {+,$,)}

T’ -> *FT’|ε {*, ε} {+,$,)}

F -> (E)|id { ( , id } { *, + , $ , ) }

Non
Termin
al
TAB
INPUT SYMBOLS

id + LE: * ( ) $
TABLE:
PARSING
E E -> TE’
FIRST E -> TE’

E’ E’-> +TE’ E’ -> ε E’ -> ε


TABLE
T T -> FT’
& T -> FT’

T’ T’ -> ε FOLLO
T’ -> *FT’ T’ -> ε T’ -> ε

F
W
Production FIRST FOLLOW
E -> TE’ { ( , id } {$,)}

E’ -> +TE’|ε {+,ε} {$,)}

T -> FT’ { ( , id } {+,$,)}

T’ -> *FT’|ε {*, ε} {+,$,)}

F -> (E)|id { ( , id } { *, + , $ , ) }

Non
Termin
al
TAB
INPUT SYMBOLS

id + LE: * ( ) $
TABLE:
PARSING
E E -> TE’
FIRST E -> TE’

E’ E’-> +TE’ E’ -> ε E’ -> ε


TABLE
T T -> FT’
& T -> FT’

T’ T’ -> ε FOLLO
T’ -> *FT’ T’ -> ε T’ -> ε

F F -> id
W
Production FIRST FOLLOW
E -> TE’ { ( , id } {$,)}

E’ -> +TE’|ε {+,ε} {$,)}

T -> FT’ { ( , id } {+,$,)}

T’ -> *FT’|ε {*, ε} {+,$,)}

F -> (E)|id { ( , id } { *, + , $ , ) }

Non
Termin
al
TAB
INPUT SYMBOLS

id + LE: * ( ) $
TABLE:
PARSING
E E -> TE’
FIRST E -> TE’

E’ E’-> +TE’ E’ -> ε E’ -> ε


TABLE
T T -> FT’
& T -> FT’

T’ T’ -> ε FOLLO
T’ -> *FT’ T’ -> ε T’ -> ε

F F -> id
W F -> (E)
CONTINUE…

Non
INPUT SYMBOLS
Termin
al
id + * ( ) $
E E -> TE’ E -> TE’
E’ E’-> +TE’ E’ -> ε E’ -> ε
T T -> FT’ T -> FT’
T’ T’ -> ε T’ -> *FT’ T’ -> ε T’ -> ε
F F -> id F -> (E)

TABLE: PARSING TABLE


 This grammar is LL(1).
 So, the parse tree can be derived from the
stack
implementation of the given parsing table.
STEP: STACK IMPLEMENTATION
STACK
IMPLEMENTATION

 The predictive parser uses an explicit stack to keep track of


pending non-terminals. It can thus be implemented without
recursion.
 Note that productions output are tracing out a lefmost
derivation
 The grammar symbols on the stack make up left-sentential
forms
LL(1)
STACK
 The input buffer contains the string to be parsed; $ is
the end- of-input marker
 The stack contains a sequence of grammar symbols
 Initially, the stack contains the start symbol of the
grammar on the top of $.
LL(1)
STACK

The parser is controlled by a program that behaves as


follows:
 The program considers X, the symbol on top of the stack,
and a, the current input symbol.
 These two symbols, X and a determine the action of the
parser.
LL(1)
STACK

Grammar Example:
:
Let’s parse the
E -> TE' input string
E'-> +TE'
| ε T -> id+idid
FT' Using the
T' -> *FT' | nonrecursive
ε LL(1) parser
F -> (E) |
id
ID + ID  ID
$

E
$
stac Parsin
k g
Table
PARSING
TABLE
id + * ( ) $
E E →TE' E →TE'

E' E' → E' → E' →


+TE'
T T →FT' T →FT'

T' T' →  T →*FT' T' → T' →

F F → id F →(E )
MATCHED STACK INPUT ACTION
E$ id+id * id$
TE’$ id+id * id$ E->TE’

FT’E’$ id+id * id$ T->FT’

id T’E’$ id+id * id$ F->id


id T’E’$ +id * id$ Match id
id E’$ +id * id$ T’->Є
id +TE’$ +id * id$ E’-> +TE’
id+ TE’$ id * id$ Match +

id+ FT’E’$ id * id$ T-> FT’

id+ idT’E’$ id * id$ F-> id


id+id T’E’$ * id$ Match id
id+id * FT’E’$ * id$ T’-> *FT’
id+id * FT’E’$ id$ Match *
id+id * idT’E’$ id$ F-> id
id+id * id T’E’$ $ Match id
id+id * id E’$ $ T’-> Є
id+id * id $ $ E’-> Є
STEP: LL(1) PARSE TREE
LL(1) PARSE
TREE

 Top-down parsing expands a parse tree from


the start symbol to the leaves
 Always expand the leftmost non-terminal
id+idi
d E

E'
T

+ E'
F T' T


i  F T'
d

i * F T'
d
DONE…

i 
d
CONTINU
E…

 There are grammars which may requite LL(1)


parsing.

 For e.g. Look at next grammar…..


CONTINU
E…
TABLE: FIRST &
GRAMMAR: FOLLOW
SYMBOL FIRST FOLLOW
S  iEtSS’ | a
S a,i $,e
S’  eS |ε
S’ e,ε $,e
Eb
E b t
SYMBOL FIRST FOLLOW
S  iEtSS’ | a
a,i $,e TABLE:
S’  eS |ε
e, ε $,e
FAST &
Eb
b t
FOLLOW

Non
INPUT SYMBOLS
Termin
al
TABLE: a b e i t $
PARSING TABLE S

S’
E
SYMBOL FIRST FOLLOW
S  iEtSS’ | a
a,i $,e TABLE:
S’  eS |ε
e, ε $,e
FAST &
Eb
b t
FOLLOW

Non
INPUT SYMBOLS
Termin
al
TABLE: a b e i t $
PARSING TABLE S Sa

S’
E
SYMBOL FIRST FOLLOW
S  iEtSS’ | a
a,i $,e TABLE:
S’  eS |ε
e, ε $,e
FAST &
Eb
b t
FOLLOW

Non
INPUT SYMBOLS
Termin
al
TABLE: a b e i t $
PARSING TABLE S Sa
SiEtSS

S’
E
SYMBOL FIRST FOLLOW
S  iEtSS’ | a
a,i $,e TABLE:
S’  eS |ε
e, ε $,e
FAST &
Eb
b t
FOLLOW

Non
INPUT SYMBOLS
Termin
al
TABLE: a b e i t $
PARSING TABLE S Sa
SiEtSS

S’ S’  eS
E
SYMBOL FIRST FOLLOW
S  iEtSS’ | a
a,i $,e TABLE:
S’  eS |ε
e, ε $,e
FAST &
Eb
b t
FOLLOW

Non
INPUT SYMBOLS
Termin
al
TABLE: a b e i t $
PARSING TABLE S Sa
SiEtSS

S’  eS
S’ S’ε
S’ε
E
SYMBOL FIRST FOLLOW
S  iEtSS’ | a
a,i $,e TABLE:
S’  eS |ε
e, ε $,e FAST &
Eb
b t
FOLLOW

Non
INPUT SYMBOLS
Termin
al
TABLE: a b e i t $
PARSING TABLE S Sa
SiEtSS

S’  eS
S’ S’ε
S’ε
E Eb
SYMBOL FIRST FOLLOW
S  iEtSS’ | a
a,i $,e TABLE:
S’  eS |ε
e, ε $,e
FAST &
Eb
b t
FOLLOW

Non
INPUT SYMBOLS
Termina
l
a b e i t $
TABLE:
SiEtSS
PARSING TABLE S Sa

S’  eS
S’ S’ε
S’ε
AMBIGUITY
E Eb
CONTINU
E…
 The grammar is ambiguous and it is evident by the fact
that
we have two entries corresponding to M[S’,e]
containing S’  ε and S’  eS.
 Note that the ambiguity will be solved if we use
LL(2) parser, i.e.
Always see for the two input symbols.
 LL(1) grammars have distinct properties.
- No ambiguous grammar or left recursive
grammar can be LL(1).
 Thus , the given grammar is not LL(1).

You might also like