如何使用pyparsing获取匹配令牌的位置?

时间:2012-03-08 14:12:18

标签: python pyparsing

到目前为止,我已尝试使用'setParseAction'来获取匹配令牌的位置,但它有时无法正常工作。以此代码为例:

>>> from pyparsing import *
>>> 
>>> Z = Literal('0')
>>> POINT = Literal('.')
>>> BIN_DIGITS = Word('01')
>>> OCT_DIGITS = Word('01234567')
>>> DEC_DIGITS = Word('0123456789')
>>> HEX_DIGITS = Word('0123456789abcdefABCDEF')
>>> DEC_INT = DEC_DIGITS.setParseAction(lambda t: int(t[0]))
>>> BIN_INT = Combine(Z + ((Literal('b') | 'B')) + BIN_DIGITS).\
...         setParseAction(lambda t: int(t[0], 2))
>>> OCT_INT = Combine(Z + ((Literal('o') | 'O')) + OCT_DIGITS).\
...         setParseAction(lambda t: int(t[0], 8))
>>> HEX_INT = Combine(Z + ((Literal('x') | 'X')) + HEX_DIGITS).\
...         setParseAction(lambda t: int(t[0], 16))
>>> INTEGER = HEX_INT | OCT_INT | BIN_INT | DEC_INT
>>> EXP = Combine(CaselessLiteral('E') + Optional(Literal('+') | '-') + DEC_INT)
>>> POINT_FLOAT = Combine(Optional(DEC_INT) + POINT + DEC_INT) | \
...         Combine(DEC_INT + POINT)
>>> EXP_FLOAT = Combine(DEC_INT + EXP) | Combine(POINT_FLOAT + EXP)
>>> FLOAT = (EXP_FLOAT | POINT_FLOAT).setParseAction(lambda t: float(t[0]))
>>> 
>>> 
>>> def p(s, l, t):
...     print 'Location of %s:  %s' % (t[0], l,)
... 
>>> 
>>> NUMBER = (FLOAT | INTEGER).setParseAction(p)
>>> NUMBER.parseString('    12345')
Location of 12345:  0
([12345], {})
>>> NUMBER.parseString(' 12345')
Location of 12345:  0
([12345], {})
>>> NUMBER.parseString('12345')
Location of 12345:  0
([12345], {})

无论我在字符串中放置数字'12345',位置始终为0。但是,如果我尝试:

>>> LITERAL = Literal('someword').setParseAction(p)
>>> LITERAL.parseString('    someword')
Location of someword:  4
(['someword'], {})
>>> LITERAL.parseString(' someword')
Location of someword:  1
(['someword'], {})
>>> LITERAL.parseString('someword')
Location of someword:  0
(['someword'], {})

它按预期工作。我在第一个例子中做错了什么?

1 个答案:

答案 0 :(得分:0)

好的,感谢Paul的提示,我通过将'Or'表达式与一个被抑制的空格表达式结合起来并使用它来解析字符串来解决这个问题。下面是最终结果:

>>> from pyparsing import *
>>> 
>>> def p(s,l,t):
...     print 'Location of %s: %s' % (s, l,)
... 
>>> Z = Literal('0')
>>> POINT = Literal('.')
>>> BIN_DIGITS = Word('01')
>>> OCT_DIGITS = Word('01234567')
>>> DEC_DIGITS = Word('0123456789')
>>> HEX_DIGITS = Word('0123456789abcdefABCDEF')
>>> DEC_INT = DEC_DIGITS.copy().setParseAction(lambda t: int(t[0]))
>>> BIN_INT = Combine(Z + ((Literal('b') | 'B')) + BIN_DIGITS).\
...         setParseAction(lambda t: int(t[0], 2))
>>> OCT_INT = Combine(Z + ((Literal('o') | 'O')) + OCT_DIGITS).\
...         setParseAction(lambda t: int(t[0], 8))
>>> HEX_INT = Combine(Z + ((Literal('x') | 'X')) + HEX_DIGITS).\
...         setParseAction(lambda t: int(t[0], 16))
>>> INTEGER = HEX_INT | OCT_INT | BIN_INT | DEC_INT
>>> EXP = Combine(CaselessLiteral('E') + Optional(Literal('+') | Literal('-')) + DEC_DIGITS)
>>> POINT_FLOAT = Combine(Optional(DEC_DIGITS) + POINT + DEC_DIGITS) | \
...         Combine(DEC_DIGITS + POINT)
>>> EXP_FLOAT = Combine(DEC_DIGITS + EXP) | Combine(POINT_FLOAT + EXP)
>>> FLOAT = (EXP_FLOAT | POINT_FLOAT).setParseAction(lambda t: float(t[0]))
>>> NUMBER = (FLOAT | INTEGER).setParseAction(p)
>>> NUMBER2 = Combine(ZeroOrMore(White(exact=1)).suppress() + NUMBER)
>>> 
>>> NUMBER2.parseString('    12345')
Location of     12345: 4
(['12345'], {})