| Next | Higher-Order Parsing | 20 |
Atoms come in three varieties, not just one:
atom → NUMBER | VAR | function "(" expression ")"
So we need the atom parser to try these three different things
It fails only if the upcoming tokens match none of them
Something like this:
sub atom {
my $in = shift;
my ($result, $out);
my $alt3 = conc(lookfor("FUNC"),
lookfor("("), $EXPRESSION, lookfor(")"),
);
if ( ($result, $out) = lookfor("NUMBER")->($in)) {
return ($result, $out);
} elsif (($result, $out) = lookfor("VAR")->($in)) {
return ($result, $out);
} elsif (($result, $out) = $alt3->($in)) {
return ($result, $out);
} else {
return;
}
}
But again, many functions will follow this same pattern
So instead we'll write a function that assembles small parsers into big ones
Given parser functions A, B, etc.:
alt(A, B, ...)
Will return a parser function that looks for A or for B, etc.
| Next | ![]() |
![]() |
Copyright © 2007 M. J. Dominus |