Next | Higher-Order Parsing | 16 |
Let's pretend for a moment 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 = ... $funcname ... $expr ...; return ($val, $t5); } else { return; # failure } }
But many other functions would also follow this pattern
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 |