def evalbase(self, symbol, left, right): if symbol == 'eq?': return self.equals(left, right) if symbol == 'quote': if isinstance(left, SyntaxTree): return left.raw_expr return left if symbol == 'car': return left.left if symbol == 'cdr': return left.right if symbol == 'cons': return self.cons(left, right) if symbol == 'atom?': return len(split_expr(left)) == 1 if symbol == 'lambda': return self.def_lambda(left, right) if symbol == 'eval': return self.eval_lambda(left, right) if symbol == 'define': return self.define(left, right) if symbol == 'cond': if left: return SyntaxTree(split_expr(right)[0], self)() return SyntaxTree(split_expr(right)[1], self)() if symbol == 'bool': if left in ['True', 'true', 't', 'tr', '1', 'T']: return True return False if symbol == 'import': self.handle_import(left)
def handle_import(self, filename): ' this is mostly used to define a lot of statements' if isinstance(filename, SyntaxTree): filename = filename.raw_expr f = open(filename, 'r') exprs = f.read() for expr in exprs: SyntaxTree(expr.replace('\n', '').replace("\t", ""), self)()
def cons(self, left, right): if self.is_tree(right): if (self.is_tree(left)): items = list(left) + list(right) tree = self.right for item in reversed(items): tree = self.cons(item, tree) return tree return SyntaxTree('(cons ' + str(left) + ' ' + right.raw_expr + ")", self, left=left, right=right) if self.is_tree(left): return SyntaxTree( '(cons ' + left.raw_expr + ' ' + str(right) + ')', self, self.cons(left.right, right)) return SyntaxTree('(cons ' + str(left) + ' ' + str(right) + ")", self)
def eval_lambda(self, lam_expr, args): if isinstance(lam_expr, SyntaxTree): lam_expr = lam_expr.raw_expr args = split_expr(args) lam_data = split_expr(lam_expr) expr = lam_data[0] largs = split_expr(lam_data[1]) evaluation_args = {} for count, arg in enumerate(largs): evaluation_args[arg] = args[count] return SyntaxTree(expr.format(**evaluation_args), self)()
from tree import SyntaxTree from convert_to_dfa import ConvertToDfa file = open('input.txt', 'r') inputs = file.read() inputs = inputs.split('\n') print(inputs) for input in inputs: regex = input tree = SyntaxTree(regex) tree.findfollowpos(tree.root) print(tree.followpos) converttree = ConvertToDfa(tree=tree) dfa = converttree.convert() outputfile = open('output.txt', 'a') converttree.write_in_file(outputfile)
from interpreter import Interpreter from tree import SyntaxTree import sys import traceback kno = Interpreter() while True: expr = input("PyLisp >>") s = SyntaxTree(expr, kno) try: stdout = s() except Exception: print("PyLisp error, printing stack trace:") print("-" * 60) traceback.print_exc(file=sys.stdout) print("-" * 60) continue print(stdout)
def get_syntax_tree(self, token_queue): '''Return a syntax tree based on the given token queue. This method does not resolve syntactic sugar.''' active_tree = None while len(token_queue) > 0: token = token_queue.popleft() if token.replace(".", "").isalnum(): # literals are leaves. if active_tree is None: active_tree = SyntaxTree(token) else: active_tree.add_branch(SyntaxTree(token)) elif token in set(self.token_priorities.keys()) or "u" + token in set(self.token_priorities.keys()): if active_tree is None: # unary operator, first active_tree = SyntaxTree("u" + token, unary=True) elif active_tree.is_leaf(): # means active tree is literal new_tree = SyntaxTree(token, active_tree.parent) # nest over current active tree if not active_tree.is_root(): active_tree.parent.right = new_tree #re-assign pointer of parent new_tree.add_branch(active_tree) # will be left active_tree = new_tree # move pointer to the new tree elif not active_tree.is_full(): # unary operator, in middle or end new_tree = SyntaxTree("u" + token, active_tree, unary=True) active_tree.add_branch(new_tree) active_tree = new_tree # shift down to unary operator else: # we use <= to preserve left-right order of operations while self.token_priorities[token] <= self.token_priorities[active_tree.val] and not active_tree.is_root(): active_tree = active_tree.parent if self.token_priorities[token] <= self.token_priorities[active_tree.val]: new_tree = SyntaxTree(token, active_tree.parent) # nest over active_tree (it's fine if active_tree is root) new_tree.add_branch(active_tree) else: new_tree = SyntaxTree(token, active_tree) # nest under current active tree new_tree.add_branch(active_tree.right) # preserve old right branch active_tree.right = new_tree # add underneath active tree active_tree = new_tree # move pointer to the new tree elif token == "(": bracket_wrapper = SyntaxTree("brackets", unary=True) if active_tree is None: active_tree = bracket_wrapper else: active_tree.add_branch(bracket_wrapper) bracket_wrapper.add_branch(self.get_syntax_tree(token_queue)) elif token == ")": return active_tree.get_root() return active_tree.get_root()
def run_test(expr, expected_result): kno = Interpreter() s = SyntaxTree(expr, kno) assert str(s()) == str(expected_result)
def get_syntax_tree(self, token_queue): '''Return a syntax tree based on the given token queue. This method does not resolve syntactic sugar.''' active_tree = None while len(token_queue) > 0: token = token_queue.popleft() if token.replace(".", "").isalnum(): # literals are leaves. if active_tree is None: active_tree = SyntaxTree(token) else: active_tree.add_branch(SyntaxTree(token)) elif token in set( self.token_priorities.keys()) or "u" + token in set( self.token_priorities.keys()): if active_tree is None: # unary operator, first active_tree = SyntaxTree("u" + token, unary=True) elif active_tree.is_leaf(): # means active tree is literal new_tree = SyntaxTree( token, active_tree.parent) # nest over current active tree if not active_tree.is_root(): active_tree.parent.right = new_tree #re-assign pointer of parent new_tree.add_branch(active_tree) # will be left active_tree = new_tree # move pointer to the new tree elif not active_tree.is_full(): # unary operator, in middle or end new_tree = SyntaxTree("u" + token, active_tree, unary=True) active_tree.add_branch(new_tree) active_tree = new_tree # shift down to unary operator else: # we use <= to preserve left-right order of operations while self.token_priorities[token] <= self.token_priorities[ active_tree.val] and not active_tree.is_root(): active_tree = active_tree.parent if self.token_priorities[token] <= self.token_priorities[ active_tree.val]: new_tree = SyntaxTree( token, active_tree.parent ) # nest over active_tree (it's fine if active_tree is root) new_tree.add_branch(active_tree) else: new_tree = SyntaxTree( token, active_tree) # nest under current active tree new_tree.add_branch( active_tree.right) # preserve old right branch active_tree.right = new_tree # add underneath active tree active_tree = new_tree # move pointer to the new tree elif token == "(": bracket_wrapper = SyntaxTree("brackets", unary=True) if active_tree is None: active_tree = bracket_wrapper else: active_tree.add_branch(bracket_wrapper) bracket_wrapper.add_branch(self.get_syntax_tree(token_queue)) elif token == ")": return active_tree.get_root() return active_tree.get_root()
def __call__(self, expr): 'Evaluates a lisp expression' tree = SyntaxTree(expr) return tree()