| Next | Higher-Order Parsing | 26 |
Regexes have a ? notation that means an item is optional
We might want to say something like:
term → factor optional("*" term)
We can define optional quite easily:
sub optional {
my $p = shift;
return alt($p, $nothing);
}
Now this:
$term = $FACTOR - (L("*") - $TERM | $nothing);
Becomes this:
$term = $FACTOR - optional(L("*") - $TERM);
Parse::RecDescent provides this with its (?) notation
Similarly, we can implement repeat
| Next | ![]() |
![]() |
Copyright © 2007 M. J. Dominus |