问题
>>> 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:
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.
来源:https://stackoverflow.com/questions/5777727/why-is-ordered-choice-in-pyparsing-failing-for-my-use-case