Non-recursive predictive parsing
Our recursive descent parser encodes state information in its run-time stack, or call stack.Using
recursive procedure calls to implement a stack abstraction may not be particularly efficient.
This suggests other implementation methods:
• Explicit stack, hand-coded parser
• Stack-based, table-driven parser
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 •••Yk) {
Set X to the top stack symbol;
}
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.
Non- recusive

Non- recusive

  • 1.
    Non-recursive predictive parsing Ourrecursive descent parser encodes state information in its run-time stack, or call stack.Using recursive procedure calls to implement a stack abstraction may not be particularly efficient. This suggests other implementation methods: • Explicit stack, hand-coded parser • Stack-based, table-driven parser 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;
  • 2.
    Else if (Xis a terminal) error Q; Else if (M[X, a] is an error entry) error Q; Else if (M[X, a] = X -> Y1Y2 •••Yk) { Set X to the top stack symbol; } 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.