| Next | Higher-Order Parsing | 39 |
Regexes also have a * notation that means an item can be repeated
For example, we might write
# term → factor repeat( "*" factor )
$term = $FACTOR - repeat(L("*") - $FACTOR);
It's not hard to express repeat with what we have already:
# repeat($p) is:
$p - repeat($p) | $nothing
But we can wrap this up as a function:
sub repeat {
my $p = shift;
my $repeat_p;
my $do_repeat_p = sub { $repeat_p->(@_) }; # proxy
$repeat_p = alt(conc($p, $do_repeat_p), $nothing);
return $repeat_p;
}
Parse::RecDescent provides this with its (s) notation
| Next | ![]() |
![]() |
Copyright © 2007 M. J. Dominus |