* @param wantTree true if the parse tree should be generated
* @return parse tree if generate parse tree open is set
*/
public String executeProgram(String sourceCode, boolean wantTree) {
// Use the compiled grammar file inside the jar
GOLDParser parser = new GOLDParser(
getClass().getResourceAsStream("/simple3/Simple3.cgt"), // compiled grammar table
"com.creativewidgetworks.goldparser.simple3", // rule handler package
true); // trim reductions
// Controls whether or not a parse tree is returned or the program executed.
parser.setGenerateTree(wantTree);
String tree = null;
try {
// Parse the source statements to see if it is syntactically correct
boolean parsedWithoutError = parser.parseSourceStatements(sourceCode);
// Holds the parse tree if setGenerateTree(true) was called
tree = parser.getParseTree();
// Either execute the code or print any error message
if (parsedWithoutError) {
parser.getCurrentReduction().execute();
} else {
System.out.println(parser.getErrorMessage());
}
} catch (ParserException e) {
System.out.println(e.getMessage());
e.printStackTrace();
}