a parser/scheme
David Grenier
grenieda at hotmail.com
Thu Nov 15 15:28:33 EST 2001
I'm trying to build a parser for a class I designed that works likes cons in
scheme. I just can't figure out how to recrusively do it with those
functions definitions:
from types import *
class cons:
def __init__(self,carpart,cdrpart):
self.car = carpart
self.cdr = cdrpart
def __str__(self):
a='(' + str(self.car)
champ=self.cdr
while type(champ) is InstanceType:
a=a + ' ' + str(champ.car)
champ=champ.cdr
if type(champ) is ListType:
return a + ')'
else:
a=a + ' ' + str(champ) + ')'
return a
def __repr__(self):
return str(self)
def parse(stringexp):
flatexp = split(replace(replace(stringexp,'(',' ( '),')',' ) '))
(remainder,res) = parselist(flatexp+[')'])
if remainder!=[]:
raise SchemeError('remains something! ' + join(remainder,' '))
return res
def parselist(flatexp, previous_res=[])
Here I can't figure out what goes int. With the previous function I get to
tokeniz my string into the individual parts like: (+ a b) i get a list which
is ['(', '+', 'a', 'b', ')'] Then I'm supposed with parselist to produce the
corresponding scheme list which would be something like cons('(', cons('+',
cons('a', cons('b', cons(')',[]))))) now I got to make that recursive method
in order to be able to trade things like that: (+ a (+ b a)) in order to
have (+ b a) parsed before it is put into the list as a single element. I'm
really desesperate, I tried to figure this out for about an hour and can't
get through it.
More information about the Python-list
mailing list