make sure entire string was parsed

Paul McGuire ptmcg at austin.rr.com
Sun Sep 11 17:57:07 EDT 2005


Steve -

>>I have to differentiate between:
>>   (NP -x-y)
>>and:
>>   (NP-x -y)
>>I'm doing this now using Combine.  Does that seem right?

If your word char set is just alphanums+"-", then this will work
without doing anything unnatural with leaveWhitespace:

from pyparsing import *

thing = Word(alphanums+"-")
LPAREN = Literal("(").suppress()
RPAREN = Literal(")").suppress()
node = LPAREN + OneOrMore(thing) + RPAREN

print node.parseString("(NP -x-y)")
print node.parseString("(NP-x -y)")

will print:

['NP', '-x-y']
['NP-x', '-y']


Your examples helped me to see what my operator precedence concern was.
 Fortunately, your usage was an And, composed using '+' operators.  If
your construct was a MatchFirst, composed using '|' operators, things
aren't so pretty:

print 2 << 1 | 3
print 2 << (1 | 3)

7
16

So I've just gotten into the habit of parenthesizing anything I load
into a Forward using '<<'.

-- Paul




More information about the Python-list mailing list