Non recursive Predictive Parsing
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
The table-driven parser in has an input buffer, a stack containing a sequence of grammar
symbols, a parsing table constructed by Algorithm and an output stream. The input buffer
contains the string to be parsed, followed by the end marker $. We reuse the symbol $ to mark
the bottom of the stack, which initially contains the start symbol of the grammar on top of $.
Algorithm: Table-driven 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 uses the predictive parsing table M
to produce a predictive parse for the input.
Set I p to point to the first symbol of w;
Set X to the top stack symbol;
W h i le (X $) {/* stack is not empty */
If (X is a) pop the stack and advance ip;
Else if (X is a terminal) error Q;
Else if (M[X, a] is an error entry) error Q;
Else if (M[X, a] = X -> Y1Y2 •••Y k) {
Set X to the top stack symbol;
}
Predictive parsing algorithm
We have already seen its the parsing table in On input id + id * id, the non recursive predictive
parser of Algorithm 4.34 makes the sequence of moves in These moves correspond to a
leftmost derivation (for the full derivation):
Note that the sentential forms in this derivation correspond to the input that has already been
matched (in column M A T C H E D) followed by the stack contents. The matched input is
shown only to highlight the correspondence. For the same reason, the top of the stack is to the
left; when we consider bottom-up parsing, it will be more natural to show the top of the stack to
the right. The input pointer points to the leftmost symbol of the string in the INPUT column.

Nonrecursive predictive parsing

  • 1.
    Non recursive PredictiveParsing 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 The table-driven parser in has an input buffer, a stack containing a sequence of grammar symbols, a parsing table constructed by Algorithm and an output stream. The input buffer contains the string to be parsed, followed by the end marker $. We reuse the symbol $ to mark the bottom of the stack, which initially contains the start symbol of the grammar on top of $. Algorithm: Table-driven 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 uses the predictive parsing table M to produce a predictive parse for the input. Set I p to point to the first symbol of w;
  • 2.
    Set X tothe top stack symbol; W h i le (X $) {/* stack is not empty */ If (X is a) pop the stack and advance ip; Else if (X is a terminal) error Q; Else if (M[X, a] is an error entry) error Q; Else if (M[X, a] = X -> Y1Y2 •••Y k) { Set X to the top stack symbol; } Predictive parsing algorithm We have already seen its the parsing table in On input id + id * id, the non recursive predictive parser of Algorithm 4.34 makes the sequence of moves in These moves correspond to a leftmost derivation (for the full derivation): Note that the sentential forms in this derivation correspond to the input that has already been matched (in column M A T C H E D) followed by the stack contents. The matched input is shown only to highlight the correspondence. For the same reason, the top of the stack is to the
  • 3.
    left; when weconsider bottom-up parsing, it will be more natural to show the top of the stack to the right. The input pointer points to the leftmost symbol of the string in the INPUT column.