make sure entire string was parsed

Paul McGuire ptmcg at
Tue Sep 13 05:42:05 CEST 2005

Steve -

Wow, this is a pretty dense pyparsing program.  You are really pushing
the envelope in your use of ParseResults, dicts, etc., but pretty much
everything seems to be working.

I still don't know the BNF you are working from, but here are some
other "shots in the dark":

1. I'm surprised func_word does not permit numbers anywhere in the
body.  Is this just a feature you have not implemented yet?  As long as
func_word does not start with a digit, you can still define one
unambiguously to allow numbers after the first character if you define
func_word as

func_word = _pp.Word(func_chars,func_chars+_pp.nums)

Perhaps similar for syn_word as well.

2. Is coord an optional sub-element of a func?  If so, you might want
to group them so that they stay together, something like:

coord_tag = _pp.Optional(_pp.Combine(coord_sep + num_word))
func_tags = _pp.ZeroOrMore(_pp.Group(tag_sep + func_word+coord_tag))

You might also add a default value for coord_tag if none is supplied,
to simplify your parse action?

coord_tag = _pp.Optional(_pp.Combine(coord_sep + num_word),None)

Now the coords and funcs will be kept together.

3. Of course, you are correct in using Combine to ensure that you only
accept adjacent characters.  But you only need to use it at the
outermost level.

4. You can use several dict-like functions directly on a ParseResults
object, such as keys(), items(), values(), in, etc.  Also, the []
notation and the .attribute notation are nearly identical, except that
[] refs on a missing element will raise a KeyError, .attribute will
always return something.  For instance, in your example, the getTag()
parse action uses dict.pop() to extract the 'coord' field.  If coord is
present, you could retrieve it using "tokens['coord']" or
"tokens.coord".  If coord is missing, "tokens['coord']" will raise a
KeyError, but tokens.coord will return an empty string.  If you need to
"listify" a ParseResults, try calling asList().

It's not clear to me what if any further help you are looking for, now
that your initial question (about StringEnd()) has been answered.  But
please let us know how things work out.

-- Paul

More information about the Python-list mailing list