instaparse

If you're not trampolining your parser, why bother getting up in the morning?
2019-11-26T11:52:35.027700Z

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?

aengelberg 2019-12-03T18:35:26.029300Z

@ahmadnazir 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.

2019-12-03T18:38:00.029500Z

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.

aengelberg 2019-12-03T18:38:02.029700Z

You can achieve greediness by using negative lookahead:

X := Y* !Y Z*

👍 1
aengelberg 2019-12-03T18:38:27.029900Z

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