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?@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.
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.
You can achieve greediness by using negative lookahead:
X := Y* !Y Z*
this ensures that it won’t start parsing Z until it can’t parse Y anymore.