Exemplo n.º 1
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)
        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)
Exemplo n.º 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))
Exemplo n.º 3
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)
Exemplo n.º 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)
Exemplo n.º 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)
Exemplo n.º 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
Exemplo n.º 8
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".format(result)
     r = lis.eval(result)
     if r is not None:
         print r
Exemplo n.º 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
Exemplo n.º 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)
     '''
Exemplo n.º 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)
Exemplo n.º 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)
Exemplo n.º 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)
Exemplo n.º 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)
Exemplo n.º 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
Exemplo n.º 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)
Exemplo n.º 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
Exemplo n.º 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)
Exemplo n.º 19
0
                    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)
Exemplo n.º 20
0
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:
Exemplo n.º 21
0
def parse(program):
    result = yacc.parse(program)
    if result != None:
        return result
Exemplo n.º 22
0
# -*- 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)

Exemplo n.º 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)
Exemplo n.º 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)
Exemplo n.º 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)