0% found this document useful (0 votes)
29 views31 pages

Top-Down Parsing (Non Recursive Predictive)

The document discusses nonrecursive predictive parsing, which utilizes an explicit stack and a parsing table to determine the appropriate production for nonterminals. It explains the components of the parser, including the input buffer, stack, and output stream, as well as the roles of FIRST and FOLLOW functions in constructing the predictive parsing table. Additionally, it covers error recovery techniques in predictive parsing, emphasizing the importance of synchronizing tokens and various strategies for handling parsing errors.

Uploaded by

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

Top-Down Parsing (Non Recursive Predictive)

The document discusses nonrecursive predictive parsing, which utilizes an explicit stack and a parsing table to determine the appropriate production for nonterminals. It explains the components of the parser, including the input buffer, stack, and output stream, as well as the roles of FIRST and FOLLOW functions in constructing the predictive parsing table. Additionally, it covers error recovery techniques in predictive parsing, emphasizing the importance of synchronizing tokens and various strategies for handling parsing errors.

Uploaded by

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

Parimal Kumar Giri

Nonrecursive Predictive Parsing


 It is possible to build a nonrecursive predictive parser
by maintaining a stack explicitly, rather than via
recursive calls.
 The key problem during predictive parsing is that of
determining the production to be applied for a
nonterminal. The nonrecursive parser looks up the
production to be applied in a parsing table.

PGiri 2
Nonrecursive Predictive Parsing(Cont.)

PGiri 3
Nonrecursive Predictive
Parsing(Cont.)
 The parser has an input buffer, a stack, a parsing
table, and an output stream.
 The input buffer contains the strings to be parsed
followed by $, a symbol used to indicate the end of
the input string.
 The stack contains a sequence of grammar symbol
with $ on the bottom, indicating the bottom of the
stack, Initially, the stack contains the start symbol
S of the grammar on top of $.

PGiri 4
Nonrecursive Predictive
Parsing(Cont.)
 The output stream show the derivation steps for the
grammar to produce the input string.
 The parser table is a two-dimensional array M[A, a] to
show the stack action for a nonterminal A in the top of
stack to meet a terminal a or the symbol $.

PGiri 5
Predictive Parsing Algorithm
 Input. A string w and a parsing table M for G
 Output. A leftmost derivation of w, if w L(G)
 Method.
 Put $S on stack where S is the start symbol of G
 Put w$ in the input buffer
 Execute the predictive parsing program (Fig. 4.14)

PGiri 6
Predictive Parsing Program

PGiri 7
Example
 Consider non-left-recursive grammar for arithmetic
expression
E TE’
E’ + TE’ |
T FT’
T’ * FT’ |
F (E) | id

PGiri 8
Example (parsing table M)

PGiri 9
Example (Stack Moves)

PGiri 10
FIRST and FOLLOW
 The construction of a predictive parser is aided by
FIRST and FOLLOW functions.
 These functions help us to construction the predictive
parser table.
 FOLLOW function can also be used as synchronizing
tokens during panic-mode error recovery.

PGiri 11
FIRST function
 If is a string of a grammar symbols, FIRST( ) be the
set of terminals that begin the strings derived from .
 If * , FIRST( ).

PGiri 12
FIRST Sets
 Compute FIRST(X) for all grammar symbols X, by
the following rules until no terminals or can be
added to any FIRST(X)
1. If X is a terminal, then FIRST(X)={X}.
2. If X , FIRST(X)
3. If X Y1Y2…Yk, then a FIRST(X) where
a FIRST(Yi) and FIRST(Y1),
FIRST(Y2) ,…,FIRST(Yi-1), Y1Y2…Yi-1 *

PGiri 13
FIRST sets (cont.)
3. If FIRST(Yj), for all j=1,2,..,k, then FIRST(X).
 Everything in FIRST(Y1) is also in FIRST(X). If Y1
does not derive , nothing more added to
FIRST(X). Otherwise, we add FIRST(Y2), and so
on.
 For FIRST(X1X2…Xn),
FIRST(X1) FIRST(X1X2…Xn),
FIRST(X2) FIRST(X1X2…Xn), if FIRST(X1) and
so on. FIRST(X1X2…Xn) if FIRST(Xi) for all i.

PGiri 14
FOLLOW function
 Define FOLLOW(A), for nonterminal A, to be the set
of terminal a that can appear immediately to right of A.
 S * Aa , a FOLLOW(A).
 If A can be the rightmost symbol in some sentential
form, then $ FOLLOW(A).

PGiri 15
Computation of FOLLOW Sets
 Compute FOLLOW(A), for nonterminal A, by the
following rules until nothing can be added to any
FOLLOW set.
1. If S is a start symbol, $ FOLLOW(S).
2. If A B , FIRST( ) FOLLOW(B).
3. If A B then FOLLOW(A) FOLLOW(B).
(FOLLOW(B) may not FOLLOW(A))
4. If A B and FIRST( ),then
FOLLOW(B)=FIRST( )-{ }  FOLLOW(A).

PGiri 16
Example
E TE’
E’ + TE’ |
T FT’
T’ * FT’ |
F (E) | id
 FIRST(E)=FIRST(T)=FIRST(F)={(, id}
 FIRST(E’)={+, }
 FIRST(T’)={*, }
 FOLLOW(E)=FOLLOW(E’)={ ), $}
 FOLLOW(T)=FOLLOW(T’)={+, ), $}
 FOLLOW(F)={+, *, ), $}

PGiri 17
Construction of Predictive Parsing Table
 Suppose A , a FIRST( ). Then, the parser will
expand A by when the current input symbol is a.
 If A , = or * , then the parser will expand
A by when the input symbol is in FOLLOW(A) or
if $ on the input has been reached and
$ FOLLOW(A).

PGiri 18
Construction of Predictive Parsing Table
(Algorithm)
 Input. Grammar G.
 Output. Parsing table M.
 Method.
1. For each production A , do steps 2 and 3.
2. For each terminal a FIRST( ), add A to
M[A, a]
3. If FIRST( ), add A to M[A, b] for each
terminal b FOLLOW(A). (b is not )
If FIRST( ) and $ FOLLOW(A), add A
to M[A, $].
4. Make each undefined entry of M be error.

PGiri 19
Example

 A grammar whose parsing table has no multiply-defined entries is


said to be LL(1).
 The first “L” means scanning the input form left to right.
 The second “L”, means a leftmost derivation.
 And, “1” means using one input symbol of lookahead at each step.

PGiri 21
Example (multiple entry)
S iEtSS’ | a
S’ eS | FIRST(S)={i,a}, FIRST(S’)={e, }
E b FOLLOW(S)={e,$}, FOLLOW(S’}={e,$}

PGiri 22
LL(1) Properties
 No ambiguous or left-recursive grammar can be
LL(1).
 A grammar G is LL(1) if and only if
A | , the following conditions hold:
1. FIRST( ) FIRST( )= .
2. At most one of * or * .
3. If * , then FIRST( ) FOLLOW(A) )= .
 If-then-else statement violates the condition 3,
so not LL(1).

PGiri 23
Error Recovery in Predictive Parsing
In non-recursive predictive parsing, an error is
detected in one of the following two situations:
1. When the terminal on top of the stack does not
match the next input symbol.
2. When non-terminal A is on top of the stack, a is
the next input symbol, and the parsing table
entry M[ A, a] is empty.

PGiri 24
Error Recovery in Predictive Parsing (cont.)
 Panic-mode error recovery is based on the
idea of skipping symbols on the input until
a token in a selected set of synchronizing
tokens appears.
 Its effectiveness depends on the choice on
the synchronizing set.
 Some heuristics are as follows.

PGiri 25
Error Recovery in Predictive Parsing (cont.)
1. We place all symbols in FOLLOW(A) into the
synchronizing set for nonterminal A. If we skip
tokens until an element of FOLLOW(A) is seen
and pop A from the stack, it is likely that
parsing can continue.
2. There is hierarchical structure on constructs in
a language; e.g., expressions within blocks, and
so on. We can add to the synchronizing set of a
lower construct the symbols that begin higher
constructs.

PGiri 26
Error Recovery in Predictive Parsing (cont.)
3. If we add symbols in FIRST(A) to the
synchronizing set of nonterminal A, then it may
be possible to resume parsing according to A if a
symbol in FIRST(A) appears in the input.
4. If a nonterminal can generate the empty string,
then the production deriving can be used as a
default. Doing so may postpone some error
detection, but cannot cause an error to be
missed.

PGiri 27
Error Recovery in Predictive Parsing (cont.)
5. If a terminal on top of the stack, cannot be matched,
a simple idea is to pop the terminal, issue a message
saying that terminal was inserted, and continue
parsing.

PGiri 28
Example
 Add “sync” to indicate synchronizing tokens obtained
from FOLLOW sets.

PGiri 29
Example
 If M[A,a]= ,
skip a.
 If M[A,a]=sync,
A is popped.
 If a token on
top of stack
does not
match input,
pop it.

PGiri 30
Thanks

You might also like