SlideShare a Scribd company logo
LL(1)
PARSER
MODEL OF COMPILER FRONT END 2
Front End
Also called parsing , where
generates parse tree
Syntax analysis
PARSING 3
When the parser starts constructing the
parse tree from the start symbol and then
tries to transform the start symbol to the
input, it is called top-down parsing.
Where bottom-up parsing starts with
the input symbols and tries to construct
the parse tree up to the start symbol.
TOP DOWN PERSER 4
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 5
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 6
 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. (We
assume that there is a unique EOF-marker $ at the end of the input.)
So look ahead meaning is - looking at input tokens without moving the input
cursor.
7
PRIME REQUIREMENT OF LL(1)
 The grammar must be -
 no left factoring
 no left recursion
 FIRST() & FOLLOW()
 Parsing Table
 Stack Implementation
 Parse Tree
8
STEP: LEFT FACTORING
9
LEFT FACTORING
 A grammar is said to be left factored when it is of the form –
A -> αβ1 | αβ2 | αβ3 | …… | αβn | γ
 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 | …… | αβn | γ
The equivalent left factored grammar will be –
A -> αA’ | γ
A’ -> β1 | β2 | β3 | …… | βn
10
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
11
12
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
13
Left Recursion Right Recursion
For grammar: For grammar:
A -> A | β A ->  A| β
A
A  A
A   A
A   A
β  A
β
This parse tree generate β  * This parse tree generate  * β
14
Right recursion-
 A production of grammar is said to have right recursion if
the right most variable RHS is 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.
15
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.
16
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
17
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' | 
18
EXAMPLE OF IMMEDIATE LEFT RECURSION:
Consider the left recursive grammar
E  E + T | T
T  T * F | F
F  (E) | id
Apply the transformation to E:
E  T E'
E'  + T E' | 
Then apply the transformation to T:
T  F T'
T'  * F T' | 
Now the grammar is
E  T E'
E'  + T E' | 
T  F T'
T'  * F T' | 
F  (E) | id
19
Continue…
The case of several immediate left recursive  -productions. Assume
that the set of all  -productions has the form
A → A 1 | A 2 | · · · | A m | β1 | β2| · · · | βn
Represents all the  -productions of the grammar, and no βi begins
with A, then we can replace these  -productions by
A →β1A′ | β2A′| · · · | βnA′
A′ → 1A′ | 2A′ | · · · | mA′ | 
20
Example:
Consider the left recursive grammar
S → SX | SSb| XS | a
X → Xb | Sa
Apply the transformation to S:
S → XSS′ | aS′
S′ → XS′ | SbS′ | ε
Apply the transformation to X:
X → SaX′
X′ → bX′ | ε
Now the grammar is
S → XSS′ | aS′
S′ → XS′ | SbS′ | ε
X → SaX′
X′ → bX′ | ε
21
Example of elimination indirect left
recursion:
S A A | 0
A S S | 1
Considering the ordering S, A, we get:
S A A | 0
A S | 0S | 1
And removing immediate left recursion, we get
S A A | 0
A 0S A′ | 1A′
A′  ε | AS A′
22
STEP:FIRST & FOLLOW
23
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
apply .
FOLLOW is used only if the current non terminal can derive ε .
24
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 }
25
Rules of FIRST
 If a grammar is
A → aB ǀ ε then 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,( , ε }
26
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 , $ }
27
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.
28
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)={*, ε }.
But when the value is € it follows the parents symbol. So
FOLLOW(B)={*,$ }
29
Example of FIRST and FOLLOW
Symbol FIRST FOLLOW
E
E’
T
T’
F
GRAMMAR:
E -> TE'
E'-> +TE'|ε
T -> FT'
T' -> *FT'|ε
F -> (E)|id
30
Example of FIRST and FOLLOW
Symbol FIRST FOLLOW
E { ( , id }
E'
T
T'
F
GRAMMAR:
E -> TE'
E'-> +TE'|ε
T -> FT'
T' -> *FT'|ε
F -> (E)|id
31
Example of FIRST and FOLLOW
Symbol FIRST FOLLOW
E { ( , id }
E' { + , ε }
T
T'
F
GRAMMAR:
E -> TE'
E'-> +TE'|ε
T -> FT'
T' -> *FT'|ε
F -> (E)|id
32
Example of FIRST and FOLLOW
Symbol FIRST FOLLOW
E { ( , id }
E' { + , ε }
T { id , ( }
T'
F
GRAMMAR:
E -> TE'
E'-> +TE'|ε
T -> FT'
T' -> *FT'|ε
F -> (E)|id
33
Example of FIRST and FOLLOW
Symbol FIRST FOLLOW
E { ( , id }
E' { + , ε }
T { id , ( }
T' { * , ε }
F
GRAMMAR:
E -> TE'
E'-> +TE'|ε
T -> FT'
T' -> *FT'|ε
F -> (E)|id
34
Example of FIRST and FOLLOW
Symbol FIRST FOLLOW
E { ( , id }
E' { + , ε }
T { id , ( }
T' { * , ε }
F { id , ( }
GRAMMAR:
E -> TE'
E'-> +TE'|ε
T -> FT'
T' -> *FT'|ε
F -> (E)|id
35
Example of FIRST and FOLLOW
Symbol FIRST FOLLOW
E { ( , id } { $ , ) }
E' { + , ε }
T { id , ( }
T' { * , ε }
F { id , ( }
GRAMMAR:
E -> TE'
E'-> +TE'|ε
T -> FT'
T' -> *FT'|ε
F -> (E)|id
36
Example of FIRST and FOLLOW
Symbol FIRST FOLLOW
E { ( , id } { $ , ) }
E' { + , ε } { $ , ) }
T { id , ( }
T' { * , ε }
F { id , ( }
GRAMMAR:
E -> TE'
E'-> +TE'|ε
T -> FT'
T' -> *FT'|ε
F -> (E)|id
37
Example of FIRST and FOLLOW
Symbol FIRST FOLLOW
E { ( , id } { $ , ) }
E' { + , ε } { $ , ) }
T { id , ( } { $ , ) ,+ }
T' { * , ε }
F { id , ( }
GRAMMAR:
E -> TE'
E'-> +TE'|ε
T -> FT'
T' -> *FT'|ε
F -> (E)|id
38
Example of FIRST and FOLLOW
Symbol FIRST FOLLOW
E { ( , id } { $ , ) }
E' { + , ε } { $ , ) }
T { id , ( } { $ , ) ,+ }
T' { * , ε } { $ , ) , + }
F { id , ( }
GRAMMAR:
E -> TE'
E'-> +TE'|ε
T -> FT'
T' -> *FT'|ε
F -> (E)|id
39
Example of FIRST and FOLLOW
Symbol FIRST FOLLOW
E { ( , id } { $ , ) }
E' { + , ε } { $ , ) }
T { id , ( } { $ , ) ,+ }
T' { * , ε } { $ , ) , + }
F { id , ( } { $ , ) , + , * }
GRAMMAR:
E -> TE'
E'-> +TE'|ε
T -> FT'
T' -> *FT'|ε
F -> (E)|id
40
STEP: PARSING TABLE
41
Example of LL(1) grammar
E -> TE’
E’ -> +TE’|ε
T -> FT’
T’ -> *FT’|ε
F -> (E)|id
42
TABLE:
PARSING
TABLE
TABLE:
FIRST & FOLLOW
43
Production Symbol FOLLOW
E -> TE’ { ( , id } { $ , ) }
E’ -> +TE’|ε { + , ε } { $ , ) }
T -> FT’ { ( , id } { + , $ , ) }
T’ -> *FT’|ε { * , ε } { + , $ , ) }
F -> (E)|id { ( , id } { *, + , $ , ) }
Non
Terminal
INPUT SYMBOLS
id + * ( ) $
E
E’
T
T’
F
TABLE:
PARSING
TABLE
TABLE:
FIRST & FOLLOW
44
Production Symbol FOLLOW
E -> TE’ { ( , id } { $ , ) }
E’ -> +TE’|ε { + , ε } { $ , ) }
T -> FT’ { ( , id } { + , $ , ) }
T’ -> *FT’|ε { * , ε } { + , $ , ) }
F -> (E)|id { ( , id } { *, + , $ , ) }
Non
Terminal
INPUT SYMBOLS
id + * ( ) $
E E -> TE’
E’
T
T’
F
TABLE:
PARSING
TABLE
TABLE:
FIRST & FOLLOW
45
Production Symbol FOLLOW
E -> TE’ { ( , id } { $ , ) }
E’ -> +TE’|ε { + , ε } { $ , ) }
T -> FT’ { ( , id } { + , $ , ) }
T’ -> *FT’|ε { * , ε } { + , $ , ) }
F -> (E)|id { ( , id } { *, + , $ , ) }
Non
Terminal
INPUT SYMBOLS
id + * ( ) $
E E -> TE’ E -> TE’
E’
T
T’
F
TABLE:
PARSING
TABLE
TABLE:
FIRST & FOLLOW
46
Production Symbol FOLLOW
E -> TE’ { ( , id } { $ , ) }
E’ -> +TE’|ε { + , ε } { $ , ) }
T -> FT’ { ( , id } { + , $ , ) }
T’ -> *FT’|ε { * , ε } { + , $ , ) }
F -> (E)|id { ( , id } { *, + , $ , ) }
Non
Terminal
INPUT SYMBOLS
id + * ( ) $
E E -> TE’ E -> TE’
E’ E’-> +TE’
T
T’
F
TABLE:
PARSING
TABLE
TABLE:
FIRST & FOLLOW
47
Production Symbol FOLLOW
E -> TE’ { ( , id } { $ , ) }
E’ -> +TE’|ε { + , ε } { $ , ) }
T -> FT’ { ( , id } { + , $ , ) }
T’ -> *FT’|ε { * , ε } { + , $ , ) }
F -> (E)|id { ( , id } { *, + , $ , ) }
Non
Terminal
INPUT SYMBOLS
id + * ( ) $
E E -> TE’ E -> TE’
E’ E’-> +TE’ E’ -> ε E’ -> ε
T
T’
F
TABLE:
PARSING
TABLE
TABLE:
FIRST & FOLLOW
48
Production Symbol FOLLOW
E -> TE’ { ( , id } { $ , ) }
E’ -> +TE’|ε { + , ε } { $ , ) }
T -> FT’ { ( , id } { + , $ , ) }
T’ -> *FT’|ε { * , ε } { + , $ , ) }
F -> (E)|id { ( , id } { *, + , $ , ) }
Non
Terminal
INPUT SYMBOLS
id + * ( ) $
E E -> TE’ E -> TE’
E’ E’-> +TE’ E’ -> ε E’ -> ε
T T -> FT’
T’
F
TABLE:
PARSING
TABLE
TABLE:
FIRST & FOLLOW
49
Production Symbol FOLLOW
E -> TE’ { ( , id } { $ , ) }
E’ -> +TE’|ε { + , ε } { $ , ) }
T -> FT’ { ( , id } { + , $ , ) }
T’ -> *FT’|ε { * , ε } { + , $ , ) }
F -> (E)|id { ( , id } { *, + , $ , ) }
Non
Terminal
INPUT SYMBOLS
id + * ( ) $
E E -> TE’ E -> TE’
E’ E’-> +TE’ E’ -> ε E’ -> ε
T T -> FT’ T -> FT’
T’
F
TABLE:
PARSING
TABLE
TABLE:
FIRST & FOLLOW
50
Production Symbol FOLLOW
E -> TE’ { ( , id } { $ , ) }
E’ -> +TE’|ε { + , ε } { $ , ) }
T -> FT’ { ( , id } { + , $ , ) }
T’ -> *FT’|ε { * , ε } { + , $ , ) }
F -> (E)|id { ( , id } { *, + , $ , ) }
Non
Terminal
INPUT SYMBOLS
id + * ( ) $
E E -> TE’ E -> TE’
E’ E’-> +TE’ E’ -> ε E’ -> ε
T T -> FT’ T -> FT’
T’ T’ -> *FT’
F
TABLE:
PARSING
TABLE
TABLE:
FIRST & FOLLOW
51
Production Symbol FOLLOW
E -> TE’ { ( , id } { $ , ) }
E’ -> +TE’|ε { + , ε } { $ , ) }
T -> FT’ { ( , id } { + , $ , ) }
T’ -> *FT’|ε { * , ε } { + , $ , ) }
F -> (E)|id { ( , id } { *, + , $ , ) }
Non
Terminal
INPUT SYMBOLS
id + * ( ) $
E E -> TE’ E -> TE’
E’ E’-> +TE’ E’ -> ε E’ -> ε
T T -> FT’ T -> FT’
T’ T’ -> ε T’ -> *FT’ T’ -> ε T’ -> ε
F
TABLE:
PARSING
TABLE
TABLE:
FIRST & FOLLOW
52
Production Symbol FOLLOW
E -> TE’ { ( , id } { $ , ) }
E’ -> +TE’|ε { + , ε } { $ , ) }
T -> FT’ { ( , id } { + , $ , ) }
T’ -> *FT’|ε { * , ε } { + , $ , ) }
F -> (E)|id { ( , id } { *, + , $ , ) }
Non
Terminal
INPUT SYMBOLS
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
TABLE:
PARSING
TABLE
TABLE:
FIRST & FOLLOW
53
Production Symbol FOLLOW
E -> TE’ { ( , id } { $ , ) }
E’ -> +TE’|ε { + , ε } { $ , ) }
T -> FT’ { ( , id } { + , $ , ) }
T’ -> *FT’|ε { * , ε } { + , $ , ) }
F -> (E)|id { ( , id } { *, + , $ , ) }
Non
Terminal
INPUT SYMBOLS
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)
Continue…
54
TABLE: PARSING TABLE
Non
Terminal
INPUT SYMBOLS
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)
 This grammar is LL(1).
 So, the parse tree can be derived from the stack
implementation of the given parsing table.
 There are grammars which may requite LL(1) parsing.
 For e.g. Look at next grammar…..
55
Continue…
Continue…
GRAMMAR:
S  iEtSS’ | a
S’  eS |ε
E  b
SYMBOL FIRST FOLLOW
S a , i $ , e
S’ e , ε $ , e
E b t
TABLE: FIRST & FOLLOW
56
TABLE:
PARSING TABLE
TABLE:
FAST & FOLLOW
57
SYMBOL FIRST FOLLOW
S  iEtSS’ | a
a , i $ , e
S’  eS |ε
e, ε $ , e
E  b
b t
Non
Terminal
INPUT SYMBOLS
a b e i t $
S
S’
E
TABLE:
PARSING TABLE
TABLE:
FAST & FOLLOW
58
SYMBOL FIRST FOLLOW
S  iEtSS’ | a
a , i $ , e
S’  eS |ε
e, ε $ , e
E  b
b t
Non
Terminal
INPUT SYMBOLS
a b e i t $
S Sa
S’
E
TABLE:
PARSING TABLE
TABLE:
FAST & FOLLOW
59
SYMBOL FIRST FOLLOW
S  iEtSS’ | a
a , i $ , e
S’  eS |ε
e, ε $ , e
E  b
b t
Non
Terminal
INPUT SYMBOLS
a b e i t $
S Sa
SiEtSS
’
S’
E
TABLE:
PARSING TABLE
TABLE:
FAST & FOLLOW
60
SYMBOL FIRST FOLLOW
S  iEtSS’ | a
a , i $ , e
S’  eS |ε
e, ε $ , e
E  b
b t
Non
Terminal
INPUT SYMBOLS
a b e i t $
S Sa
SiEtSS
’
S’ S’  eS
E
TABLE:
PARSING TABLE
TABLE:
FAST & FOLLOW
61
SYMBOL FIRST FOLLOW
S  iEtSS’ | a
a , i $ , e
S’  eS |ε
e, ε $ , e
E  b
b t
Non
Terminal
INPUT SYMBOLS
a b e i t $
S Sa
SiEtSS
’
S’
S’  eS
S’ε
S’ε
E
TABLE:
PARSING TABLE
TABLE:
FAST & FOLLOW
62
SYMBOL FIRST FOLLOW
S  iEtSS’ | a
a , i $ , e
S’  eS |ε
e, ε $ , e
E  b
b t
Non
Terminal
INPUT SYMBOLS
a b e i t $
S Sa
SiEtSS
’
S’
S’  eS
S’ε
S’ε
E E  b
TABLE:
PARSING TABLE
TABLE:
FAST & FOLLOW
63
AMBIGUITY
SYMBOL FIRST FOLLOW
S  iEtSS’ | a
a , i $ , e
S’  eS |ε
e, ε $ , e
E  b
b t
Non
Terminal
INPUT SYMBOLS
a b e i t $
S Sa
SiEtSS
’
S’
S’  eS
S’ε
S’ε
E E  b
Continue…
 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).
64
STEP: STACK IMPLEMENTATION
65
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
66
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 $.
67
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.
 There are three possibilities.
68
LL(1) Stack
1. X  a  $,
the parser halts and annouces successful completion.
2. X  a  $
the parser pops x off the stack and advances input pointer to next
input symbol
3. If X is a nonterminal, the program consults entry M[x,a] of parsing
table M.
If the entry is a production M[x,a] = {x → uvw } then the
parser replaces x on top of the stack by wvu (with u on top).
As output, the parser just prints the production used:
x → uvw .
69
LL(1) Stack
Example:
Let’s parse the input
string
id+idid
Using the nonrecursive
LL(1) parser
70
Grammar:
E -> TE'
E'-> +TE'|ε
T -> FT'
T' -> *FT'|ε
F -> (E)|id
71
id
E
+ id  id
$
$
stack Parsing
Table
Parsing Table
72
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 )
73
id
E
+ id  id
$
$
stack Parsing
Table
E →T E'
74
id + id  id
$
$
stack Parsing
Table M
T →
T
E'
F T'
Non
Terminal
INPUT SYMBOLS
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)
75
T'
id + id  id
$
$
stack Parsing
Table M
→
E'
F
F id
Non
Terminal
INPUT SYMBOLS
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)
76
T'
id + id  id
$
$
stack Parsing
Table M
E'
id
Non
Terminal
INPUT SYMBOLS
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)
77
T'
+ id  id
$
$
stack Parsing
Table M
→
E'
T' 
id
Non
Terminal
INPUT SYMBOLS
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)
78
+ id  id
$
$
stack Parsing
Table M
→
E'
E' +
id
E'T
Non
Terminal
INPUT SYMBOLS
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)
79
+ id  id
$
$
stack Parsing
Table M
E'
+
id
T
Non
Terminal
INPUT SYMBOLS
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)
80
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
81
LL(1) Parse Tree
 Top-down parsing expands a parse tree from the start
symbol to the leaves
 Always expand the leftmost non-terminal
82
id+idid
83E
T
E'
F
id
T'

E'
T+
F T'
id

* F T'
id 
DONE…
Ll(1) Parser in Compilers
Ad

More Related Content

What's hot (20)

Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
Muhammed Afsal Villan
 
Context free grammar
Context free grammar Context free grammar
Context free grammar
Mohammad Ilyas Malik
 
Data Structures : hashing (1)
Data Structures : hashing (1)Data Structures : hashing (1)
Data Structures : hashing (1)
Home
 
Hashing
HashingHashing
Hashing
Amar Jukuntla
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
Dattatray Gandhmal
 
LL(1) parsing
LL(1) parsingLL(1) parsing
LL(1) parsing
KHYATI PATEL
 
First and follow set
First and follow setFirst and follow set
First and follow set
Dawood Faheem Abbasi
 
Phases of Compiler
Phases of CompilerPhases of Compiler
Phases of Compiler
Tanzeela_Hussain
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
Akshaya Arunan
 
CLR AND LALR PARSER
CLR AND LALR PARSERCLR AND LALR PARSER
CLR AND LALR PARSER
Akila Krishnamoorthy
 
Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler Design
Kuppusamy P
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
Shuvongkor Barman
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
Kuppusamy P
 
Queue ppt
Queue pptQueue ppt
Queue ppt
SouravKumar328
 
Bottom - Up Parsing
Bottom - Up ParsingBottom - Up Parsing
Bottom - Up Parsing
kunj desai
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysis
Richa Sharma
 
Data structure tries
Data structure triesData structure tries
Data structure tries
Md. Naim khan
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
MAHASREEM
 
Dfs presentation
Dfs presentationDfs presentation
Dfs presentation
Alizay Khan
 
Introduction TO Finite Automata
Introduction TO Finite AutomataIntroduction TO Finite Automata
Introduction TO Finite Automata
Ratnakar Mikkili
 
Data Structures : hashing (1)
Data Structures : hashing (1)Data Structures : hashing (1)
Data Structures : hashing (1)
Home
 
Symbol table in compiler Design
Symbol table in compiler DesignSymbol table in compiler Design
Symbol table in compiler Design
Kuppusamy P
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
Shuvongkor Barman
 
Code optimization in compiler design
Code optimization in compiler designCode optimization in compiler design
Code optimization in compiler design
Kuppusamy P
 
Bottom - Up Parsing
Bottom - Up ParsingBottom - Up Parsing
Bottom - Up Parsing
kunj desai
 
Compiler design syntax analysis
Compiler design syntax analysisCompiler design syntax analysis
Compiler design syntax analysis
Richa Sharma
 
Data structure tries
Data structure triesData structure tries
Data structure tries
Md. Naim khan
 
Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design Syntax Analysis in Compiler Design
Syntax Analysis in Compiler Design
MAHASREEM
 
Dfs presentation
Dfs presentationDfs presentation
Dfs presentation
Alizay Khan
 
Introduction TO Finite Automata
Introduction TO Finite AutomataIntroduction TO Finite Automata
Introduction TO Finite Automata
Ratnakar Mikkili
 

Similar to Ll(1) Parser in Compilers (20)

Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
BBDITM LUCKNOW
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLP
kartikaVashisht
 
PARSING.ppt
PARSING.pptPARSING.ppt
PARSING.ppt
ayyankhanna6480086
 
Compiler unit 2&3
Compiler unit 2&3Compiler unit 2&3
Compiler unit 2&3
BBDITM LUCKNOW
 
Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive Parsing
Tanzeela_Hussain
 
Chapter3pptx__2021_12_23_22_52_54.pptx
Chapter3pptx__2021_12_23_22_52_54.pptxChapter3pptx__2021_12_23_22_52_54.pptx
Chapter3pptx__2021_12_23_22_52_54.pptx
DrIsikoIsaac
 
Ch06
Ch06Ch06
Ch06
Hankyo
 
Theory of automata and formal language lab manual
Theory of automata and formal language lab manualTheory of automata and formal language lab manual
Theory of automata and formal language lab manual
Nitesh Dubey
 
lec02-Syntax Analysis and LL(1) (2).pptx
lec02-Syntax Analysis and LL(1) (2).pptxlec02-Syntax Analysis and LL(1) (2).pptx
lec02-Syntax Analysis and LL(1) (2).pptx
ishayujadon
 
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPTCh4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
FutureTechnologies3
 
lec02-Syntax Analysis and LL(1).pdf
lec02-Syntax Analysis and LL(1).pdflec02-Syntax Analysis and LL(1).pdf
lec02-Syntax Analysis and LL(1).pdf
wigewej294
 
07 top-down-parsing
07 top-down-parsing07 top-down-parsing
07 top-down-parsing
Harish Khodke
 
Bottom up parsing.pptx
Bottom up parsing.pptxBottom up parsing.pptx
Bottom up parsing.pptx
RamBhardwaj11
 
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
venkatapranaykumarGa
 
Chapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.pptChapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.ppt
FamiDan
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
ASHOK KUMAR REDDY
 
Lexical 2
Lexical 2Lexical 2
Lexical 2
ASHOK KUMAR REDDY
 
Parsing
ParsingParsing
Parsing
ShrikantSharma86
 
Syntax Analysis_73 pages notes IIIT Manipur.pdf
Syntax Analysis_73 pages notes IIIT Manipur.pdfSyntax Analysis_73 pages notes IIIT Manipur.pdf
Syntax Analysis_73 pages notes IIIT Manipur.pdf
venkyvenky7674886497
 
Compiler Deisgn-Varrious parsing methods
Compiler Deisgn-Varrious parsing methodsCompiler Deisgn-Varrious parsing methods
Compiler Deisgn-Varrious parsing methods
cscprabh
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLP
kartikaVashisht
 
Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive Parsing
Tanzeela_Hussain
 
Chapter3pptx__2021_12_23_22_52_54.pptx
Chapter3pptx__2021_12_23_22_52_54.pptxChapter3pptx__2021_12_23_22_52_54.pptx
Chapter3pptx__2021_12_23_22_52_54.pptx
DrIsikoIsaac
 
Theory of automata and formal language lab manual
Theory of automata and formal language lab manualTheory of automata and formal language lab manual
Theory of automata and formal language lab manual
Nitesh Dubey
 
lec02-Syntax Analysis and LL(1) (2).pptx
lec02-Syntax Analysis and LL(1) (2).pptxlec02-Syntax Analysis and LL(1) (2).pptx
lec02-Syntax Analysis and LL(1) (2).pptx
ishayujadon
 
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPTCh4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
FutureTechnologies3
 
lec02-Syntax Analysis and LL(1).pdf
lec02-Syntax Analysis and LL(1).pdflec02-Syntax Analysis and LL(1).pdf
lec02-Syntax Analysis and LL(1).pdf
wigewej294
 
Bottom up parsing.pptx
Bottom up parsing.pptxBottom up parsing.pptx
Bottom up parsing.pptx
RamBhardwaj11
 
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
5-Introduction to Parsing and Context Free Grammar-09-05-2023.pptx
venkatapranaykumarGa
 
Chapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.pptChapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.ppt
FamiDan
 
Syntax Analysis_73 pages notes IIIT Manipur.pdf
Syntax Analysis_73 pages notes IIIT Manipur.pdfSyntax Analysis_73 pages notes IIIT Manipur.pdf
Syntax Analysis_73 pages notes IIIT Manipur.pdf
venkyvenky7674886497
 
Compiler Deisgn-Varrious parsing methods
Compiler Deisgn-Varrious parsing methodsCompiler Deisgn-Varrious parsing methods
Compiler Deisgn-Varrious parsing methods
cscprabh
 
Ad

More from Mahbubur Rahman (9)

Randomized Algorithm- Advanced Algorithm
Randomized Algorithm- Advanced AlgorithmRandomized Algorithm- Advanced Algorithm
Randomized Algorithm- Advanced Algorithm
Mahbubur Rahman
 
Cloudonomics in Advanced Cloud Computing
Cloudonomics in Advanced Cloud ComputingCloudonomics in Advanced Cloud Computing
Cloudonomics in Advanced Cloud Computing
Mahbubur Rahman
 
Constraint Satisfaction Problem (CSP) : Cryptarithmetic, Graph Coloring, 4- Q...
Constraint Satisfaction Problem (CSP) : Cryptarithmetic, Graph Coloring, 4- Q...Constraint Satisfaction Problem (CSP) : Cryptarithmetic, Graph Coloring, 4- Q...
Constraint Satisfaction Problem (CSP) : Cryptarithmetic, Graph Coloring, 4- Q...
Mahbubur Rahman
 
Geographic Routing in WSN
Geographic Routing in WSNGeographic Routing in WSN
Geographic Routing in WSN
Mahbubur Rahman
 
Streaming Stored Video- Computer Networking
Streaming Stored Video- Computer Networking  Streaming Stored Video- Computer Networking
Streaming Stored Video- Computer Networking
Mahbubur Rahman
 
Random Oracle Model & Hashing - Cryptography & Network Security
Random Oracle Model & Hashing - Cryptography & Network SecurityRandom Oracle Model & Hashing - Cryptography & Network Security
Random Oracle Model & Hashing - Cryptography & Network Security
Mahbubur Rahman
 
Modern Block Cipher- Modern Symmetric-Key Cipher
Modern Block Cipher- Modern Symmetric-Key CipherModern Block Cipher- Modern Symmetric-Key Cipher
Modern Block Cipher- Modern Symmetric-Key Cipher
Mahbubur Rahman
 
Web Server And Database Server
Web Server And Database ServerWeb Server And Database Server
Web Server And Database Server
Mahbubur Rahman
 
LEX & YACC
LEX & YACCLEX & YACC
LEX & YACC
Mahbubur Rahman
 
Randomized Algorithm- Advanced Algorithm
Randomized Algorithm- Advanced AlgorithmRandomized Algorithm- Advanced Algorithm
Randomized Algorithm- Advanced Algorithm
Mahbubur Rahman
 
Cloudonomics in Advanced Cloud Computing
Cloudonomics in Advanced Cloud ComputingCloudonomics in Advanced Cloud Computing
Cloudonomics in Advanced Cloud Computing
Mahbubur Rahman
 
Constraint Satisfaction Problem (CSP) : Cryptarithmetic, Graph Coloring, 4- Q...
Constraint Satisfaction Problem (CSP) : Cryptarithmetic, Graph Coloring, 4- Q...Constraint Satisfaction Problem (CSP) : Cryptarithmetic, Graph Coloring, 4- Q...
Constraint Satisfaction Problem (CSP) : Cryptarithmetic, Graph Coloring, 4- Q...
Mahbubur Rahman
 
Geographic Routing in WSN
Geographic Routing in WSNGeographic Routing in WSN
Geographic Routing in WSN
Mahbubur Rahman
 
Streaming Stored Video- Computer Networking
Streaming Stored Video- Computer Networking  Streaming Stored Video- Computer Networking
Streaming Stored Video- Computer Networking
Mahbubur Rahman
 
Random Oracle Model & Hashing - Cryptography & Network Security
Random Oracle Model & Hashing - Cryptography & Network SecurityRandom Oracle Model & Hashing - Cryptography & Network Security
Random Oracle Model & Hashing - Cryptography & Network Security
Mahbubur Rahman
 
Modern Block Cipher- Modern Symmetric-Key Cipher
Modern Block Cipher- Modern Symmetric-Key CipherModern Block Cipher- Modern Symmetric-Key Cipher
Modern Block Cipher- Modern Symmetric-Key Cipher
Mahbubur Rahman
 
Web Server And Database Server
Web Server And Database ServerWeb Server And Database Server
Web Server And Database Server
Mahbubur Rahman
 
Ad

Recently uploaded (20)

Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales moduleHow To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
Celine George
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and GuestsLDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDM Mia eStudios
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
puzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tensepuzzle Irregular Verbs- Simple Past Tense
puzzle Irregular Verbs- Simple Past Tense
OlgaLeonorTorresSnch
 
Final Evaluation.docx...........................
Final Evaluation.docx...........................Final Evaluation.docx...........................
Final Evaluation.docx...........................
l1bbyburrell
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
CNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscessCNS infections (encephalitis, meningitis & Brain abscess
CNS infections (encephalitis, meningitis & Brain abscess
Mohamed Rizk Khodair
 
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales moduleHow To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
Celine George
 
Ancient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian HistoryAncient Stone Sculptures of India: As a Source of Indian History
Ancient Stone Sculptures of India: As a Source of Indian History
Virag Sontakke
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...Classification of mental disorder in 5th semester bsc. nursing and also used ...
Classification of mental disorder in 5th semester bsc. nursing and also used ...
parmarjuli1412
 
spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)spinal cord disorders (Myelopathies and radiculoapthies)
spinal cord disorders (Myelopathies and radiculoapthies)
Mohamed Rizk Khodair
 
How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18How to Share Accounts Between Companies in Odoo 18
How to Share Accounts Between Companies in Odoo 18
Celine George
 
Origin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theoriesOrigin of Brahmi script: A breaking down of various theories
Origin of Brahmi script: A breaking down of various theories
PrachiSontakke5
 
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptxANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
ANTI-VIRAL DRUGS unit 3 Pharmacology 3.pptx
Mayuri Chavan
 
All About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdfAll About the 990 Unlocking Its Mysteries and Its Power.pdf
All About the 990 Unlocking Its Mysteries and Its Power.pdf
TechSoup
 
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFAMEDICAL BIOLOGY MCQS  BY. DR NASIR MUSTAFA
MEDICAL BIOLOGY MCQS BY. DR NASIR MUSTAFA
Dr. Nasir Mustafa
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and GuestsLDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDMMIA Reiki News Ed3 Vol1 For Team and Guests
LDM Mia eStudios
 

Ll(1) Parser in Compilers

  • 2. MODEL OF COMPILER FRONT END 2 Front End Also called parsing , where generates parse tree Syntax analysis
  • 3. PARSING 3 When the parser starts constructing the parse tree from the start symbol and then tries to transform the start symbol to the input, it is called top-down parsing. Where bottom-up parsing starts with the input symbols and tries to construct the parse tree up to the start symbol.
  • 4. TOP DOWN PERSER 4 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.
  • 5. PREDICTIVE PARSER 5 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.
  • 6. LL(1) PARSER 6  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.
  • 7. 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. (We assume that there is a unique EOF-marker $ at the end of the input.) So look ahead meaning is - looking at input tokens without moving the input cursor. 7
  • 8. PRIME REQUIREMENT OF LL(1)  The grammar must be -  no left factoring  no left recursion  FIRST() & FOLLOW()  Parsing Table  Stack Implementation  Parse Tree 8
  • 10. LEFT FACTORING  A grammar is said to be left factored when it is of the form – A -> αβ1 | αβ2 | αβ3 | …… | αβn | γ  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 | …… | αβn | γ The equivalent left factored grammar will be – A -> αA’ | γ A’ -> β1 | β2 | β3 | …… | βn 10
  • 11. 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 11
  • 13. 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 13
  • 14. Left Recursion Right Recursion For grammar: For grammar: A -> A | β A ->  A| β A A  A A   A A   A β  A β This parse tree generate β  * This parse tree generate  * β 14
  • 15. Right recursion-  A production of grammar is said to have right recursion if the right most variable RHS is 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. 15
  • 16. 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. 16
  • 17. 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 17
  • 18. 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' |  18
  • 19. EXAMPLE OF IMMEDIATE LEFT RECURSION: Consider the left recursive grammar E  E + T | T T  T * F | F F  (E) | id Apply the transformation to E: E  T E' E'  + T E' |  Then apply the transformation to T: T  F T' T'  * F T' |  Now the grammar is E  T E' E'  + T E' |  T  F T' T'  * F T' |  F  (E) | id 19
  • 20. Continue… The case of several immediate left recursive  -productions. Assume that the set of all  -productions has the form A → A 1 | A 2 | · · · | A m | β1 | β2| · · · | βn Represents all the  -productions of the grammar, and no βi begins with A, then we can replace these  -productions by A →β1A′ | β2A′| · · · | βnA′ A′ → 1A′ | 2A′ | · · · | mA′ |  20
  • 21. Example: Consider the left recursive grammar S → SX | SSb| XS | a X → Xb | Sa Apply the transformation to S: S → XSS′ | aS′ S′ → XS′ | SbS′ | ε Apply the transformation to X: X → SaX′ X′ → bX′ | ε Now the grammar is S → XSS′ | aS′ S′ → XS′ | SbS′ | ε X → SaX′ X′ → bX′ | ε 21
  • 22. Example of elimination indirect left recursion: S A A | 0 A S S | 1 Considering the ordering S, A, we get: S A A | 0 A S | 0S | 1 And removing immediate left recursion, we get S A A | 0 A 0S A′ | 1A′ A′  ε | AS A′ 22
  • 24. 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 apply . FOLLOW is used only if the current non terminal can derive ε . 24
  • 25. 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 } 25
  • 26. Rules of FIRST  If a grammar is A → aB ǀ ε then 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,( , ε } 26
  • 27. 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 , $ } 27
  • 28. 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. 28
  • 29. 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)={*, ε }. But when the value is € it follows the parents symbol. So FOLLOW(B)={*,$ } 29
  • 30. Example of FIRST and FOLLOW Symbol FIRST FOLLOW E E’ T T’ F GRAMMAR: E -> TE' E'-> +TE'|ε T -> FT' T' -> *FT'|ε F -> (E)|id 30
  • 31. Example of FIRST and FOLLOW Symbol FIRST FOLLOW E { ( , id } E' T T' F GRAMMAR: E -> TE' E'-> +TE'|ε T -> FT' T' -> *FT'|ε F -> (E)|id 31
  • 32. Example of FIRST and FOLLOW Symbol FIRST FOLLOW E { ( , id } E' { + , ε } T T' F GRAMMAR: E -> TE' E'-> +TE'|ε T -> FT' T' -> *FT'|ε F -> (E)|id 32
  • 33. Example of FIRST and FOLLOW Symbol FIRST FOLLOW E { ( , id } E' { + , ε } T { id , ( } T' F GRAMMAR: E -> TE' E'-> +TE'|ε T -> FT' T' -> *FT'|ε F -> (E)|id 33
  • 34. Example of FIRST and FOLLOW Symbol FIRST FOLLOW E { ( , id } E' { + , ε } T { id , ( } T' { * , ε } F GRAMMAR: E -> TE' E'-> +TE'|ε T -> FT' T' -> *FT'|ε F -> (E)|id 34
  • 35. Example of FIRST and FOLLOW Symbol FIRST FOLLOW E { ( , id } E' { + , ε } T { id , ( } T' { * , ε } F { id , ( } GRAMMAR: E -> TE' E'-> +TE'|ε T -> FT' T' -> *FT'|ε F -> (E)|id 35
  • 36. Example of FIRST and FOLLOW Symbol FIRST FOLLOW E { ( , id } { $ , ) } E' { + , ε } T { id , ( } T' { * , ε } F { id , ( } GRAMMAR: E -> TE' E'-> +TE'|ε T -> FT' T' -> *FT'|ε F -> (E)|id 36
  • 37. Example of FIRST and FOLLOW Symbol FIRST FOLLOW E { ( , id } { $ , ) } E' { + , ε } { $ , ) } T { id , ( } T' { * , ε } F { id , ( } GRAMMAR: E -> TE' E'-> +TE'|ε T -> FT' T' -> *FT'|ε F -> (E)|id 37
  • 38. Example of FIRST and FOLLOW Symbol FIRST FOLLOW E { ( , id } { $ , ) } E' { + , ε } { $ , ) } T { id , ( } { $ , ) ,+ } T' { * , ε } F { id , ( } GRAMMAR: E -> TE' E'-> +TE'|ε T -> FT' T' -> *FT'|ε F -> (E)|id 38
  • 39. Example of FIRST and FOLLOW Symbol FIRST FOLLOW E { ( , id } { $ , ) } E' { + , ε } { $ , ) } T { id , ( } { $ , ) ,+ } T' { * , ε } { $ , ) , + } F { id , ( } GRAMMAR: E -> TE' E'-> +TE'|ε T -> FT' T' -> *FT'|ε F -> (E)|id 39
  • 40. Example of FIRST and FOLLOW Symbol FIRST FOLLOW E { ( , id } { $ , ) } E' { + , ε } { $ , ) } T { id , ( } { $ , ) ,+ } T' { * , ε } { $ , ) , + } F { id , ( } { $ , ) , + , * } GRAMMAR: E -> TE' E'-> +TE'|ε T -> FT' T' -> *FT'|ε F -> (E)|id 40
  • 42. Example of LL(1) grammar E -> TE’ E’ -> +TE’|ε T -> FT’ T’ -> *FT’|ε F -> (E)|id 42
  • 43. TABLE: PARSING TABLE TABLE: FIRST & FOLLOW 43 Production Symbol FOLLOW E -> TE’ { ( , id } { $ , ) } E’ -> +TE’|ε { + , ε } { $ , ) } T -> FT’ { ( , id } { + , $ , ) } T’ -> *FT’|ε { * , ε } { + , $ , ) } F -> (E)|id { ( , id } { *, + , $ , ) } Non Terminal INPUT SYMBOLS id + * ( ) $ E E’ T T’ F
  • 44. TABLE: PARSING TABLE TABLE: FIRST & FOLLOW 44 Production Symbol FOLLOW E -> TE’ { ( , id } { $ , ) } E’ -> +TE’|ε { + , ε } { $ , ) } T -> FT’ { ( , id } { + , $ , ) } T’ -> *FT’|ε { * , ε } { + , $ , ) } F -> (E)|id { ( , id } { *, + , $ , ) } Non Terminal INPUT SYMBOLS id + * ( ) $ E E -> TE’ E’ T T’ F
  • 45. TABLE: PARSING TABLE TABLE: FIRST & FOLLOW 45 Production Symbol FOLLOW E -> TE’ { ( , id } { $ , ) } E’ -> +TE’|ε { + , ε } { $ , ) } T -> FT’ { ( , id } { + , $ , ) } T’ -> *FT’|ε { * , ε } { + , $ , ) } F -> (E)|id { ( , id } { *, + , $ , ) } Non Terminal INPUT SYMBOLS id + * ( ) $ E E -> TE’ E -> TE’ E’ T T’ F
  • 46. TABLE: PARSING TABLE TABLE: FIRST & FOLLOW 46 Production Symbol FOLLOW E -> TE’ { ( , id } { $ , ) } E’ -> +TE’|ε { + , ε } { $ , ) } T -> FT’ { ( , id } { + , $ , ) } T’ -> *FT’|ε { * , ε } { + , $ , ) } F -> (E)|id { ( , id } { *, + , $ , ) } Non Terminal INPUT SYMBOLS id + * ( ) $ E E -> TE’ E -> TE’ E’ E’-> +TE’ T T’ F
  • 47. TABLE: PARSING TABLE TABLE: FIRST & FOLLOW 47 Production Symbol FOLLOW E -> TE’ { ( , id } { $ , ) } E’ -> +TE’|ε { + , ε } { $ , ) } T -> FT’ { ( , id } { + , $ , ) } T’ -> *FT’|ε { * , ε } { + , $ , ) } F -> (E)|id { ( , id } { *, + , $ , ) } Non Terminal INPUT SYMBOLS id + * ( ) $ E E -> TE’ E -> TE’ E’ E’-> +TE’ E’ -> ε E’ -> ε T T’ F
  • 48. TABLE: PARSING TABLE TABLE: FIRST & FOLLOW 48 Production Symbol FOLLOW E -> TE’ { ( , id } { $ , ) } E’ -> +TE’|ε { + , ε } { $ , ) } T -> FT’ { ( , id } { + , $ , ) } T’ -> *FT’|ε { * , ε } { + , $ , ) } F -> (E)|id { ( , id } { *, + , $ , ) } Non Terminal INPUT SYMBOLS id + * ( ) $ E E -> TE’ E -> TE’ E’ E’-> +TE’ E’ -> ε E’ -> ε T T -> FT’ T’ F
  • 49. TABLE: PARSING TABLE TABLE: FIRST & FOLLOW 49 Production Symbol FOLLOW E -> TE’ { ( , id } { $ , ) } E’ -> +TE’|ε { + , ε } { $ , ) } T -> FT’ { ( , id } { + , $ , ) } T’ -> *FT’|ε { * , ε } { + , $ , ) } F -> (E)|id { ( , id } { *, + , $ , ) } Non Terminal INPUT SYMBOLS id + * ( ) $ E E -> TE’ E -> TE’ E’ E’-> +TE’ E’ -> ε E’ -> ε T T -> FT’ T -> FT’ T’ F
  • 50. TABLE: PARSING TABLE TABLE: FIRST & FOLLOW 50 Production Symbol FOLLOW E -> TE’ { ( , id } { $ , ) } E’ -> +TE’|ε { + , ε } { $ , ) } T -> FT’ { ( , id } { + , $ , ) } T’ -> *FT’|ε { * , ε } { + , $ , ) } F -> (E)|id { ( , id } { *, + , $ , ) } Non Terminal INPUT SYMBOLS id + * ( ) $ E E -> TE’ E -> TE’ E’ E’-> +TE’ E’ -> ε E’ -> ε T T -> FT’ T -> FT’ T’ T’ -> *FT’ F
  • 51. TABLE: PARSING TABLE TABLE: FIRST & FOLLOW 51 Production Symbol FOLLOW E -> TE’ { ( , id } { $ , ) } E’ -> +TE’|ε { + , ε } { $ , ) } T -> FT’ { ( , id } { + , $ , ) } T’ -> *FT’|ε { * , ε } { + , $ , ) } F -> (E)|id { ( , id } { *, + , $ , ) } Non Terminal INPUT SYMBOLS id + * ( ) $ E E -> TE’ E -> TE’ E’ E’-> +TE’ E’ -> ε E’ -> ε T T -> FT’ T -> FT’ T’ T’ -> ε T’ -> *FT’ T’ -> ε T’ -> ε F
  • 52. TABLE: PARSING TABLE TABLE: FIRST & FOLLOW 52 Production Symbol FOLLOW E -> TE’ { ( , id } { $ , ) } E’ -> +TE’|ε { + , ε } { $ , ) } T -> FT’ { ( , id } { + , $ , ) } T’ -> *FT’|ε { * , ε } { + , $ , ) } F -> (E)|id { ( , id } { *, + , $ , ) } Non Terminal INPUT SYMBOLS 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
  • 53. TABLE: PARSING TABLE TABLE: FIRST & FOLLOW 53 Production Symbol FOLLOW E -> TE’ { ( , id } { $ , ) } E’ -> +TE’|ε { + , ε } { $ , ) } T -> FT’ { ( , id } { + , $ , ) } T’ -> *FT’|ε { * , ε } { + , $ , ) } F -> (E)|id { ( , id } { *, + , $ , ) } Non Terminal INPUT SYMBOLS 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)
  • 54. Continue… 54 TABLE: PARSING TABLE Non Terminal INPUT SYMBOLS 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)  This grammar is LL(1).  So, the parse tree can be derived from the stack implementation of the given parsing table.
  • 55.  There are grammars which may requite LL(1) parsing.  For e.g. Look at next grammar….. 55 Continue…
  • 56. Continue… GRAMMAR: S  iEtSS’ | a S’  eS |ε E  b SYMBOL FIRST FOLLOW S a , i $ , e S’ e , ε $ , e E b t TABLE: FIRST & FOLLOW 56
  • 57. TABLE: PARSING TABLE TABLE: FAST & FOLLOW 57 SYMBOL FIRST FOLLOW S  iEtSS’ | a a , i $ , e S’  eS |ε e, ε $ , e E  b b t Non Terminal INPUT SYMBOLS a b e i t $ S S’ E
  • 58. TABLE: PARSING TABLE TABLE: FAST & FOLLOW 58 SYMBOL FIRST FOLLOW S  iEtSS’ | a a , i $ , e S’  eS |ε e, ε $ , e E  b b t Non Terminal INPUT SYMBOLS a b e i t $ S Sa S’ E
  • 59. TABLE: PARSING TABLE TABLE: FAST & FOLLOW 59 SYMBOL FIRST FOLLOW S  iEtSS’ | a a , i $ , e S’  eS |ε e, ε $ , e E  b b t Non Terminal INPUT SYMBOLS a b e i t $ S Sa SiEtSS ’ S’ E
  • 60. TABLE: PARSING TABLE TABLE: FAST & FOLLOW 60 SYMBOL FIRST FOLLOW S  iEtSS’ | a a , i $ , e S’  eS |ε e, ε $ , e E  b b t Non Terminal INPUT SYMBOLS a b e i t $ S Sa SiEtSS ’ S’ S’  eS E
  • 61. TABLE: PARSING TABLE TABLE: FAST & FOLLOW 61 SYMBOL FIRST FOLLOW S  iEtSS’ | a a , i $ , e S’  eS |ε e, ε $ , e E  b b t Non Terminal INPUT SYMBOLS a b e i t $ S Sa SiEtSS ’ S’ S’  eS S’ε S’ε E
  • 62. TABLE: PARSING TABLE TABLE: FAST & FOLLOW 62 SYMBOL FIRST FOLLOW S  iEtSS’ | a a , i $ , e S’  eS |ε e, ε $ , e E  b b t Non Terminal INPUT SYMBOLS a b e i t $ S Sa SiEtSS ’ S’ S’  eS S’ε S’ε E E  b
  • 63. TABLE: PARSING TABLE TABLE: FAST & FOLLOW 63 AMBIGUITY SYMBOL FIRST FOLLOW S  iEtSS’ | a a , i $ , e S’  eS |ε e, ε $ , e E  b b t Non Terminal INPUT SYMBOLS a b e i t $ S Sa SiEtSS ’ S’ S’  eS S’ε S’ε E E  b
  • 64. Continue…  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). 64
  • 66. 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 66
  • 67. 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 $. 67
  • 68. 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.  There are three possibilities. 68
  • 69. LL(1) Stack 1. X  a  $, the parser halts and annouces successful completion. 2. X  a  $ the parser pops x off the stack and advances input pointer to next input symbol 3. If X is a nonterminal, the program consults entry M[x,a] of parsing table M. If the entry is a production M[x,a] = {x → uvw } then the parser replaces x on top of the stack by wvu (with u on top). As output, the parser just prints the production used: x → uvw . 69
  • 70. LL(1) Stack Example: Let’s parse the input string id+idid Using the nonrecursive LL(1) parser 70 Grammar: E -> TE' E'-> +TE'|ε T -> FT' T' -> *FT'|ε F -> (E)|id
  • 71. 71 id E + id  id $ $ stack Parsing Table
  • 72. Parsing Table 72 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 )
  • 73. 73 id E + id  id $ $ stack Parsing Table E →T E'
  • 74. 74 id + id  id $ $ stack Parsing Table M T → T E' F T' Non Terminal INPUT SYMBOLS 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)
  • 75. 75 T' id + id  id $ $ stack Parsing Table M → E' F F id Non Terminal INPUT SYMBOLS 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)
  • 76. 76 T' id + id  id $ $ stack Parsing Table M E' id Non Terminal INPUT SYMBOLS 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)
  • 77. 77 T' + id  id $ $ stack Parsing Table M → E' T'  id Non Terminal INPUT SYMBOLS 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)
  • 78. 78 + id  id $ $ stack Parsing Table M → E' E' + id E'T Non Terminal INPUT SYMBOLS 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)
  • 79. 79 + id  id $ $ stack Parsing Table M E' + id T Non Terminal INPUT SYMBOLS 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)
  • 80. 80 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’-> Є
  • 81. STEP: LL(1) PARSE TREE 81
  • 82. LL(1) Parse Tree  Top-down parsing expands a parse tree from the start symbol to the leaves  Always expand the leftmost non-terminal 82

Editor's Notes

  翻译: