Next | Higher-Order Parsing | 19 |
Let's pretend for a bit that atom has only this one rule:
atom → "FUNC" "(" expression ")"
We could write atom() like this:
sub atom { my $t1 = shift; my ($expr, $t2, $t3, $t4, $t5);
if ( ($funcname, $t2) = lookfor("FUNC")->($t1) && (undef, $t3) = lookfor("(")->($t2) && ($expr, $t4) = expression($t3) && (undef, $t5) = lookfor(")")->($t4)) { my $val = something involving $funcname and $expr; return ($val, $t5); } else { return; # failure } }
Most of our parser functions would look something like this
So instead we'll write a function that assembles small parsers into big ones
Given parser functions A, B, etc.:
conc(A, B, ...)
Will return a parser function that looks for A, then B, etc.
Next | Copyright © 2007 M. J. Dominus |