Next | Higher-Order Parsing | 28 |
Many rules are naturally expressed in terms of "optional" items
Instead of:
term → factor ("*" term | nothing)
We might want to say something like:
term → factor optional("*" term)
We can define optional quite easily:
sub optional { my $p = shift; return alt($p, $nothing); }
Now this:
$term = $Factor - (L("*") - $Term | $nothing);
Becomes this:
$term = $Factor - optional(L("*") - $Term);
Next | Copyright © 2007 M. J. Dominus |