Cleaned up.
1.1 --- a/smm.py Tue May 17 14:05:25 2011 +0200
1.2 +++ b/smm.py Tue May 17 16:08:04 2011 +0200
1.3 @@ -9,21 +9,29 @@
1.4 import token
1.5
1.6 def main():
1.7 - args = parse_arguments()
1.8 - scanner = Scanner(TOKENS)
1.9 - parser = Parser(SYNTAX, scanner)
1.10 - #(accepted, out, tree_nodes) = parser.parse(args.formula)
1.11 - #tree = SyntaxTree(out, tree_nodes)
1.12 - #print tree.root
1.13 p = Variable(["p"])
1.14 q = Variable(["q"])
1.15 r = Variable(["r"])
1.16 - formula = Imp([Eq([p, q]), r])
1.17 - #formula = Not([Or([p, q])])
1.18 - #formula = Not([Not([p])])
1.19 + formula1 = Imp([Eq([p, q]), r])
1.20 + formula2 = Not([Or([p, q])])
1.21 + formula3 = Not([Not([p])])
1.22 + use = formula1
1.23 +
1.24 + args = parse_arguments()
1.25 + if (args.formula):
1.26 + scanner = Scanner(TOKENS)
1.27 + parser = Parser(SYNTAX, scanner)
1.28 + (accepted, out, tree_nodes) = parser.parse(args.formula)
1.29 + if not accepted:
1.30 + print EXCUSE
1.31 + return
1.32 + tree = SyntaxTree(out, tree_nodes)
1.33 + formula = tree.root
1.34 +
1.35 print formula
1.36 print "reduced"
1.37 print reduce(formula)
1.38 + print reduce(formula)
1.39
1.40 def reduce(formula):
1.41 map = {IMP: lambda (a, b): Or([Not([a]), b]),
1.42 @@ -39,9 +47,7 @@
1.43 if not isinstance(formula, Operator) or formula.id not in map:
1.44 return formula
1.45 return map[formula.id](values())
1.46 -
1.47 -
1.48 -
1.49 +
1.50 NOT = "~"
1.51 AND = "&"
1.52 OR = "|"
1.53 @@ -64,9 +70,7 @@
1.54 out = str(self.values[0])
1.55 if self.arity == 1:
1.56 out = self.id + str(self.values[0])
1.57 - elif self.arity == 2:
1.58 - #print self.id
1.59 - #print self.values
1.60 + elif self.arity == 2:
1.61 out = "(" + str(self.values[0]) + " " + self.id + " " + str(self.values[1]) + ")"
1.62 return out
1.63
1.64 @@ -247,15 +251,10 @@
1.65 class SyntaxTree(object):
1.66 def __init__(self, seq, tree_nodes):
1.67 seq.reverse()
1.68 - tree_nodes.reverse()
1.69 - print tree_nodes
1.70 - print seq
1.71 - self.root = self.compile(seq, tree_nodes)[0]
1.72 - print self.root
1.73 + tree_nodes.reverse()
1.74 + self.root = self.compile(seq, tree_nodes)[0]
1.75 def compile(self, seq, tree_nodes):
1.76 - stack = []
1.77 - waiting = []
1.78 - print seq
1.79 + stack = []
1.80 while len(seq):
1.81 s = seq.pop()
1.82 if s == 0:
1.83 @@ -278,45 +277,17 @@
1.84 elif s == 7:
1.85 stack.append(Imp(self.compile(seq, tree_nodes)))
1.86 elif s == 8:
1.87 - stack.append(Eq(self.compile(seq, tree_nodes)))
1.88 - print stack
1.89 - return stack
1.90 -
1.91 - def compile_(self, tree_nodes):
1.92 - table = []
1.93 - unfinished = []
1.94 - node = None
1.95 - print tree_nodes
1.96 - for n, v in (tree_nodes):
1.97 - print
1.98 - print n
1.99 - if n.arity >= 0:
1.100 - if len(table) < n.arity:
1.101 - print "appending unfinished ", n
1.102 - unfinished.append(n)
1.103 - else:
1.104 - args = table[:n.arity]
1.105 - if n.arity == 0:
1.106 - args = [v]
1.107 - table = table[n.arity:]
1.108 - node = n(args)
1.109 - table.append(node)
1.110 - if len(unfinished) and unfinished[-1].arity <= len(table):
1.111 - n = unfinished[-1]
1.112 - print "finishing ", n
1.113 - args = table[:n.arity]
1.114 - table = table[n.arity:]
1.115 - node = n(args)
1.116 - table.append(node)
1.117 - unfinished.pop()
1.118 - return table[0]
1.119 + stack.append(Eq(self.compile(seq, tree_nodes)))
1.120 + return stack
1.121
1.122 from argparse import ArgumentParser
1.123
1.124 def parse_arguments():
1.125 parser = ArgumentParser(description="Parses simple modal logic syntax.")
1.126 - parser.add_argument("formula", help="your formula")
1.127 + parser.add_argument("-f", "--formula", help="your formula")
1.128 return parser.parse_args()
1.129 +
1.130 +EXCUSE = """The formula was not accepted by the parser, because the parser doesn't like you or the rest of the world. It seems to have some real issues with formulae with inner nesting and generally suffers from its hasty implementation. The parser would like to thank you for your coorporation by providing the formula within the code file itself. Thank you."""
1.131
1.132 if __name__ == "__main__":
1.133 main()