def default(self, line):       
        """Called on an input line when the command prefix is not recognized.
           In that case we execute the line as Python code.
        """
        result = yacc.parse(line)
        if isinstance(result, str):
            if result[len(result) - 2] + result[len(result) - 1] == '**':
                #print result[0 : len(result) - 2]
                result = yacc.parse(result[0 : len(result) - 2])

        if result != None:
            print (result)
Exemple #2
0
 def default(self, line):       
     """Called on an input line when the command prefix is not recognized.
        In that case we execute the line as Python code."""
     ast = yacc.parse(line)
     print ('AST:', ast)
     import lis
     print ('RESULT:', lis.eval(ast))
 def default(self, line):
     """Called on an input line when the command prefix is not recognized.
        In that case we execute the line as Python code.
     """
     result = yacc.parse(line)
     print "result is: ", result
     print lis.eval(result)
Exemple #4
0
def main():
    with open('testfile.c', 'r') as content_file:
        content = content_file.read()
        AST = yacc.parse(content)
        print AST
        if AST != None:
            lis.eval(AST)
Exemple #5
0
 def default(self, line):
     """Called on an input line when the command prefix is not recognized.
        In that case we execute the line as Python code.
     """
     result = yacc.parse(line)
     print "result is: ", result
     print lis.eval(result)
Exemple #6
0
 def default(self, line):       
     """Called on an input line when the command prefix is not recognized.
        In that case we execute the line as Python code.
     """
     result = yacc.parse(line)
     s = lisp_str(result)
     if s != 'nil':
         print s
    def default(self, line):
        """Called on an input line when the command prefix is not recognized.
           In that case we execute the line as Python code.
        """
        result = yacc.parse(line)

        import lis
        r = lis.eval(result)
        if r is not None: print r
 def default(self, line):       
     """Called on an input line when the command prefix is not recognized.
        In that case we execute the line as Python code.
     """
     result = yacc.parse(line)
     print "AST is".format(result)
     r = lis.eval(result)
     if r is not None:
         print r
Exemple #9
0
 def default(self, line):       
     """Called on an input line when the command prefix is not recognized.
        In that case we execute the line as Python code.
     """
     result = yacc.parse(line)  # parsing the line and evaluating the line; this yacc refers to the yacc that's in ply folder
     # result is the ast
     s = lisp_str(result)
     if s != 'nil':
         print s
Exemple #10
0
 def default(self, line):       
     """Called on an input line when the command prefix is not recognized.
        In that case we execute the line as Python code.
     """
     result = yacc.parse(line)  # parsing the line and evaluating the line; this yacc refers to the yacc that's in ply folder
     # result is the ast
     print "result is: ", result
     import lis
     print lis.eval(result)
     '''
Exemple #11
0
 def default(self, line):
     """Called on an input line when the command prefix is not recognized.
        In that case we execute the line as Python code.
     """
     AST = yacc.parse(line)
     s = lisp_str(AST)
     if s != 'nil':
         print("Parsed input: " + s)
         print("into AST: " + str(AST))
         print("Trying to evaluate result...")
         result = lis.eval(AST)
         print(result)
Exemple #12
0
 def default(self, line):       
     """Called on an input line when the command prefix is not recognized.
        In that case we execute the line as Python code.
     """
     AST = yacc.parse(line)
     s = lisp_str(AST)
     if s != 'nil':
         print ("Parsed input: " + s)
         print ("into AST: " + str(AST))
         print ("Trying to evaluate result...")
         result = lis.eval(AST)
         print (result)
Exemple #13
0
    def default(self, line):
        """Called on an input line when the command prefix is not recognized.
           In that case we execute the line as Python code.
        """
        print "\nParsing input with YACC:"
        result = yacc.parse(line)
        # This result is the Abstract Syntax Tree.
        print "The result is: ", result

        # Imports lis.py methods in the same folder, then passes the result for evaluation.
        import lis
        print ("\nEvaluating result with Lis.py:")
        print lis.eval(result)
Exemple #14
0
    def default(self, line):
        def eval(list):
            if list[0] == 'car':
                return (lambda x: x[1][1][0])(list)
            else:
                return "unrecognized function"

        """Called on an input line when the command prefix is not recognized.
           In that case we execute the line as Python code.
        """
        result = yacc.parse(line)
        print(result)
        #import lis
        print eval(result)
Exemple #15
0
    def default(self, line):       
        """Called on an input line when the command prefix is not recognized.
           In that case we execute the line as Python code.
        """
        result = yacc.parse(line)

        # print "AST is: ", result
        # import lis
        # r = lis.eval(result)
        # if r is not None: print r
        
        s = lisp_str(result)
        if s != 'nil':
            print s
Exemple #16
0
    def default(self, line):
        def eval(list):
            if list[0] == 'car':
                return (lambda x : x[1][1][0])(list)
            else:
                return "unrecognized function"

        """Called on an input line when the command prefix is not recognized.
           In that case we execute the line as Python code.
        """
        result = yacc.parse(line)
        print (result)
        #import lis
        print eval(result)
Exemple #17
0
    def default(self, line):       
        """Called on an input line when the command prefix is not recognized.
           In that case we execute the line as Python code.
        """
        result = yacc.parse(line)
        print "mini-lisp.py: AST is: ", result

        dirtyToken = Evaluator.tokenCleaner(result)
        f = lambda x, y, z : x[z][z:-z]
        cleanedToken = f(result, Evaluator.tokenCleaner(), 1)
        output = Evaluator.evaluate(cleanedToken)

        if output is not None:
           print output
Exemple #18
0
 def default(self, line):       
     """Called on an input line when the command prefix is not recognized.
        In that case we execute the line as Python code.
     """
     # from class
     '''
     result = yacc.parse(line)
     s = lisp_str(result)
     if s != 'nil':
         print s
     '''
     result = yacc.parse(line)
     print "result is: ", result
     import lis
     print lis.eval(result)
                    evaluate(commands[2]) # Execute code block
                elif len(commands) == 4: # Else statement
                    evaluate(commands[3][1])
            elif commands[1][0] in constants and isinstance(commands[1][2], (str)): # a == "word", where a is a constant
                if booleanHelper(constants[commands[1][0]], commands[1][1], commands[1][2]): # if statement evaluates to true
                    evaluate(commands[2]) # Execute code block
                elif len(commands) == 4: # Else statement
                    evaluate(commands[3][1])
            else:
                raise Exception("Symbol not found")

print("***test-helloworld.swift***")
with open('test-helloworld.swift', 'r') as content_file:
    content = content_file.read()
content_file.close()
ast = yacc.parse(content)

print "ast is: " + str(ast)
evaluate(ast)
# Clear dictionaries for the next files
vars.clear()
constants.clear()
print("\n\n")

print("***test-arithmetic.swift***")
with open('test-arithmetic.swift', 'r') as content_file:
    content = content_file.read()
content_file.close()
ast = yacc.parse(content)

print "ast is: " + str(ast)
from yacc import yacc

# Use this if you want to build the parser using SLR instead of LALR
# yacc.yacc(method="SLR")

if __name__ == '__main__':

    import sys
    from classes import *

    if len(sys.argv) == 3:
        kb = KnowledgeBase()
        prem = open(sys.argv[1]).readlines()
        for n,p in enumerate(prem):
            s = yacc.parse(p)
            if s:
                kb.add_knowledge(s)
            else:
                print "Error en la linea %d de %s" % (n, sys.argv[1])
        goals = open(sys.argv[2]).readlines()
        for n,g in enumerate(goals):
            s = yacc.parse(g)
            if s:
                kb.add_goal(s)
            else:
                print "Error en la linea %d de %s" % (n, sys.argv[2])
        kb.search()

    else:

        while 1:
Exemple #21
0
def parse(program):
    result = yacc.parse(program)
    if result != None:
        return result
# -*- coding: utf-8 -*-
from yacc import yacc
import swift


with open('testfile.c', 'r') as content_file:
    chunk = ""
    for line in content_file:
        print("")
        result = yacc.parse(line)
        print("AST is {}".format(result))
        r = swift.eval(result)
        if r is not None:
            print "Result is {}".format(r)

Exemple #23
0
from yacc import yacc, lisp_str
import cmd


with open('testerfile.txt', 'r') as content_file:
    content = content_file.read()
s = ''
lines = []
for i in content:
    if i == '\n':
        lines.append(s)
        s = ''
    else:
        s += i
lines.append(s)
for i in lines:
    result = yacc.parse(i)
    if isinstance(result, str):
        if result[len(result) - 2] + result[len(result) - 1] == '**':
            result = yacc.parse(result[0 : len(result) - 2])
    if result != None:
        print (result)
Exemple #24
0
def eval(x, env=global_env):
    "Evaluate an expression in an environment."
    if (isinstance(x, Symbol)):  # variable reference
        if DEBUG: print "var ref"
        if (env.find(x) != 0):
            return env.find(x)[x]
        else:
            if (str_check.match(x)):
                if (var_check1.search(x)):
                    l = var_check1.findall(x)
                    if (len(l) == 1):
                        y = (l[0])[2:(len(l[0]) - 1)]
                        y = yacc.parse(y)
                        y = str(eval(y))
                        x = x.replace(str(l[0]), y, 1)
                    else:
                        for s in (0, (len(l) - 1)):
                            y = (l[s])[2:(len(l[s]) - 1)]
                            y = yacc.parse(y)
                            y = str(eval(y))
                            x = x.replace(str(l[s]), str(eval(y)), 1)
                    return x
                elif (lambda_check.match(x)):
                    l = lambda_check.findall(x)
                    y = (l[0])[2:(len(l[0]) - 1)]
                    y = yacc.parse(y)
                    y = str(eval(y))
                    print y
                else:
                    return x
            else:
                print 'Could not resolve symbol', x
                return None
    elif not isinstance(x, List):  # constant literal
        if DEBUG: print "const lit"
        return x
    elif x[0] == 'quote':  # (quote exp)
        if DEBUG: print "quote"
        (_, exp) = x
        return exp
    elif x[0] == 'if':  # (if test conseq alt)
        if DEBUG: print "if"
        (_, test, conseq, alt) = x
        exp = (conseq if eval(test, env) else alt)
        return eval(exp, env)
    elif x[0] == 'define':  # (define var exp)
        if DEBUG: print "def"
        (_, var, exp) = x
        if (eval(exp, env) != None):
            env[var] = eval(exp, env)
        else:
            print 'Not a valid constant assignment.'
    elif x[0] == 'set!':  # (set! var exp)
        if DEBUG: print "set!"
        (_, var, exp) = x
        env.find(var)[var] = eval(exp, env)
    elif x[0] == 'lambda':  # (lambda (var...) body)
        if DEBUG: print "lambda"
        (_, parms, body) = x
        return Procedure(parms, body, env)
    elif x[0] == 'exec':
        if DEBUG: print "exec"
        proc = eval(x[0], env)
        import re
        exec(proc(re.sub(r"^'|'$", '', x[1])))
        return toReturn
    else:  # (proc arg...)
        if DEBUG: print "else"
        proc = eval(x[0], env)
        args = [eval(exp, env) for exp in x[1:]]
        return proc(*args)
Exemple #25
0
                (str)):  # a == "word", where a is a constant
                if booleanHelper(
                        constants[commands[1][0]], commands[1][1],
                        commands[1][2]):  # if statement evaluates to true
                    evaluate(commands[2])  # Execute code block
                elif len(commands) == 4:  # Else statement
                    evaluate(commands[3][1])
            else:
                raise Exception("Symbol not found")


print("***test-helloworld.swift***")
with open('test-helloworld.swift', 'r') as content_file:
    content = content_file.read()
content_file.close()
ast = yacc.parse(content)

print "ast is: " + str(ast)
evaluate(ast)
# Clear dictionaries for the next files
vars.clear()
constants.clear()
print("\n\n")

print("***test-arithmetic.swift***")
with open('test-arithmetic.swift', 'r') as content_file:
    content = content_file.read()
content_file.close()
ast = yacc.parse(content)

print "ast is: " + str(ast)