Pretty Scheme, ??? Python

Paul McGuire ptmcg at austin.rr.com
Tue Jul 3 12:30:02 EDT 2007


On Jul 3, 11:08 am, Neil Cerutti <horp... at yahoo.com> wrote:
>
> 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

I think the problem is with your Groups, that they are wrapping the
ParseResults into a single-element list.  Try either of the following
(but not both!):
1. Remove Group from the definitions of binop and with_.

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

2. Change the parse actions to deref the 0'th element of t for binop
and with_.

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

As a troubleshooting measure, you can also create an explicit method,
and then decorate it with pyparsing's built-in @traceParseAction
decorator.

@traceParseAction
def test(t):
    return BinOp(t.op,t.lhs,t.rhs)

This should print out information on the arguments being passed to
test, and the results being returned.

-- Paul




More information about the Python-list mailing list