'''This class is a node of a relational expression. Leaves are relations and internal nodes are operations.'''
'''This class is a node of a relational expression. Leaves are relations and internal nodes are operations.'''
def__init__(self,expression):
def__init__(self,expression):
expression=expression.strip()
print"Parsing: ",expression
'''expression must be a valid relational algrbra expression that would be accepted by the parser
'''expression must be a valid relational algrbra expression that would be accepted by the parser
andmustbeutf16'''
andmustbeutf16'''
self.kind=0
self.kind=0
@ -33,11 +36,77 @@ class node (object):
self.prop=""
self.prop=""
'''*-ᑌᑎᐅᐊᐅLEFTᐊᐅRIGHTᐊᐅFULLᐊπσρ'''
'''*-ᑌᑎᐅᐊᐅLEFTᐊᐅRIGHTᐊᐅFULLᐊπσρ'''
binary=(u"*",u"-",u"ᑌ",u"ᑎ")
unary=(u"π",u"σ",u"ρ")
'''(a ᑌ (a ᑌ b ᑌ c ᑌ d)) ᑎ c - σ i==3(πa,b(aᑌ b ᑎ c))'''
'''(a ᑌ (a ᑌ b ᑌ c ᑌ d)) ᑎ c - σ i==3(πa,b(aᑌ b ᑎ c))'''
level=0#Current parentesis level
start=-1#Start of the parentesis
end=-1#End of the parentesis.
tokens=list(expression)#Splitted expression
r=range(len(tokens))
r.reverse()
lev_non_zero_chars=0#Number of chars inside parentesis
foriinr:#Parses expression from end to begin, to preserve operation's order
iftokens[i]==u"(":
iflevel==0:
start=i
printstart
level+=1
eliftokens[i]==u")":
level-=1
iflevel==0:
end=i
printend
iflevel!=0:
lev_non_zero_chars+=1
ifi==0andlevel==0andtokens[i]inunary:#Unary operator found, must grab its parameters and its child relation they
child=""
forqintokens[start+1:end]:
child+=q
self.name=tokens[i]
print"-----",tokens[i]
print"---",start,end,lev_non_zero_chars
printchild
#print prop
#self.child=node(child)
iflevel==0andtokens[i]inbinary:#Binary operator found, everything on left will go in the left subree and everhthing on the right will go in the right subtree
self.kind=BINARY
left=""
right=""
ifstart==end==-1:#No parentesis before
end=i
forqintokens[start+1:end]:
left+=q
self.name=tokens[i]
forqintokens[i+1:]:
right+=q
print"self: ",tokens[i]
print"left: ",left
print"right:",right
self.left=node(left)
self.right=node(right)
return
foriinlist(expression):
iflev_non_zero_chars!=0andlev_non_zero_chars+1==len(expression):#Expression is entirely contained in parentesis, removing them