http://paperlined.org/dev/perl/lexers_parsers/example1.pl
#!/usr/bin/env perl
# demonstration of a simple lexer that uses the following features of Perl:
# (?{ code }) - run a piece of code, in the middle of running a regexp
# $^N - the most recent capture group
# $^R - the return value of the last (?{ ... })
use strict;
use warnings;
use Data::Dumper;
my $str = '(3 + 2) * 5';
my @tokens;
$str =~ /
^
(
(?:
\s+ |
( \( ) (?{ [OPEN_PAREN => $^N] }) |
( \) ) (?{ [CLOSE_PAREN => $^N] }) |
( \d+ ) (?{ [NUMBER => $^N] }) |
( [-+*\/] ) (?{ [OPERATOR => $^N] })
)
(?{ push @tokens, $^R if $^R;
undef; # reset $^R
})
)*
$
/x or die "syntax error\n";
print Dumper \@tokens;
Generated by GNU enscript 1.6.4.