Replies: 2 comments 1 reply
-
I made a mistake in my description.
In short, I get an extra match on "comparison". Updated above. |
Beta Was this translation helpful? Give feedback.
-
The last comparison is a successful match of comparison, it just comes as part of a failed match on chain. I made some changes to your code that make this process clearer (and might be easier to do than adding parse actions to trace the parsing behavior): exp_comparison = pp.Word(pp.alphanums) + pp.Word(pp.alphanums)
exp_comparison.set_parse_action(_comparison_action)
exp_comparison.set_name("exp_comparison").set_debug() # <-- added
exp_compchain = exp_comparison('comparison') + exp_chain('chain')
exp_compchain.set_parse_action(_compchain_action)
exp_compchain.set_name("exp_compchain").set_debug() # <-- added which generates this output:
Right now, the only way to conditionalize the comparison parse action based on whether the chain will match will be to do a lookahead using FollowedBy: exp_compchain = pp.FollowedBy(
exp_comparison().set_parse_action(None) + exp_chain().set_parse_action(None)
) + exp_comparison('comparison') + exp_chain('chain') I had to create copies of exp_comparison and exp_chain with no parse actions to put in the lookahead |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Can someone explain why this behaves the way it does?
It prints:
I'm expecting:
It seems to be calling the parse actions on failed matches.
Beta Was this translation helpful? Give feedback.
All reactions