| Next | Higher-Order Parsing | 18 |
In functional languages, we needn't write write 9 similar lookfor functions
Instead, we can have another function build them as required:
sub lookfor {
my $target = shift;
my $parser =
sub {
my $tokens = shift;
my $tok = first($tokens);
if ($tok->type eq $target) {
return ($tok->value, rest($tokens));
} else {
return; # failure
}
};
return $parser;
}
Now instead of lookfor_PLUS we just use lookfor("+")
Instead of lookfor_NUMBER we just use lookfor("NUMBER")
| Next | ![]() |
![]() |
Copyright © 2007 M. J. Dominus |