Top-Down Parsing Predictive Parsing

Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Top-down Parsing – Non Recursive Predictive Parser

Predictive Parsers
Some changes like removal of left recursion and left factoring the resultant grammar results in a
grammar that can be parsed by a recursive descent parser that needs no backtracking, called as
predictive parsing. The key problem during top down parsing is, given the current input symbol
‘a’ and the non-terminal A to be expanded, which among the different alternative productions
Aα|β|…….|γ is to be chosen. This problem can be solved with the help of a table constructed
called as the predictive parsing table.
Non-recursive Predictive Parser
A non-recursive predictive parser can be built by maintaining a stack explicitly, rather than
implicitly via recursive calls. The parser mimics a leftmost derivation. If w is the input that has
been matched so far, then the stack holds a sequence of grammar symbols a such that

Components of a Predictive Parser


There are four important components of a predictive parser:
1. The Parsing Program
2. The Parsing Table
3. An Input buffer, and
4. A Stack
The Parsing Program
The parser is controlled by the parsing program that considers X, the symbol on top of the stack,
and a, the current input symbol. The behavior of the parser can be described in terms of
its configurations, which give the stack contents and the remaining input.
The Parsing Table
The parsing table is a two dimensional array M[A,a], where A is a nonterminal, and ‘a’ terminal
or the symbol $.
Input buffer and Stack
The input buffer contains the string to be parsed, followed by the end-marker $. The stack consists
a sequence of grammar symbols with $ at the bottom. Initially the stack contains the start symbol
of the grammar on top of $.
The diagrammatic representation of the non-recursive predictive parser is given in the Figure 1:

Figure 1: Model of a non-recursive predictive parser

The Parsing Program Procedure:


The predictive parser considers X, the symbol on top of the stack, and a, the current input symbol.
These two symbols determine the action of the parser. There are four possibilities:
If X is a terminal and
If X=a=$  halt and return success
If X=a≠$  pop X off the stack and advance input pointer to the next symbol
If X ≠a  halt and return failure
If X is a non-terminal  we consult the parsing table Check M[X,a]
If the entry is a production rule, then replace X on the stack by the Right
Hand Side of the production
If the entry is blank, then halt and return failure
The behavior of the parser can be described in terms of its configurations, which give the stack
contents and the remaining input. The following algorithm completely describes how the
configurations are manipulated during predictive parsing

Algorithm: Non-recursive predictive parsing.


Input: A string w and a parsing table M for grammar G.
Output: If w is in L(G), a leftmost derivation of w; otherwise, an error indication.
Method: Initially, the parser is in a configuration with w$ in the input buffer and the start symbol
S of G on top of the stack, above $. The program in Fig. 4.20 uses the predictive parsing table M
to produce a predictive parse for the input.
set ip to point to the first symbol of w;
set X to the top stack symbol;
while ( X ≠$ ) { /* stack is not empty */
if ( X is a ) pop the stack and advance ip;
else if ( X is a terminal ≠ a) error();
else if ( M[X,a] is an error entry ) error();
else if ( M[X,a] = X -> Y1Y2 •••Yk) {
output the production X -> Y1 Y2 • • • Yk;
pop the stack;
push Yk, Yk-i,... ,Yi onto the stack, with Y1 on top;
}
set X to the top stack symbol;
}
Example: Consider the grammar given below. The parsing table of the grammar is given in Figure
2. On input id + id * id, the non-recursive predictive parser of Algorithm above makes the sequence
of moves in the Figure 3. These moves correspond to a leftmost derivation:

Figure 2: Parsing Table for the above Grammar


Figure 3: Moves made by predictive parser on input id+id*id
With input id+id*id the predictive parser makes the sequence of moves in Figure 3. The input
pointer points to the leftmost symbol of the string in the INPUT column of Figure 3. If we observe
carefully we can see that the predictive parser traces out a leftmost derivation for the input, that is,
the productions output are those of a leftmost derivation. The input symbols that have already been
scanned, make up the left-sentential forms in the derivation.

You might also like