You are on page 1of 56

Nonrecursive Predictive

Parsing
 It is possible to build a
nonrecursive predictive
parser
 This is done by maintaining
an explicit stack
1
Nonrecursive Predictive
Parsing
 It is possible to build a
nonrecursive predictive
parser
 This is done by maintaining
an explicit stack
2
Table-driven Parsers
 The nonrecursive LL(1)
parser looks up the
production to apply by
looking up a parsing table

3
Table-driven Parsers
LL(1) table:
 One dimension for current
non-terminal to expand
 One dimension for next token
 Table entry contains one
production

4
Table-driven Parsers
LL(1) table:
 One dimension for current
non-terminal to expand
 One dimension for next token
 Table entry contains one
production
5
Table-driven Parsers
LL(1) table:
 One dimension for current
non-terminal to expand
 One dimension for next token
 Table entry contains one
production
6
Consider the expression grammar
1 E → T E'
2 E' → + T E'
3 | 
4 T → F T'
5 T' → * F T'
6 | 
7 F → (E )
8 | id
7
Predictive Parsing Table
id + * ( ) $
E E →TE' E →TE'

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


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

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


F F → id F →(E )

Rows for for


Columns current
next non-terminal
token to expand
8
Predictive Parsing Table
id + * ( ) $
E E →TE' E →TE'

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


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

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


F F → id F →(E )

Table entries are errors


Blank productions
9
Predictive Parsers
 The predictive parser uses
an explicit stack to keep
track of pending non-
terminals
 It can thus be implemented
without recursion.
10
Predictive Parsers
 The predictive parser uses
an explicit stack to keep
track of pending non-
terminals
 It can thus be implemented
without recursion.
11
Predictive Parsers
a + b $ input
stack
X Predictive
Y output
parser
Z
$
Parsing table M
12
LL(1) Parsing Algorithm
 The input buffer contains
the string to be parsed; $ is
the end-of-input marker
 The stack contains a
sequence of grammar
symbols
13
LL(1) Parsing Algorithm
 Initially, the stack contains
the start symbol of the
grammar on the top of $.

14
LL(1) Parsing Algorithm

The parser is controlled by a


program that behaves as
follows:

15
LL(1) Parsing Algorithm
 The program considers X,
the symbol on top of the
stack, and a, the current
input symbol.

16
LL(1) Parsing Algorithm
 These two symbols, X and a
determine the action of the
parser.
 There are three possibilities.

17
LL(1) Parsing Algorithm
 These two symbols, X and a
determine the action of the
parser.
 There are three possibilities.

18
LL(1) Parsing Algorithm
1. X  a  $,
the parser halts and
annouces successful
completion.

19
LL(1) Parsing Algorithm
2. X  a  $
the parser pops X off the
stack and advances input
pointer to next input
symbol

20
LL(1) Parsing Algorithm
3. If X is a nonterminal, the
program consults entry
M[X,a] of parsing table M.

21
LL(1) Parsing Algorithm
If the entry is a production
M[X,a] = {X → UVW }
the parser replaces X on
top of the stack by WVU
(with U on top).

22
LL(1) Parsing Algorithm
As output, the parser just
prints the production used:
X → UVW
However, any other code
could be executed here.

23
LL(1) Parsing Algorithm

If M[X,a] =error, the parser


calls an error recovery
routine

24
LL(1) Parsing Algorithm
Example:
Let’s parse the input string
id+idid
using the nonrecursive
LL(1) parser
25
id + id  id $

E
$
stack Parsing
Table M
26
Predictive Parsing Table
id + * ( ) $
E E →TE' E →TE'

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


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

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

F F → id F →(E )

27
id + id  id $

E
$
E →T E'
stack Parsing
Table M
28
id + id  id $

T
E'
$
→ F T'
TParsing
stack
Table M
29
id + id  id $

F
T'
E'
$
→ id
FParsing
stack
Table M
30
id + id  id $

id
T'
E'
$
stack Parsing
Table M
31
+ id  id $

T'
E'
$
→
T'Parsing
stack
Table M
32
+ id  id $

E'
$
→ +T E'
E'Parsing
stack
Table M
33
+ id  id $

+
T
E'
$
stack Parsing
Table M
34
Stack Input Ouput
$E id+idid$
$E' T id+idid$ E →TE'
$E' T' F id+idid$ T →FT'
$E'T' id id+idid$ F → id
$E' T' +idid$
$E' +idid$ T' →
$E' T + +idid$ E' → +TE'
35
Stack Input Ouput
$E' T idid$
$E' T' F idid$ T →FT'
$E' T' id idid$ F → id
$E' T' id$
$E' T' F  id$ T → FT'
$E' T' F id$
$E'T' id id$ F → id
36
Stack Input Ouput
$E' T' $
$E' $ T' →
$ $ E' →

37
LL(1) Parsing Algorithm
 Note that productions output
are tracing out a lefmost
derivation
 The grammar symbols on
the stack make up left-
sentential forms
38
LL(1) Parsing Algorithm
 Note that productions output
are tracing out a lefmost
derivation
 The grammar symbols on
the stack make up left-
sentential forms
39
LL(1) Table Construction
 Top-down parsing expands
a parse tree from the start
symbol to the leaves
 Always expand the leftmost
non-terminal

40
LL(1) Table Construction
 Top-down parsing expands
a parse tree from the start
symbol to the leaves
 Always expand the leftmost
non-terminal

41
id+idid
E

T E'

F T' + T E'

id  and so on ....
42
E

T E'

F T' + T E'

id 

The leaves at any point form a string


A 
43
E

T E'

F T' + T E'

id 

 only contains terminals

44
E

T E'
id + id  id F T' E'
+ T
 b 
id 

input
The string
prefix is b
matches
Next token is b
45
LL(1) Table Construction
 Consider the state
S → A
with b the next token and we
are trying to match b
 There are two possibilities

46
LL(1) Table Construction
 Consider the state
S → A
with b the next token and we
are trying to match b
 There are two possibilities

47
LL(1) Table Construction
1. b belongs to an expansion
of A
Any A →  can be used if b
can start a string derived
from 

48
LL(1) Table Construction
1. b belongs to an expansion
of A
Any A →  can be used if b
can start a string derived
from 

49
LL(1) Table Construction

In this case we say that


b  FIRST()

50
LL(1) Table Construction
2. b does not belong to an
expansion of A
Expansion of A is empty, i.e.,

A →  and b belongs an
expansion of , e.g., b
51
LL(1) Table Construction
2. b does not belong to an
expansion of A
Expansion of A is empty, i.e.,

A →  and b belongs an
expansion of , e.g., b
52
LL(1) Table Construction
which means that b can
appear after A in a
derivation of the form
S → Ab

53
LL(1) Table Construction

We say that
b  FOLLOW(A)

54
LL(1) Table Construction
Any A →  can be used if 
expands to 
We say that   FIRST(A) in
this case

55
Computing FIRST Sets
Definition
FIRST(X) =
{ b | X → ba }

{  | X → }

56

You might also like