Why is ordered choice in pyparsing failing for my

2019-06-07 09:43发布

>>> g = MatchFirst( Literal("scoobydoo"), Literal("scooby") )
>>> g.parseString( "scooby" )
pyparsing.ParseException: Expected "scoobydoo" (at char 0), (line:1, col:1)

Is the ParseException thrown because the scooby has already been consumed in the character stream & thus the parser cannot backtrack ? I'm looking for a detailed implementation explanation for this.

At the moment, I consider this a bug because why would the parser short-circuit the matching since it has not search all the choices in production rule.

UPDATE:

Seems like MatchFirst is not exactly equivalent to | operator. Why ?

>>> g = Literal("scoobydoo") | Literal("scooby")
>>> g.parseString("scooby").asList()
['scooby']
>>> g.parseString("scoobydoo").asList()
['scoobydoo']

1条回答
贼婆χ
2楼-- · 2019-06-07 10:15

MatchFirst (or '|') does short-circuiting by design. To force all alternatives to be checked, use Or (or '^'). oneOf("scooby scoobydoo") will also work, since oneOf will short-circuit, but only after rearranging alternative words that have leading overlaps.

查看更多
登录 后发表回答