def setUp (self): # Parse the MyFront grammar, create a set of automata for it (like # pgen does), and then convert the automata to generators for the # treepoline. grammar_st = PgenParser.parseFile(basil.lang.python.__path__[0] + "/python26/Grammar") grammar_obj = PyPgen.buildParser(grammar_st) grammar_obj.setStart(grammar_obj.stringToSymbolMap()['file_input']) self.parser1 = grammar_obj gram_tup0 = grammar_obj.toTuple() gram_tup1 = DFAParser.addAccelerators(gram_tup0) handlers = trampoline.pgen_grammar_to_handlers(gram_tup1, {}) # Override the start special nonterminal to just do what it is # supposed to: def parse_start (instream, outtree): yield 'file_input' handlers['start'] = parse_start self.handlers = handlers
def main (*args): """main() - Unit test routine for the PushdownAutomaton module.""" from basil.lang.python import DFAParser from basil.parsing import PgenParser, PyPgen import sys, getopt # ____________________________________________________________ opts, args = getopt.getopt(args, "o:") outFile = sys.stdout for optFlag, optArg in opts: if optFlag == "-o": outFile = open(optArg, "w") argc = len(args) if argc > 1: print "Usage:\n\tPushdownAutomaton.py [opts] <file.pgen>\n" sys.exit(-1) elif argc == 1: srcFile = args[0] else: srcFile = "./tests/test.pgen" grammarST = PgenParser.parseFile(srcFile) parser = PyPgen.buildParser(grammarST) grammarTup = parser.grammarObj # ____________________________________________________________ # Round trip test myGrammarObj = PushdownGrammar(grammarTup) myGrammarObj.compareToTuple(grammarTup) grammarTup2 = myGrammarObj.toTuple() myGrammarObj.compareToTuple(grammarTup2) # Now with accelerators... grammarTup3 = DFAParser.addAccelerators(grammarTup) myGrammarObj2 = PushdownGrammar(grammarTup3) myGrammarObj2.compareToTuple(grammarTup3) grammarTup4 = myGrammarObj2.toTuple() myGrammarObj2.compareToTuple(grammarTup4) # ____________________________________________________________ # Output tests. outFile.write("%s\n" % myGrammarObj2.toString("graphviz")) if outFile != sys.stdout: outFile.close()