Why is ordered choice in pyparsing failing for my use case?

不问归期 提交于 2019-12-23 03:06:17

问题


>>> 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!