SlideShare una empresa de Scribd logo
1 de 84
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

Más contenido relacionado

La actualidad más candente

Chomsky classification of Language
Chomsky classification of LanguageChomsky classification of Language
Chomsky classification of LanguageDipankar Boruah
 
State Space Representation and Search
State Space Representation and SearchState Space Representation and Search
State Space Representation and SearchHitesh Mohapatra
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notationsNikhil Sharma
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translationAkshaya Arunan
 
Symbol table management and error handling in compiler design
Symbol table management and error handling in compiler designSymbol table management and error handling in compiler design
Symbol table management and error handling in compiler designSwati Chauhan
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structureAbrish06
 
Type checking in compiler design
Type checking in compiler designType checking in compiler design
Type checking in compiler designSudip Singh
 
Algorithm and pseudocode conventions
Algorithm and pseudocode conventionsAlgorithm and pseudocode conventions
Algorithm and pseudocode conventionssaranyatdr
 
sum of subset problem using Backtracking
sum of subset problem using Backtrackingsum of subset problem using Backtracking
sum of subset problem using BacktrackingAbhishek Singh
 
Push Down Automata (PDA) | TOC (Theory of Computation) | NPDA | DPDA
Push Down Automata (PDA) | TOC  (Theory of Computation) | NPDA | DPDAPush Down Automata (PDA) | TOC  (Theory of Computation) | NPDA | DPDA
Push Down Automata (PDA) | TOC (Theory of Computation) | NPDA | DPDAAshish Duggal
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSGayathri Gaayu
 
Data structures question paper anna university
Data structures question paper anna universityData structures question paper anna university
Data structures question paper anna universitysangeethajames07
 

La actualidad más candente (20)

Chomsky classification of Language
Chomsky classification of LanguageChomsky classification of Language
Chomsky classification of Language
 
State Space Representation and Search
State Space Representation and SearchState Space Representation and Search
State Space Representation and Search
 
Asymptotic notations
Asymptotic notationsAsymptotic notations
Asymptotic notations
 
Syntax directed translation
Syntax directed translationSyntax directed translation
Syntax directed translation
 
Symbol table management and error handling in compiler design
Symbol table management and error handling in compiler designSymbol table management and error handling in compiler design
Symbol table management and error handling in compiler design
 
Bottom up parser
Bottom up parserBottom up parser
Bottom up parser
 
Parsing
ParsingParsing
Parsing
 
String matching algorithms
String matching algorithmsString matching algorithms
String matching algorithms
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Np cooks theorem
Np cooks theoremNp cooks theorem
Np cooks theorem
 
Graph in data structure
Graph in data structureGraph in data structure
Graph in data structure
 
Type checking in compiler design
Type checking in compiler designType checking in compiler design
Type checking in compiler design
 
Algorithm and pseudocode conventions
Algorithm and pseudocode conventionsAlgorithm and pseudocode conventions
Algorithm and pseudocode conventions
 
sum of subset problem using Backtracking
sum of subset problem using Backtrackingsum of subset problem using Backtracking
sum of subset problem using Backtracking
 
Lexical analysis - Compiler Design
Lexical analysis - Compiler DesignLexical analysis - Compiler Design
Lexical analysis - Compiler Design
 
System calls
System callsSystem calls
System calls
 
Push Down Automata (PDA) | TOC (Theory of Computation) | NPDA | DPDA
Push Down Automata (PDA) | TOC  (Theory of Computation) | NPDA | DPDAPush Down Automata (PDA) | TOC  (Theory of Computation) | NPDA | DPDA
Push Down Automata (PDA) | TOC (Theory of Computation) | NPDA | DPDA
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
 
Data structures question paper anna university
Data structures question paper anna universityData structures question paper anna university
Data structures question paper anna university
 

Similar a Ll(1) Parser in Compilers

Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLPkartikaVashisht
 
Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTanzeela_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.pptxDrIsikoIsaac
 
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 manualNitesh Dubey
 
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).pdfwigewej294
 
Bottom up parsing.pptx
Bottom up parsing.pptxBottom up parsing.pptx
Bottom up parsing.pptxRamBhardwaj11
 
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.pptxvenkatapranaykumarGa
 
Chapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.pptChapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.pptFamiDan
 
compiler-lecture-6nn-14112022-110738am.ppt
compiler-lecture-6nn-14112022-110738am.pptcompiler-lecture-6nn-14112022-110738am.ppt
compiler-lecture-6nn-14112022-110738am.pptSheikhMuhammadSaad3
 
Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Iffat Anjum
 
LR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingLR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingR Islam
 
parsing in compiler design presentation .pptx
parsing in compiler design presentation .pptxparsing in compiler design presentation .pptx
parsing in compiler design presentation .pptxGeetanjaliSingh82
 

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

Cd2 [autosaved]
Cd2 [autosaved]Cd2 [autosaved]
Cd2 [autosaved]
 
Syntactic analysis in NLP
Syntactic analysis in NLPSyntactic analysis in NLP
Syntactic analysis in NLP
 
PARSING.ppt
PARSING.pptPARSING.ppt
PARSING.ppt
 
Compiler unit 2&3
Compiler unit 2&3Compiler unit 2&3
Compiler unit 2&3
 
Top Down Parsing, Predictive Parsing
Top Down Parsing, Predictive ParsingTop Down Parsing, Predictive Parsing
Top Down Parsing, Predictive Parsing
 
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
 
Ch06
Ch06Ch06
Ch06
 
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
 
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
 
07 top-down-parsing
07 top-down-parsing07 top-down-parsing
07 top-down-parsing
 
Bottom up parsing.pptx
Bottom up parsing.pptxBottom up parsing.pptx
Bottom up parsing.pptx
 
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
 
Chapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.pptChapter 3 -Syntax Analyzer.ppt
Chapter 3 -Syntax Analyzer.ppt
 
Lexical 2
Lexical 2Lexical 2
Lexical 2
 
Parsing
ParsingParsing
Parsing
 
compiler-lecture-6nn-14112022-110738am.ppt
compiler-lecture-6nn-14112022-110738am.pptcompiler-lecture-6nn-14112022-110738am.ppt
compiler-lecture-6nn-14112022-110738am.ppt
 
Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2
 
LR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingLR(1) and SLR(1) parsing
LR(1) and SLR(1) parsing
 
parsing in compiler design presentation .pptx
parsing in compiler design presentation .pptxparsing in compiler design presentation .pptx
parsing in compiler design presentation .pptx
 
Parsing LL(1), SLR, LR(1)
Parsing LL(1), SLR, LR(1)Parsing LL(1), SLR, LR(1)
Parsing LL(1), SLR, LR(1)
 

Más de Mahbubur Rahman

Randomized Algorithm- Advanced Algorithm
Randomized Algorithm- Advanced AlgorithmRandomized Algorithm- Advanced Algorithm
Randomized Algorithm- Advanced AlgorithmMahbubur Rahman
 
Cloudonomics in Advanced Cloud Computing
Cloudonomics in Advanced Cloud ComputingCloudonomics in Advanced Cloud Computing
Cloudonomics in Advanced Cloud ComputingMahbubur 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 WSNMahbubur 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 SecurityMahbubur 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 CipherMahbubur Rahman
 
Web Server And Database Server
Web Server And Database ServerWeb Server And Database Server
Web Server And Database ServerMahbubur Rahman
 

Más de Mahbubur Rahman (9)

Randomized Algorithm- Advanced Algorithm
Randomized Algorithm- Advanced AlgorithmRandomized Algorithm- Advanced Algorithm
Randomized Algorithm- Advanced Algorithm
 
Cloudonomics in Advanced Cloud Computing
Cloudonomics in Advanced Cloud ComputingCloudonomics in Advanced Cloud Computing
Cloudonomics in Advanced Cloud Computing
 
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...
 
Geographic Routing in WSN
Geographic Routing in WSNGeographic Routing in WSN
Geographic Routing in WSN
 
Streaming Stored Video- Computer Networking
Streaming Stored Video- Computer Networking  Streaming Stored Video- Computer Networking
Streaming Stored Video- Computer Networking
 
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
 
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
 
Web Server And Database Server
Web Server And Database ServerWeb Server And Database Server
Web Server And Database Server
 
LEX & YACC
LEX & YACCLEX & YACC
LEX & YACC
 

Último

ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...JhezDiaz1
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Celine George
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptxmary850239
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)lakshayb543
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 

Último (20)

ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptxFINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
FINALS_OF_LEFT_ON_C'N_EL_DORADO_2024.pptx
 
Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17Field Attribute Index Feature in Odoo 17
Field Attribute Index Feature in Odoo 17
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptxLEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
LEFT_ON_C'N_ PRELIMS_EL_DORADO_2024.pptx
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx4.18.24 Movement Legacies, Reflection, and Review.pptx
4.18.24 Movement Legacies, Reflection, and Review.pptx
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptxYOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
YOUVE GOT EMAIL_FINALS_EL_DORADO_2024.pptx
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
Visit to a blind student's school🧑‍🦯🧑‍🦯(community medicine)
 
Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 

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

Notas del editor

  1. 74