Pretty Scheme, ??? Python

Neil Cerutti horpner at yahoo.com
Tue Jul 3 12:08:02 EDT 2007


On 2007-07-03, Paul McGuire <ptmcg at austin.rr.com> wrote:
> Here is my alternative solution (not using results names).  I used
> the base WAE class to accept the tokens as the initialization var,
> then unpack the list into variables in each respective calc()
> method.  I did not see the need for a subst() method. 

I used recursion to effect substitution of id's with their
values, whereas you chose to use a dictionary of stacks. I prefer
the functional solution for being simpler, and state-less.

Thanks for the nice example. I think I've seen enough to create
something satisfying.

There seems to be a bug in my current grammar. The factory for
BinOp is not getting the correct named results. Can you see
something I've messed up?

LPAR = Literal("(").suppress()
RPAR = Literal(")").suppress()

wae = Forward()
num = Word(nums).setResultsName('number')
id_ = Word(alphas).setResultsName('name')

binop = Group( LPAR + oneOf("+ -").setResultsName('op') + 
        wae.setResultsName('lhs') + wae.setResultsName('rhs') + RPAR )
with_ = Group( LPAR + "with" + LPAR + id_.setResultsName('bound_id') + 
        wae.setResultsName('named_expr') + RPAR + 
        wae.setResultsName('bound_body') + RPAR )

wae << (binop | with_ | num | id_)

num.setParseAction(lambda t: Num(int(t.number)))
id_.setParseAction(lambda t: Id(t.name))
binop.setParseAction(lambda t: BinOp(t.op, t.lhs, t.rhs))
with_.setParseAction(lambda t: With(t.bound_id, t.named_expr, t.bound_body))

def parse(s):
    return (wae + StringEnd()).parseString(s).asList()[0]

For test case:

 '(+ 3 45)'

I get:

C:\WINNT\system32\cmd.exe /c python wae.py
**********************************************************************
File "wae.py", line 6, in __main__
Failed example:
    parse('(+ 3 45)')
Exception raised:
    Traceback (most recent call last):
      File "c:\edconn32\python25\lib\doctest.py", line 1212, in __run
        compileflags, 1) in test.globs
      File "<doctest __main__[1]>", line 1, in <module>
        parse('(+ 3 45)')
      File "wae.py", line 122, in parse
        return (wae + StringEnd()).parseString(s).asList()[0]
      File "C:\edconn32\Python25\Lib\site-packages\pyparsing.py", line 906, in p
arseString
        loc, tokens = self._parse( instring.expandtabs(), 0 )
      File "C:\edconn32\Python25\Lib\site-packages\pyparsing.py", line 784, in _
parseNoCache
        loc,tokens = self.parseImpl( instring, preloc, doActions )
      File "C:\edconn32\Python25\Lib\site-packages\pyparsing.py", line 1961, in
parseImpl
        loc, resultlist = self.exprs[0]._parse( instring, loc, doActions, callPr
eParse=False )
      File "C:\edconn32\Python25\Lib\site-packages\pyparsing.py", line 784, in _
parseNoCache
        loc,tokens = self.parseImpl( instring, preloc, doActions )
      File "C:\edconn32\Python25\Lib\site-packages\pyparsing.py", line 2204, in
parseImpl
        return self.expr._parse( instring, loc, doActions, callPreParse=False )
      File "C:\edconn32\Python25\Lib\site-packages\pyparsing.py", line 784, in _
parseNoCache
        loc,tokens = self.parseImpl( instring, preloc, doActions )
      File "C:\edconn32\Python25\Lib\site-packages\pyparsing.py", line 2070, in
parseImpl
        ret = e._parse( instring, loc, doActions )
      File "C:\edconn32\Python25\Lib\site-packages\pyparsing.py", line 810, in _
parseNoCache
        tokens = fn( instring, tokensStart, retTokens )
      File "C:\edconn32\Python25\Lib\site-packages\pyparsing.py", line 658, in t
mp
        return f(t)
      File "wae.py", line 118, in <lambda>
        binop.setParseAction(lambda t: BinOp(t.op, t.lhs, t.rhs))
      File "wae.py", line 73, in __init__
        '-': (operator.sub, 'Sub')}[op]
    KeyError: ''
**********************************************************************

op ought to be '+' or '-'. In fact, testing showed than none of
the result names for binop are being set correctly.

-- 
Neil Cerutti
The word "genius" isn't applicable in football. A genius is a guy like Norman
Einstein. --Joe Theisman



More information about the Python-list mailing list