Syntax tree construction.
4 Author: Eugen Sawin <sawine@informatik.uni-freiburg.de>
22 class Operator(object):
23 def __init__(self, id):
30 def __init__(self, values):
31 Operator.__init__(self, NOT)
32 self.value = values[0]
38 def __init__(self, values):
39 Operator.__init__(self, AND)
44 def __init__(self, values):
45 Operator.__init__(self, OR)
51 Operator.__init__(self, IMP)
56 Operator.__init__(self, EQ)
60 def __init__(self, values):
61 Operator.__init__(self, BOX)
62 self.value = values[0]
64 class Diamond(Operator):
67 Operator.__init__(self, DIAMOND)
69 class Variable(Operator):
71 def __init__(self, values):
72 Operator.__init__(self, VARIABLE)
76 def __init__(self, values):
77 Operator.__init__(self, LB)
81 def __init__(self, values):
82 Operator.__init__(self, RB)
84 class Formula(object):
89 TOKENS = {re.compile("\("): Lb,
94 re.compile("->"): Imp,
95 re.compile("<->"): Eq,
96 re.compile("\[\]"): Box,
97 re.compile("<>"): Diamond,
98 re.compile("[a-z]+"): Variable}
100 class Scanner(object):
101 def __init__(self, tokens, source=None):
105 while self.i < len(self.source):
106 for p, token in self.tokens.iteritems():
107 m = p.match(self.source[self.i:])
111 return (token, value)
114 def reset(self, source):
129 SYNTAX = ((Variable,),
140 def __init__(self, parent, value):
144 def addChild(self, value):
145 self.children.append(self, node)
147 class Parser(object):
148 def __init__(self, syntax, scanner):
150 self.scanner = scanner
151 def parse(self, source):
152 table = {(S, Variable): 0,
161 (A, Variable, And): 5,
162 (A, Variable, Or): 6,
163 (A, Variable, Imp): 7,
164 (A, Variable, Eq): 8}
168 self.scanner.reset(source)
169 (token, value) = self.scanner.next()
170 (lookahead, _) = self.scanner.next()
172 while token and len(stack):
176 #print "token ", token
177 #print "lookahead ", lookahead
179 trash.append(stack.pop())
181 (lookahead, _) = self.scanner.next()
184 if (top, token) in table:
185 action = table[(top, token)]
186 elif (top, token, lookahead) in table:
187 action = table[(top, token, lookahead)]
188 #print "action ", action, " replace ", self.syntax[action]
194 stack.extend(reversed(self.syntax[action]))
195 accepted = accepted and not len(stack)
196 return (accepted, out, trash)
200 args = parse_arguments()
201 scanner = Scanner(TOKENS)
202 parser = Parser(SYNTAX, scanner)
203 (accepted, out, trash) = parser.parse(args.formula)
204 print (accepted, out, trash)
207 for t in reversed(trash):
210 print "arity ", t.arity
211 print "table ", table
212 if len(table) < t.arity:
215 args = table[:t.arity]
217 table = table[t.arity:]
222 if len(unfinished) and unfinished[-1].arity <= len(table):
223 args = table[:unfinished[-1].arity]
224 print "unfinished args ", args
225 table = table[unfinished[-1].arity:]
226 node = unfinished[-1](args)
227 print "unfinished node ", node
233 from argparse import ArgumentParser
235 def parse_arguments():
236 parser = ArgumentParser(description="Parses simple modal logic syntax.")
237 parser.add_argument("formula", help="your formula")
238 return parser.parse_args()
240 if __name__ == "__main__":