Next | Higher-Order Parsing | 10 |
Lexing is mostly a matter of simple pattern matching
Perl actually has special regex features just for this purpose
Most of which alas, I have no time to explain
It is in the book, though
Also in Friedl's book
We will assume that @tokens looks something like this:
# (x^2 + 3*x) * sin(x * 2) + 14 (["("], ["VAR", "x"], ["^"], ["NUMBER", 2], ["+"], ["NUMBER", 3], ["*"], ["VAR", "x"], [")"], ["FUNCTION", "sin"], ["("], ["VAR", "x"], ["*"], ["NUMBER", 2], [")"], ["+"], ["NUMBER", 14])
Also:
sub type { my $token = shift; $token->[0] } sub value { my $token = shift; $token->[1] }
Next | Copyright © 2007 M. J. Dominus |