improved parser stub, not completely working yet

git-svn-id: http://galileo.dmi.unict.it/svn/relational/trunk@16 014f5005-505e-4b48-8d0a-63407b615a7c
master
LtWorf 2008-07-20 17:49:44 +07:00
parent 5c9f4e63f4
commit e93e188cb6
1 changed files with 60 additions and 5 deletions

@ -16,17 +16,72 @@
# along with this program. If not, see <http://www.gnu.org/licenses/>. # along with this program. If not, see <http://www.gnu.org/licenses/>.
# #
# author Salvo "LtWorf" Tomaselli <tiposchi@tiscali.it> # author Salvo "LtWorf" Tomaselli <tiposchi@tiscali.it>
def parse(expr): def parse(expr):
'''This function parses a relational algebra expression, converting it into python, '''This function parses a relational algebra expression, converting it into python,
executable by eval function to get the result of the expression.''' executable by eval function to get the result of the expression.'''
symbols=("σ","π","ρ")
expr=expr.strip()
start=-1
#Parses the string from end to begin
for i in range(len(expr),-1,-1):
if expr[i:i+1]=="(":
start=i+1
break
if start==-1: #No complex operators
return parse_op(expr)
end=expr.find(")",start)
internal=parse(expr[start:end])
print "Internal: %s" % (internal)
endp=start-1
start=-1
symbol=""
for i in range(endp,-1,-1):
if expr[i:i+2] in symbols:
symbol=expr[i:i+2]
start=i+2
break
parameters=parse(expr[start:endp])
print "Parameters: %s"% ( parameters)
res=""
if symbol=="π":#Projection
params=""
count=0
for i in parameters.split(","):
if count!=0:
params+=","
else:
count=1
params+="\"%s\"" % (i.strip())
res="%s.projection(%s)" % (internal,params)
elif symbol== "σ": #Selection
res="%s.selection(\"%s\")" % (internal,parameters)
elif symbol=="ρ": #Rename
params=parameters.replace(",","\",\"").replace("","\",\"")
res="%s.rename(\"%s\")" % (internal,params)
print res
res= ("%s%s%s") % (expr[0:start-2],res.replace(" ",""),expr[end+1:])
print res
return parse_op(res)
#Selection σage > 25 Λ rank = weight(A) #Selection σage > 25 Λ rank = weight(A)
#Projection π a,b(A) #Projection Q ᐅᐊ π a,b(A) ᐅᐊ B
#Rename ρid➡i,name➡n(A) #Rename ρid➡i,name➡n(A)
#A B #ρid➡i,name➡n(π a,b(A))
#A ᐅᐊ B #A ᐅᐊ B
#A ᑎ B
def parse_op(expr):
'''This function parses a relational algebra expression including only operators
(not functions) and no parenthesis, converting it into python,
executable by eval function to get the result of the expression.'''
result="" result=""
symbols={} symbols={}