first commit
This commit is contained in:
42
perl/Examples/Chap8/expr-parser.pl
Normal file
42
perl/Examples/Chap8/expr-parser.pl
Normal file
@@ -0,0 +1,42 @@
|
||||
|
||||
|
||||
###
|
||||
### simple-expr-parser.pl
|
||||
###
|
||||
|
||||
## Chapter 8 section 4
|
||||
|
||||
use Parser ':all';
|
||||
use Lexer ':all';
|
||||
|
||||
my $expression;
|
||||
my $Expression = parser { $expression->(@_) };
|
||||
$expression = alternate(concatenate(lookfor('INT'),
|
||||
lookfor(['OP', '+']),
|
||||
$Expression),
|
||||
concatenate(lookfor('INT'),
|
||||
lookfor(['OP', '*']),
|
||||
$Expression),
|
||||
concatenate(lookfor(['OP', '(']),
|
||||
$Expression,
|
||||
lookfor(['OP', ')'])),
|
||||
lookfor('INT'));
|
||||
my $entire_input = concatenate($Expression, \&End_of_Input);
|
||||
my @input = q[2 * 3 + (4 * 5)];
|
||||
my $input = sub { return shift @input };
|
||||
my $lexer = iterator_to_stream(
|
||||
make_lexer($input,
|
||||
['TERMINATOR', qr/;\n*|\n+/ ],
|
||||
['INT, qr/\d+/ ],
|
||||
['PRINT', qr/\bprint\b/ ],
|
||||
['IDENTIFIER', qr|[A-Za-z_]\w*| ],
|
||||
['OP', qr#\*\*|[-=+*/()]# ],
|
||||
['WHITESPACE', qr/\s+/, sub { "" } ],
|
||||
)
|
||||
);
|
||||
if (my ($result, $remaining_input) = $entire_input->($lexer)) {
|
||||
use Data::Dumper;
|
||||
print Dumper($result), "\n";
|
||||
} else {
|
||||
warn "Parse error.\n";
|
||||
}
|
||||
Reference in New Issue
Block a user