Esempio n. 1
0
 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)
Esempio n. 2
0
 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)
Esempio n. 3
0
 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)
Esempio n. 4
0
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 }
Esempio n. 5
0
 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
Esempio n. 6
0
 def testTrueFalse(self):
     productionrulesetlogical = load_bnf_file("pydsl/contrib/grammar/TrueFalse.bnf")
     parser = RecursiveDescentParser(productionrulesetlogical)
     result = parser.get_trees(self.tokelist5)
     self.assertTrue(result)
Esempio n. 7
0
 def testRecursiveDescentParserStore(self):
     from pydsl.Grammar.Parser.RecursiveDescent import RecursiveDescentParser
     descentparser = RecursiveDescentParser(self.productionset1)
     result = descentparser.check(self.tokelist1)
     self.assertTrue(result)
Esempio n. 8
0
 def testCenterRecursion(self):
     from pydsl.Grammar.Parser.RecursiveDescent import RecursiveDescentParser
     descentparser = RecursiveDescentParser(self.productionsetcr)
     result = descentparser.check(self.dots)
     self.assertTrue(result)
Esempio n. 9
0
 def testRecursiveDescentParserNullBad(self):
     from pydsl.Grammar.Parser.RecursiveDescent import RecursiveDescentParser
     descentparser = RecursiveDescentParser(self.productionset2)
     result = descentparser.check(self.tokelist4)
     self.assertTrue(not result)