Esempio n. 1
0
def main():
    import sys, PgenParser, pprint, DFAParser

    # ____________________________________________________________
    # Generate a test parser
    grammarST = PgenParser.parseFile("Grammar")
    pgenObj = PyPgen()
    grammarObj = pgenObj(grammarST)
    if "-py" in sys.argv:
        print "# %s" % ("_" * 70)
        print "# This was automatically generated by PyPgen."
        print "# Hack at your own risk."
        print
        print "grammarObj =",
    pprint.pprint(grammarObj)
    if "-i" in sys.argv:
        # __________________________________________________
        # Parse some input
        if len(sys.argv) > 1:
            inputFile = sys.argv[1]
            fileObj = open(inputFile)
        else:
            inputFile = "<stdin>"
            fileObj = sys.stdin
        tokenizer = StdTokenizer.StdTokenizer().tokenize(fileObj)
        parseTree = DFAParser.parsetok(tokenizer, grammarObj, 257)
        fileObj.close()
        # __________________________________________________
        # Show the result
        if __BASIL__:
            from basil.visuals.TreeBox import showTree

            showTree(parseTree).mainloop()
        else:
            pprint.pprint(parseTree)
Esempio n. 2
0
def main ():
    import sys, PgenParser, pprint
    # ____________________________________________________________
    # Generate a test parser
    if len(sys.argv) > 1:
        grammarFile = sys.argv[1]
    else:
        grammarFile = "tests/test.pgen"
    grammarST = PgenParser.parseFile(grammarFile)
    parser = buildParser(grammarST)
    pprint.pprint(parser.toTuple())
    # ____________________________________________________________
    # Parse some input
    if len(sys.argv) > 2:
        inputFile = sys.argv[2]
        fileObj = open(inputFile)
    else:
        inputFile = "<stdin>"
        fileObj = sys.stdin
    tokenizer = StdTokenizer.StdTokenizer.tokenize(fileObj)
    parser.setStart(257)
    parseTree = parser.parseTokens(tokenizer)
    fileObj.close()
    # ____________________________________________________________
    # Show the result
    from basil.visuals.TreeBox import showTree
    showTree(parseTree).mainloop()
Esempio n. 3
0
def parserMain (gObj):
    """parserMain()
    Main routine for the default CLI for PyPgen generated parsers.
    """
    import sys, getopt, pprint
    inputFile = None
    outputFile = None
    graphicalOutput = False
    # ____________________________________________________________
    opts, args = getopt.getopt(sys.argv[1:], "gi:o:")
    for (opt_flag, opt_arg) in opts:
        if opt_flag == "-i":
            inputFile = opt_arg
        elif opt_flag == "-o":
            outputFile = opt_arg
        elif opt_flag == "-g":
            graphicalOutput = True
    # ____________________________________________________________
    parser = PyPgenParser(gObj)
    if inputFile != None:
        st = parser.parseFile(inputFile)
    else:
        st = parser.parseString(sys.stdin.read())
    if outputFile == None:
        outputFileObj = sys.stdout
    else:
        outputFileObj = open(outputFile, "w")
    pprint.pprint(st, outputFileObj)
    if graphicalOutput:
        from basil.visuals.TreeBox import showTree
        showTree(st).mainloop()
Esempio n. 4
0
def main ():
    import sys
    if len(sys.argv) > 1:
        parseTree = parseFile(sys.argv[1])
    else:
        parseTree = parseString(sys.stdin.read())
    from basil.visuals.TreeBox import showTree
    showTree(parseTree).mainloop()
Esempio n. 5
0
def main ():
    graphical = False
    tk = None
    if "-g" in sys.argv:
        from basil.visuals.TreeBox import showTree
        graphical = True
    # ____________________________________________________________
    # Check out the metagrammar.
    pprint.pprint(pgen.metaParser.stringToSymbolMap())
    pprint.pprint(pgen.metaParser.symbolToStringMap())
    # ____________________________________________________________
    # Parse the stock Python grammar.
    gAst = pgen.metaParser.parseFile("test.pgen")
    gAstTup = pgen.astToTuple(gAst)
    if graphical:
        tk = showTree(gAstTup)
    else:
        pprint.pprint(gAstTup)
    # ____________________________________________________________
    # Build a parser state machine.
    gParser = pgen.buildParser(gAst)
    print gParser
    # ____________________________________________________________
    # Test the state machine out.
    strToSymMap = gParser.stringToSymbolMap()
    pprint.pprint(strToSymMap)
    pprint.pprint(pgen.symbolToStringMap(gParser))
    # Use the default start symbol - the first symbol in the input grammar.
    testStr = open("test_pgen.py").read()
    fAst0 = gParser.parseString(testStr)
    fAst0Tup = fAst0.toTuple()
    if graphical:
        showTree(fAst0Tup, tk)
    else:
        pprint.pprint(fAst0Tup)
    # And now with a different start symbol.
    gParser.setStart(strToSymMap["file_input"])
    fAst1 = gParser.parseString(testStr)
    fAst1Tup = fAst1.toTuple()
    if graphical:
        showTree(fAst1Tup, tk)
    else:
        pprint.pprint(fAst1Tup)
    # Throw TK into its main loop.
    if graphical:
        tk.mainloop()
Esempio n. 6
0
def main ():
    """main()
    Main routine for the internalize module.  Supposed to load a model based
    on the input file extension and the formats supported by the model
    factory.
    """
    global __DEBUG__
    # ____________________________________________________________
    graphical = False
    help = False
    retVal = 0
    opts, args = getopt.getopt(sys.argv[1:], "dhgv")
    for (opt_key, opt_arg) in opts:
        if opt_key in ("-g", "-v"):
            graphical = True
        elif opt_key == "-h":
            help = True
        elif opt_key == "-d":
            __DEBUG__ = True
    if (len(args) != 2) or (help == True):
        print __topdoc__
        if not help:
            retVal = -1
        sys.exit(retVal)
    # ____________________________________________________________
    moduleName = args[0]
    targetName = args[1]
    module = loadModule(moduleName)
    model = None
    if module != None:
        model = internalize(module, targetName)
    else:
        print "Error, could not load module %s." % moduleName
    # ____________________________________________________________
    if graphical:
        from basil.visuals.TreeBox import showTree
        factory = module.getModelFactory()()
        tree = factory.externalizeTree(model)
        if __DEBUG__:
            pprint.pprint(tree)
        showTree(tree).mainloop()
    if __DEBUG__:
        print "Done. model =", `model`
Esempio n. 7
0
def main():
    graphical = False
    if "-g" in sys.argv:
        from basil.visuals.TreeBox import showTree

        graphical = True
    # ____________________________________________________________
    grammarST0 = PgenParser.parseFile("test.pgen")
    grammarST1Obj = pgen.metaParser.parseFile("test.pgen")
    grammarST1 = grammarST1Obj.toTuple()
    print "Preliminaries:", grammarST0 == grammarST1
    if graphical:
        tk = showTree(grammarST0)
        showTree(grammarST1, tk)
    pgenObj = PyPgen.PyPgen()
    parser0 = pgenObj(grammarST0)
    parser1 = pgenObj(grammarST1)
    parser2Obj = pgen.buildParser(grammarST1Obj)
    parser2 = parser2Obj.toTuple()
    print "Test 1:", parser0 == parser1
    print "Test 2:", parser0 == parser2
    # __________________________________________________
    # The funny part of this sequence is: if the previous tests worked, isn't
    # it reasonable to assume the next test is given?  (i.e. the inputs are
    # structurally identical!)
    testPythonFile = "../PyPgen.py"
    fileObj = open(testPythonFile)
    tokenizer0 = StdTokenizer.StdTokenizer(testPythonFile, fileObj.readline)
    parse0 = DFAParser.parsetok(tokenizer0, parser0, 257)
    # __________________________________________________
    fileObj.seek(0)
    tokenizer1 = StdTokenizer.StdTokenizer(testPythonFile, fileObj.readline)
    parse1 = DFAParser.parsetok(tokenizer1, parser1, 257)
    # __________________________________________________
    fileObj.seek(0)
    tokenizer2 = StdTokenizer.StdTokenizer(testPythonFile, fileObj.readline)
    parse2 = DFAParser.parsetok(tokenizer2, parser2, 257)
    # __________________________________________________
    print "Test 3:", parse0 == parse1
    print "Test 4:", parse0 == parse2
    if graphical:
        showTree(parse0, tk)
        showTree(parse1, tk)
        showTree(parse2, tk)
        tk.mainloop()
Esempio n. 8
0
def main (inputGrammar, inputFile = None):
    """main() - Silly little test routine"""
    # ____________________________________________________________
    # Build tokenizer
    import sys
    try:
        from basil.lang.python import StdTokenizer
    except ImportError:
        import StdTokenizer
    if inputFile == None:
        inputFile = "<stdin>"
        fileObj = sys.stdin
    else:
        fileObj = open(inputFile)
    tokenizer = StdTokenizer.StdTokenizer().tokenize(fileObj)
    # ____________________________________________________________
    # Build parser
    import pprint
    from basil.parsing import pgen
    gramAst = pgen.metaParser.parseFile(inputGrammar)
    myParser = pgen.buildParser(gramAst)
    grammar = myParser.toTuple()
    if __DEBUG__:
        pprint.pprint(grammar)
    symbols = myParser.stringToSymbolMap()
    # ____________________________________________________________
    # Run parser
    import time
    t0 = time.time()
    parseTree = parsetok(tokenizer, grammar, symbols['file_input'])
    t1 = time.time()
    print "DFAParser took %g seconds" % (t1 - t0)
    fileObj.close()
    # ____________________________________________________________
    # Display AST
    from basil.visuals.TreeBox import showTree
    showTree(parseTree).mainloop()