declist ---> dec { dec }
dec ---> type id { "," id } ";"
type ---> "double" | "int"
Stack (top at right) Curr Rest of Input Action
Sym
--------------------------------------------------------------------------
$ ( id + id ) * id $ shift
$ ( id + id ) * id $ shift
$ ( id + id ) * id $ reduce: F ---> id
$ ( F + id ) * id $ reduce: T ---> F
$ ( T + id ) * id $ reduce: E ---> T
$ ( E + id ) * id $ shift
$ ( E + id ) * id $ shift
$ ( E + id ) * id $ reduce: F ---> id
$ ( E + F ) * id $ reduce: T ---> F
$ ( E + T ) * id $ reduce: E ---> E + T
$ ( E ) * id $ shift
$ ( E ) * id $ reduce: F ---> ( E )
$ F * id $ reduce: T ---> F
$ T * id $ shift
$ T * id $ shift
$ T * id $ reduce: F ---> id
$ T * F $ reduce: T ---> T * F
$ T $ reduce: E ---> T
$ E $ reduce: S ---> E
$ S $ accept
/* GrammarDebug.java: simple parser -- no output
* grammar:
* P ---> S '$'
* S ---> 'b' M 'b' (S is the start symbol)
* M ---> '(' L
* M ---> 'a'
* L ---> M 'a' ')'
*/
import java.io.*;
class GrammarDebug {
private int level = 0;
private char next;
private void P() {
scan();
S();
if (next != '$') error(2);
else System.out.println("Successful parse");
}
private void S() {
enter('S');
if (next == 'b') scan();
else error(0);
M();
if (next == 'b') scan();
else error(1);
leave('S');
}
private void M() {
enter('M');
if (next == '(') {
scan();
L();
}
else if (next == 'a') scan();
else error(3);
leave('M');
}
private void L() {
enter('L');
M();
if (next == 'a') scan();
else error(4);
if (next == ')') scan();
else error(5);
leave('L');
}
public static void main(String[] args) {
GrammarDebug grammarDebug = new GrammarDebug();
grammarDebug.P();
}
}
Here is a run of the program, with input shown in boldface:
ten42% java GrammarDebug b ( ( a a ) a ) b $ +-S: Enter, Next == b | +-M: Enter, Next == ( | | +-L: Enter, Next == ( | | | +-M: Enter, Next == ( | | | | +-L: Enter, Next == a | | | | | +-M: Enter, Next == a | | | | | +-M: Leave, Next == a | | | | +-L: Leave, Next == a | | | +-M: Leave, Next == a | | +-L: Leave, Next == b | +-M: Leave, Next == b +-S: Leave, Next == $ Successful parse