尊敬的 微信汇率:1円 ≈ 0.046166 元 支付宝汇率:1円 ≈ 0.046257元 [退出登录]
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

More Related Content

What's hot

LL(1) parsing
LL(1) parsingLL(1) parsing
LL(1) parsing
KHYATI PATEL
 
RECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSINGRECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSING
Jothi Lakshmi
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
Dattatray Gandhmal
 
Depth first search [dfs]
Depth first search [dfs]Depth first search [dfs]
Depth first search [dfs]
DEEPIKA T
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First Search
Kevin Jadiya
 
Compiler design
Compiler designCompiler design
Compiler design
Thakur Ganeshsingh Thakur
 
Single pass assembler
Single pass assemblerSingle pass assembler
Single pass assembler
Bansari Shah
 
Syntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address CodeSyntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address Code
sanchi29
 
Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design
Dr. C.V. Suresh Babu
 
Semaphores
SemaphoresSemaphores
Semaphores
Mohd Arif
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazation
Siva Sathya
 
Problem reduction AND OR GRAPH & AO* algorithm.ppt
Problem reduction AND OR GRAPH & AO* algorithm.pptProblem reduction AND OR GRAPH & AO* algorithm.ppt
Problem reduction AND OR GRAPH & AO* algorithm.ppt
arunsingh660
 
LR(0) PARSER
LR(0) PARSERLR(0) PARSER
Three Address code
Three Address code Three Address code
Three Address code
Pooja Dixit
 
Production System in AI
Production System in AIProduction System in AI
Production System in AI
Bharat Bhushan
 
Cs419 lec10 left recursion and left factoring
Cs419 lec10   left recursion and left factoringCs419 lec10   left recursion and left factoring
Cs419 lec10 left recursion and left factoring
Arab Open University and Cairo University
 
LR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingLR(1) and SLR(1) parsing
LR(1) and SLR(1) parsing
R Islam
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
ASHOK KUMAR REDDY
 
Types of Compilers
Types of CompilersTypes of Compilers
Types of Compilers
Hemant Chetwani
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
Akhil Kaushik
 

What's hot (20)

LL(1) parsing
LL(1) parsingLL(1) parsing
LL(1) parsing
 
RECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSINGRECURSIVE DESCENT PARSING
RECURSIVE DESCENT PARSING
 
Recognition-of-tokens
Recognition-of-tokensRecognition-of-tokens
Recognition-of-tokens
 
Depth first search [dfs]
Depth first search [dfs]Depth first search [dfs]
Depth first search [dfs]
 
Breadth First Search & Depth First Search
Breadth First Search & Depth First SearchBreadth First Search & Depth First Search
Breadth First Search & Depth First Search
 
Compiler design
Compiler designCompiler design
Compiler design
 
Single pass assembler
Single pass assemblerSingle pass assembler
Single pass assembler
 
Syntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address CodeSyntax-Directed Translation into Three Address Code
Syntax-Directed Translation into Three Address Code
 
Introduction to Compiler design
Introduction to Compiler design Introduction to Compiler design
Introduction to Compiler design
 
Semaphores
SemaphoresSemaphores
Semaphores
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazation
 
Problem reduction AND OR GRAPH & AO* algorithm.ppt
Problem reduction AND OR GRAPH & AO* algorithm.pptProblem reduction AND OR GRAPH & AO* algorithm.ppt
Problem reduction AND OR GRAPH & AO* algorithm.ppt
 
LR(0) PARSER
LR(0) PARSERLR(0) PARSER
LR(0) PARSER
 
Three Address code
Three Address code Three Address code
Three Address code
 
Production System in AI
Production System in AIProduction System in AI
Production System in AI
 
Cs419 lec10 left recursion and left factoring
Cs419 lec10   left recursion and left factoringCs419 lec10   left recursion and left factoring
Cs419 lec10 left recursion and left factoring
 
LR(1) and SLR(1) parsing
LR(1) and SLR(1) parsingLR(1) and SLR(1) parsing
LR(1) and SLR(1) parsing
 
Top down parsing
Top down parsingTop down parsing
Top down parsing
 
Types of Compilers
Types of CompilersTypes of Compilers
Types of Compilers
 
Code Optimization
Code OptimizationCode Optimization
Code Optimization
 

Similar to Ll(1) Parser in Compilers

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
 
Parsing
ParsingParsing
Parsing
khush_boo31
 
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
 
Lexical 2
Lexical 2Lexical 2
Parsing
ParsingParsing
compiler-lecture-6nn-14112022-110738am.ppt
compiler-lecture-6nn-14112022-110738am.pptcompiler-lecture-6nn-14112022-110738am.ppt
compiler-lecture-6nn-14112022-110738am.ppt
SheikhMuhammadSaad3
 
Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2Lecture 05 syntax analysis 2
Lecture 05 syntax analysis 2
Iffat Anjum
 
parsing in compiler design presentation .pptx
parsing in compiler design presentation .pptxparsing in compiler design presentation .pptx
parsing in compiler design presentation .pptx
GeetanjaliSingh82
 

Similar to 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
 
Parsing
ParsingParsing
Parsing
 
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPTCh4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
Ch4_topdownparser_ngfjgh_ngjfhgfffdddf.PPT
 
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
 
parsing in compiler design presentation .pptx
parsing in compiler design presentation .pptxparsing in compiler design presentation .pptx
parsing in compiler design presentation .pptx
 

More from 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
 
LEX & YACC
LEX & YACCLEX & YACC
LEX & YACC
Mahbubur Rahman
 

More from 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
 

Recently uploaded

220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx
Kalna College
 
Library news letter Kitengesa Uganda June 2024
Library news letter Kitengesa Uganda June 2024Library news letter Kitengesa Uganda June 2024
Library news letter Kitengesa Uganda June 2024
Friends of African Village Libraries
 
Science-9-Lesson-1-The Bohr Model-NLC.pptx pptx
Science-9-Lesson-1-The Bohr Model-NLC.pptx pptxScience-9-Lesson-1-The Bohr Model-NLC.pptx pptx
Science-9-Lesson-1-The Bohr Model-NLC.pptx pptx
Catherine Dela Cruz
 
managing Behaviour in early childhood education.pptx
managing Behaviour in early childhood education.pptxmanaging Behaviour in early childhood education.pptx
managing Behaviour in early childhood education.pptx
nabaegha
 
Interprofessional Education Platform Introduction.pdf
Interprofessional Education Platform Introduction.pdfInterprofessional Education Platform Introduction.pdf
Interprofessional Education Platform Introduction.pdf
Ben Aldrich
 
220711130095 Tanu Pandey message currency, communication speed & control EPC ...
220711130095 Tanu Pandey message currency, communication speed & control EPC ...220711130095 Tanu Pandey message currency, communication speed & control EPC ...
220711130095 Tanu Pandey message currency, communication speed & control EPC ...
Kalna College
 
220711130082 Srabanti Bag Internet Resources For Natural Science
220711130082 Srabanti Bag Internet Resources For Natural Science220711130082 Srabanti Bag Internet Resources For Natural Science
220711130082 Srabanti Bag Internet Resources For Natural Science
Kalna College
 
The basics of sentences session 8pptx.pptx
The basics of sentences session 8pptx.pptxThe basics of sentences session 8pptx.pptx
The basics of sentences session 8pptx.pptx
heathfieldcps1
 
Erasmus + DISSEMINATION ACTIVITIES Croatia
Erasmus + DISSEMINATION ACTIVITIES CroatiaErasmus + DISSEMINATION ACTIVITIES Croatia
Erasmus + DISSEMINATION ACTIVITIES Croatia
whatchangedhowreflec
 
Decolonizing Universal Design for Learning
Decolonizing Universal Design for LearningDecolonizing Universal Design for Learning
Decolonizing Universal Design for Learning
Frederic Fovet
 
Creating Images and Videos through AI.pptx
Creating Images and Videos through AI.pptxCreating Images and Videos through AI.pptx
Creating Images and Videos through AI.pptx
Forum of Blended Learning
 
The Science of Learning: implications for modern teaching
The Science of Learning: implications for modern teachingThe Science of Learning: implications for modern teaching
The Science of Learning: implications for modern teaching
Derek Wenmoth
 
BỘ BÀI TẬP TEST THEO UNIT - FORM 2025 - TIẾNG ANH 12 GLOBAL SUCCESS - KÌ 1 (B...
BỘ BÀI TẬP TEST THEO UNIT - FORM 2025 - TIẾNG ANH 12 GLOBAL SUCCESS - KÌ 1 (B...BỘ BÀI TẬP TEST THEO UNIT - FORM 2025 - TIẾNG ANH 12 GLOBAL SUCCESS - KÌ 1 (B...
BỘ BÀI TẬP TEST THEO UNIT - FORM 2025 - TIẾNG ANH 12 GLOBAL SUCCESS - KÌ 1 (B...
Nguyen Thanh Tu Collection
 
INTRODUCTION TO HOSPITALS & AND ITS ORGANIZATION
INTRODUCTION TO HOSPITALS & AND ITS ORGANIZATION INTRODUCTION TO HOSPITALS & AND ITS ORGANIZATION
INTRODUCTION TO HOSPITALS & AND ITS ORGANIZATION
ShwetaGawande8
 
bryophytes.pptx bsc botany honours second semester
bryophytes.pptx bsc botany honours  second semesterbryophytes.pptx bsc botany honours  second semester
bryophytes.pptx bsc botany honours second semester
Sarojini38
 
220711130100 udita Chakraborty Aims and objectives of national policy on inf...
220711130100 udita Chakraborty  Aims and objectives of national policy on inf...220711130100 udita Chakraborty  Aims and objectives of national policy on inf...
220711130100 udita Chakraborty Aims and objectives of national policy on inf...
Kalna College
 
Creativity for Innovation and Speechmaking
Creativity for Innovation and SpeechmakingCreativity for Innovation and Speechmaking
Creativity for Innovation and Speechmaking
MattVassar1
 
Diversity Quiz Prelims by Quiz Club, IIT Kanpur
Diversity Quiz Prelims by Quiz Club, IIT KanpurDiversity Quiz Prelims by Quiz Club, IIT Kanpur
Diversity Quiz Prelims by Quiz Club, IIT Kanpur
Quiz Club IIT Kanpur
 
8+8+8 Rule Of Time Management For Better Productivity
8+8+8 Rule Of Time Management For Better Productivity8+8+8 Rule Of Time Management For Better Productivity
8+8+8 Rule Of Time Management For Better Productivity
RuchiRathor2
 
IoT (Internet of Things) introduction Notes.pdf
IoT (Internet of Things) introduction Notes.pdfIoT (Internet of Things) introduction Notes.pdf
IoT (Internet of Things) introduction Notes.pdf
roshanranjit222
 

Recently uploaded (20)

220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx220711130088 Sumi Basak Virtual University EPC 3.pptx
220711130088 Sumi Basak Virtual University EPC 3.pptx
 
Library news letter Kitengesa Uganda June 2024
Library news letter Kitengesa Uganda June 2024Library news letter Kitengesa Uganda June 2024
Library news letter Kitengesa Uganda June 2024
 
Science-9-Lesson-1-The Bohr Model-NLC.pptx pptx
Science-9-Lesson-1-The Bohr Model-NLC.pptx pptxScience-9-Lesson-1-The Bohr Model-NLC.pptx pptx
Science-9-Lesson-1-The Bohr Model-NLC.pptx pptx
 
managing Behaviour in early childhood education.pptx
managing Behaviour in early childhood education.pptxmanaging Behaviour in early childhood education.pptx
managing Behaviour in early childhood education.pptx
 
Interprofessional Education Platform Introduction.pdf
Interprofessional Education Platform Introduction.pdfInterprofessional Education Platform Introduction.pdf
Interprofessional Education Platform Introduction.pdf
 
220711130095 Tanu Pandey message currency, communication speed & control EPC ...
220711130095 Tanu Pandey message currency, communication speed & control EPC ...220711130095 Tanu Pandey message currency, communication speed & control EPC ...
220711130095 Tanu Pandey message currency, communication speed & control EPC ...
 
220711130082 Srabanti Bag Internet Resources For Natural Science
220711130082 Srabanti Bag Internet Resources For Natural Science220711130082 Srabanti Bag Internet Resources For Natural Science
220711130082 Srabanti Bag Internet Resources For Natural Science
 
The basics of sentences session 8pptx.pptx
The basics of sentences session 8pptx.pptxThe basics of sentences session 8pptx.pptx
The basics of sentences session 8pptx.pptx
 
Erasmus + DISSEMINATION ACTIVITIES Croatia
Erasmus + DISSEMINATION ACTIVITIES CroatiaErasmus + DISSEMINATION ACTIVITIES Croatia
Erasmus + DISSEMINATION ACTIVITIES Croatia
 
Decolonizing Universal Design for Learning
Decolonizing Universal Design for LearningDecolonizing Universal Design for Learning
Decolonizing Universal Design for Learning
 
Creating Images and Videos through AI.pptx
Creating Images and Videos through AI.pptxCreating Images and Videos through AI.pptx
Creating Images and Videos through AI.pptx
 
The Science of Learning: implications for modern teaching
The Science of Learning: implications for modern teachingThe Science of Learning: implications for modern teaching
The Science of Learning: implications for modern teaching
 
BỘ BÀI TẬP TEST THEO UNIT - FORM 2025 - TIẾNG ANH 12 GLOBAL SUCCESS - KÌ 1 (B...
BỘ BÀI TẬP TEST THEO UNIT - FORM 2025 - TIẾNG ANH 12 GLOBAL SUCCESS - KÌ 1 (B...BỘ BÀI TẬP TEST THEO UNIT - FORM 2025 - TIẾNG ANH 12 GLOBAL SUCCESS - KÌ 1 (B...
BỘ BÀI TẬP TEST THEO UNIT - FORM 2025 - TIẾNG ANH 12 GLOBAL SUCCESS - KÌ 1 (B...
 
INTRODUCTION TO HOSPITALS & AND ITS ORGANIZATION
INTRODUCTION TO HOSPITALS & AND ITS ORGANIZATION INTRODUCTION TO HOSPITALS & AND ITS ORGANIZATION
INTRODUCTION TO HOSPITALS & AND ITS ORGANIZATION
 
bryophytes.pptx bsc botany honours second semester
bryophytes.pptx bsc botany honours  second semesterbryophytes.pptx bsc botany honours  second semester
bryophytes.pptx bsc botany honours second semester
 
220711130100 udita Chakraborty Aims and objectives of national policy on inf...
220711130100 udita Chakraborty  Aims and objectives of national policy on inf...220711130100 udita Chakraborty  Aims and objectives of national policy on inf...
220711130100 udita Chakraborty Aims and objectives of national policy on inf...
 
Creativity for Innovation and Speechmaking
Creativity for Innovation and SpeechmakingCreativity for Innovation and Speechmaking
Creativity for Innovation and Speechmaking
 
Diversity Quiz Prelims by Quiz Club, IIT Kanpur
Diversity Quiz Prelims by Quiz Club, IIT KanpurDiversity Quiz Prelims by Quiz Club, IIT Kanpur
Diversity Quiz Prelims by Quiz Club, IIT Kanpur
 
8+8+8 Rule Of Time Management For Better Productivity
8+8+8 Rule Of Time Management For Better Productivity8+8+8 Rule Of Time Management For Better Productivity
8+8+8 Rule Of Time Management For Better Productivity
 
IoT (Internet of Things) introduction Notes.pdf
IoT (Internet of Things) introduction Notes.pdfIoT (Internet of Things) introduction Notes.pdf
IoT (Internet of Things) introduction Notes.pdf
 

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

  1. 74
  翻译: