示例#1
0
def load(filename):
    """Load a database by generating code for the interpreter 
    to execute. Return a new phonebook
    """
    try:
        f = open(str(filename), 'r')
    except:
        print "oops, file not found"
        return []
    entries = f.readlines()
    pbook = []
    for entry in entries:
        values = entry.split(';')
        lexed = parser.lexer("add %s %s" % (values[1], values[0]))
        succ, parsed = parser.parse(lexed)
        if succ:
            pbook = exe(pbook, parsed)
        else:
            print "error, bad filetype"
            return []
        if len(values) > 2:
            for i in range(len(values) - 2):
                lexed = parser.lexer("alias %s %s" % (values[1], \
                    values[i + 2]))
                succ, parsed = parser.parse(lexed)
                if succ:
                    pbook = exe(pbook, parsed)
                else:
                    print "error, bad filetype"
                    return []
    f.close()
    return pbook
示例#2
0
def main():
    pbook = []
    if len(sys.argv) > 1:
        # if a file is an argument
        for line in open(sys.argv[1], 'r'):
            l = line.strip('\n')
            print prompt,
            print l
            succ, tree = parser.parse(parser.lexer(l))
            if succ:
                pbook = exe(pbook, tree)
            else:
                print tree
    else:
        while True:
            try:
                a = raw_input(prompt)
            except:
                break
            succ, tree = parser.parse(parser.lexer(a))
            if len(tree) == 0:
                print "not a keyword"
            elif succ:
                pbook = exe(pbook, tree)
            elif not succ:
                print tree
示例#3
0
def main():
    print(' +' + '~' * 45 + '+')
    print(' | T U L K K I   L A U S E L O G I I K A L L E |')
    print(' +' + '~' * 45 + '+')

    inputstring, tautology = getArgs()
    tokens, labels = parser.lexer(inputstring)
    if parser.T_INVALID not in tokens:
        print("\n   Leksikaalinen analyysi ok.")

        parseTree = parser.syntacticAnalysis(tokens, labels)
        if parseTree:  # (Syntactic analysis returns valid parse tree if ok, else 'False')
            print("   Syntaktinen analyysi ok.")
            parseTree.printTree()

            print(
                '   LAUSE:\n  ',
                inputstring.replace('v', ' v ').replace('&', ' & ').replace(
                    '->', ' -> ').replace('< ->', ' <-> '))
            ids = parser.askTruthValues(labels, tokens)
            terms = [
            ]  # Use 'terms' as a stack of all the terms from the parse tree (in post order)
            parser.getTermsInPostOrder(parseTree, terms, ids)

            # Form 'pretty' strings for output:
            statement = ' '.join(labels).replace('~ ', '~').replace(
                '( ', '(').replace(' )', ')')
            statementWithThruthValues = statement
            for key, val in ids.items():
                statementWithThruthValues = statementWithThruthValues.replace(
                    key, str(val))

            print(' +' + '~' * (len(statement) + 28) + '|')
            print(" | Lause", statementWithThruthValues, "ON",
                  "TOSI" if parser.testStatement(terms) else "EPÄTOSI")

            if tautology:
                tautology = parser.checkTautology(parseTree, ids)
                print(" | \n | Lause", statement,
                      "ON TAUTOLOGIA" if tautology else "EI OLE TAUTOLOGIA")
            print(' +' + '~' * (len(statement) + 28) + '|')
            print('   Ohjelma lopetettu.')

        else:
            print("   Tarkista syntaksi!")
    else:
        for i, token in enumerate(tokens):
            if parser.T_INVALID == token:
                print("   Tarkista lause, termi nro %d virheellinen (%s)" %
                      (i + 1, labels[i]))