Fork me on GitHub
#instaparse
<
2019-11-26
>
Ahmad Nazir Raja11:11:35

Hi, This is related to greedy behavior of instaparse. I have the following grammar:

X := Y* Z*

Y := CHAR
Z := CHAR

CHAR := ('a' | 'b' | 'c')
With input aaa I get the output shown below. I expect greedy behavior and that Y should be matched instead of Z. Does anyone have an idea to why this happens or how to enforce greedy behavior?

aengelberg18:12:26

@U82LVQ5NX sorry for the late reply, but instaparse doesn’t guarantee greediness or non-greediness; in fact, if you call insta/parses you will get every version of the parse including ones where Y is parsing some or all of the chars.

Ahmad Nazir Raja18:12:00

Yes, I tried parses and I could see all versions. For some reason I thought there would be a way to prefer one version over the other. Anyway, thanks for the response.

aengelberg18:12:02

You can achieve greediness by using negative lookahead:

X := Y* !Y Z*

👍 4
aengelberg18:12:27

this ensures that it won’t start parsing Z until it can’t parse Y anymore.