Next | Higher-Order Parsing | 14 |
The next simplest is a parser that looks for a particular token:
sub lookfor_PLUS { my $tokens = shift; my $tok = first($tokens); if (type($tok) eq "+") { return ("+", rest($tokens)); } else { return; # failure } }
sub lookfor_NUMBER { my $tokens = shift; my $tok = first($tokens); if (type($tok) eq "NUMBER") { return (value($tok), rest($tokens)); } else { return; # failure } }
Note that the "value" returned by lookfor_NUMBER is the value of the number token it finds
Next | Copyright © 2007 M. J. Dominus |