| Next | Higher-Order Parsing | 29 |
Many rules are naturally expressed in terms of "repeated" items
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;
}
| Next | ![]() |
![]() |
Copyright © 2007 M. J. Dominus |