def __init__(self): import grammar block.block.__init__(self, parser.pushstream(lex.lex(proarkhe.illiterate()))) self.decladd("OMEGA", "ordinals") self.decladd("natural", "type") self.decladd("integer", "type") self.decladd("real", "type") self.decladd("Boolean", "type") self.decladd("true", "Boolean") self.decladd("false", "Boolean") self.visit(grammar.parentset) self.walk(grammar.tokenset)
return True def tokenset(node, order): if order == tree.tnode.WALK_POST and (not node.start and node.children): node.start = node.children[0].start node.end = node.children[-1].end return True import block import exp if __name__ == "__main__": import sys import reader s = S(parser.pushstream(lex.lex(reader.filereader(sys.stdin)))) if not s.parse(): print("Cannot parse") elif not s.atend(): print("Garbage after S") else: print(s.pprint()) print(s.printexp()) s.block = proarkhe.proarkhe() s.visit(parentset) s.walk(tokenset) s.reform([exp.simplify_depth], [exp.simplify_arith]) s.visit(parentset) s.visit(block.blockset) s.visit(block.declsset)
def pop(self): self.marks.pop() def rollback(self): self.pos, self.out = self.marks.pop() if __name__ == "__main__": import sys import grammar import reader import exp import block te = trex(parser.pushstream(lex.lex(reader.filereader(sys.stdin)))) assert te.parse() print(te.pprint()) program = """ [ c: c; (# not very well-founded #) a: c; OMEGA : OMEGA | a := a + 1 * c; c := 6 - a + c + a * 0 + OMEGA; a := (a) + 1 + c; [| [ y : a; z : c; y1 : c |];
def simplify_depth(node): if len(node.children) == 1 and \ any([isinstance(node, ntype) for ntype in [exp, logic, order, add, mul, unary, atom, stmt.stmt, stmt.stmts, block.decls]]): return [node.children[0]] elif any([parser.istoken(node, t) for t in ["[", "]", "(", ")", ";", "|", ":", ":="]]): return [] else: return [node] import trex import reader identities = [trex.trex(parser.pushstream( lex.lex(reader.stringreader(s)))) for s in ['"0" ; "+" ; x:. ;^>add>', # 0 + x == x 'x:. ; "+" ; "0" ;^>add>', # x + 0 == x 'x:. ; "-" ; "0" ;^>add>', # x - 0 == x 'x:"OMEGA" ; "+" ; . ;^>add>', # OMEGA + x == OMEGA 'x:"OMEGA" ; "-" ; . ;^>add>', # OMEGA - x == OMEGA '. ; "+" ; x:"OMEGA" ;^>add>', # x + OMEGA == OMEGA '"1" ; "*" ; x:. ;^>mul>', # 1 * x == x 'x:. ; "*" ; "1" ;^>mul>', # x * 1 == x 'x:. ; "/" ; "1" ;^>mul>', # x / 1 == x 'x:"0" ; "*" ; . ;^>mul>', # 0 * x == 0 'x:"0" ; "/" ; . ;^>mul>', # 0 / x == 0 '. ; "*" ; x:"0" ;^>mul>']] # x * 0 == 0 assert all(i.parse() and i.atend() for i in identities)
def rest(self): return self.buf[self.count:] import readline if __name__ == "__main__": import proarkhe import grammar import parser import block import exp import lex while True: rep = repreader(["; ", "+ "]) e = grammar.S(parser.pushstream(lex.lex(rep))) try: parsed = e.parse() except EOFError: break except lex.lex.lexerror as lexexp: continue if not parsed: print("Cannot parse: '{}'@{} '{}'".format(rep.buf, rep.count, rep.rest())) elif rep.rest() and not rep.rest().isspace(): # for whatever reason "".isspace() is False print("Garbage after S: '{}' ({})".format(rep.buf, len(rep.buf))) else:
stmt.stmts, block.decls]]): return [node.children[0]] elif any([ parser.istoken(node, t) for t in ["[", "]", "(", ")", ";", "|", ":", ":="] ]): return [] else: return [node] import trex import reader identities = [ trex.trex(parser.pushstream(lex.lex(reader.stringreader(s)))) for s in [ '"0" ; "+" ; x:. ;^>add>', # 0 + x == x 'x:. ; "+" ; "0" ;^>add>', # x + 0 == x 'x:. ; "-" ; "0" ;^>add>', # x - 0 == x 'x:"OMEGA" ; "+" ; . ;^>add>', # OMEGA + x == OMEGA 'x:"OMEGA" ; "-" ; . ;^>add>', # OMEGA - x == OMEGA '. ; "+" ; x:"OMEGA" ;^>add>', # x + OMEGA == OMEGA '"1" ; "*" ; x:. ;^>mul>', # 1 * x == x 'x:. ; "*" ; "1" ;^>mul>', # x * 1 == x 'x:. ; "/" ; "1" ;^>mul>', # x / 1 == x 'x:"0" ; "*" ; . ;^>mul>', # 0 * x == 0 'x:"0" ; "/" ; . ;^>mul>', # 0 / x == 0 '. ; "*" ; x:"0" ;^>mul>' ] ] # x * 0 == 0