def testLogicalExpression(self): productionrulesetlogical = load_bnf_file("pydsl/contrib/grammar/LogicalExpression.bnf") parser = RecursiveDescentParser(productionrulesetlogical) result = parser.get_trees("True&&False") self.assertTrue(result) result = parser.get_trees("True&|False") self.assertFalse(result)
def testHTMLTable(self): productionrulesetlogical = load_bnf_file("pydsl/contrib/grammar/TrueHTMLTable.bnf") parser = RecursiveDescentParser(productionrulesetlogical) result = parser.get_trees("<table><tr><td>1</td></tr></table>") self.assertTrue(result) result = parser.get_trees("<trble><tr><td>1</td></tr></table>") self.assertFalse(result)
def testLogicalExp(self): productionrulesetlogical = load_bnf_file("pydsl/contrib/grammar/LogicalExpression.bnf") #import pdb #pdb.set_trace() parser = RecursiveDescentParser(productionrulesetlogical) result = parser.get_trees(self.tokelist5) self.assertTrue(result)
class SyntaxDirectedTransformer(object): def __init__(self, inputgrammar, outputgrammar, blockdic): self.blockdic = blockdic if isinstance(inputgrammar, str): from pydsl.Memory.Loader import load inputgrammar = load(inputgrammar) from pydsl.Grammar.Parser.RecursiveDescent import RecursiveDescentParser self.inputgrammar = inputgrammar self.outputgrammar = outputgrammar self.parser = RecursiveDescentParser(inputgrammar) self.blockdic = blockdic def __parseSymbolTokenTree(self, stt): """Returns a tokenlist""" #productionruleset = list(self.inputchanneldic.values())[0].productionset from pydsl.Grammar.Symbol import TerminalSymbol if isinstance(stt.production, TerminalSymbol): return stt.content childlist = [] for child in stt.childlist: childlist.append(self.__parseSymbolTokenTree(child)) if not str(stt.production) in self.blockdic and len(childlist) == 1: return childlist[0] return self.blockdic[str(stt.production)](childlist) def __call__(self, word): stt = self.parser.get_trees(word)[0] return self.__parseSymbolTokenTree(stt) @property def summary(self): return {"iclass":"SyntaxDirectedTransformer", "input":self.inputgrammar, "output":self.outputgrammar }
def __init__(self, inputgrammar, outputgrammar, blockdic): self.blockdic = blockdic if isinstance(inputgrammar, str): from pydsl.Memory.Loader import load inputgrammar = load(inputgrammar) from pydsl.Grammar.Parser.RecursiveDescent import RecursiveDescentParser self.inputgrammar = inputgrammar self.outputgrammar = outputgrammar self.parser = RecursiveDescentParser(inputgrammar) self.blockdic = blockdic
def testTrueFalse(self): productionrulesetlogical = load_bnf_file("pydsl/contrib/grammar/TrueFalse.bnf") parser = RecursiveDescentParser(productionrulesetlogical) result = parser.get_trees(self.tokelist5) self.assertTrue(result)
def testRecursiveDescentParserStore(self): from pydsl.Grammar.Parser.RecursiveDescent import RecursiveDescentParser descentparser = RecursiveDescentParser(self.productionset1) result = descentparser.check(self.tokelist1) self.assertTrue(result)
def testCenterRecursion(self): from pydsl.Grammar.Parser.RecursiveDescent import RecursiveDescentParser descentparser = RecursiveDescentParser(self.productionsetcr) result = descentparser.check(self.dots) self.assertTrue(result)
def testRecursiveDescentParserNullBad(self): from pydsl.Grammar.Parser.RecursiveDescent import RecursiveDescentParser descentparser = RecursiveDescentParser(self.productionset2) result = descentparser.check(self.tokelist4) self.assertTrue(not result)