pyparsing

Daniel 'Dang' Griffith noemail at noemail4u.com
Thu May 13 08:29:51 EDT 2004


On Thu, 13 May 2004 08:05:32 +0200, bostjan.jerko at mf.uni-lj.si
(Bo¹tjan Jerko) wrote:

>Hello !
>
>I am trying to understand pyparsing. Here is a little test program 
>to check Optional subclass:
>
>from pyparsing import Word,nums,Literal,Optional
>
>lbrack=Literal("[").suppress()
>rbrack=Literal("]").suppress()
>ddot=Literal(":").suppress()
>start = Word(nums+".")
>step = Word(nums+".")
>end = Word(nums+".")
>        
>sequence=lbrack+start+Optional(ddot+step)+ddot+end+rbrack
>
>tokens = sequence.parseString("[0:0.1:1]")
>print tokens
>
>tokens1 = sequence.parseString("[1:2]")
>print tokens1
>
>It works on tokens, but the error message is showed on 
>the second string ("[1:2]"). I don't get it. I did use 
>Optional for ddot and step so I guess they are optional.
>
>Any hints what I am doing wrong?
>
>The versions are pyparsing 1.1.2 and Python 2.3.3.
>
>Thanks,
>
>B.
I don't see anything "obviously" wrong to me, but changing it thusly
seems to resolve the problem (I added a few intermediate rules to 
make it more obvious):

pref = lbrack + start
midf = ddot + step
suff = ddot + end + rbrack
sequence = pref + midf + suff | pref + suff

I've run into "this kind of thing" now and again, and have always 
been able to resolve it by reorganizing my rules.

    --dang



More information about the Python-list mailing list