creation of the tree completed

git-svn-id: http://galileo.dmi.unict.it/svn/relational/trunk@130 014f5005-505e-4b48-8d0a-63407b615a7c
This commit is contained in:
LtWorf 2009-04-28 18:40:53 +00:00
parent 0ff9ed22a0
commit 92b5e55a75

View File

@ -34,10 +34,13 @@ class node (object):
def __init__(self,expression): def __init__(self,expression):
'''Generates the tree from the tokenized expression''' '''Generates the tree from the tokenized expression'''
if len(expression)==1: if len(expression)==1:
print "Relation: ",expression[0] if expression[0].__class__==[].__class__: #We have a list!
self.kind=RELATION expression=expression[0]
self.name=expression[0] if len(expression)==1 and expression[0].__class__=="".__class__: #We have a string!
return print "Relation: ",expression[0]
self.kind=RELATION
self.name=expression[0]
return
for i in range(len(expression)-1,-1,-1): #Expression from right to left for i in range(len(expression)-1,-1,-1): #Expression from right to left
if expression[i] in b_operators: #Binary operator if expression[i] in b_operators: #Binary operator
print "Operator: ",expression[i] print "Operator: ",expression[i]
@ -49,8 +52,18 @@ class node (object):
self.left=node(expression[:i]) self.left=node(expression[:i])
self.right=node(expression[i+1:]) self.right=node(expression[i+1:])
return return
elif expression[i] in u_operators: #Unary operator for i in range(len(expression)-1,-1,-1): #Expression from right to left
if expression[i] in u_operators: #Unary operator
self.kind=UNARY self.kind=UNARY
self.name=expression[i]
self.prop=expression[1+i]
self.child=node(expression[2+i])
print "Operator: ",expression[i]
print "prop: ",expression[1+i]
print "child: ",self.child
return return
pass pass
@ -58,7 +71,7 @@ class node (object):
if (self.kind==RELATION): if (self.kind==RELATION):
return self.name return self.name
elif (self.kind==UNARY): elif (self.kind==UNARY):
return self.name + " "+ self.prop+ " (" + self.child +")" return self.name + " "+ self.prop+ " (" + self.child.__str__() +")"
elif (self.kind==BINARY): elif (self.kind==BINARY):
if self.left.kind==RELATION: if self.left.kind==RELATION:
le=self.left.__str__() le=self.left.__str__()
@ -162,8 +175,9 @@ if __name__=="__main__":
#n=node(u"π a,b (d-a*b)") #n=node(u"π a,b (d-a*b)")
#print n.__str__() #print n.__str__()
#a= tokenize("((a b) - c d) ᐅRIGHTᐊ a * (π a,b (a))") a= tokenize("(a - (a b) * π a,b (a-b)) - ρ 123 (a)")
a=tokenize("a*b-c") #a= tokenize(u"π a,b (a*b)")
#a=tokenize("(a-b*c)*(b-c)")
print a print a
print node(a) print node(a)
#print tokenize("(a)") #print tokenize("(a)")