Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non-greedy matching in Treetop/PEG?

Tags:

regex

treetop

How would I do something like this in Treetop?

/.+?;/

It seems like the only way is to do:

[^;]+ ';'

Which is kind of ugly.. any other way? .+? doesn't seem to work..

like image 724
cloudhead Avatar asked Dec 18 '22 07:12

cloudhead


1 Answers

PEGs are greedy and blind by default, that means they eat as much input as they can and they do not consider what comes afterwards:

S <- P1* P2 (greedy, blind)

That can be considerably easy fixed though by making use of the ordered choice (and without using lookaheads):

S <- P1 S / P2 (greedy, non-blind)

S <- P2 / P1 S (lazy, non-blind)

like image 111
Lukas Renggli Avatar answered Dec 31 '22 09:12

Lukas Renggli