It's simple. Using the debug event interface, the predefined ParseTreeBuilder class listens for debug events and build a parse tree. Just turn on -debug commandline option and then pass in the ParseTreeBuilder listener. Ask the listener for its tree and, using the standard toStringTree() method for any CommonTree, print it out.
Sample run:
java TestP int i; i=4; (<grammar prog> (prog (decl (type int) i ;) (stat i = (expr 4))))
Labels: