Files
devops/perl/Examples/Chap8/expr-parser-2.pl
2025-09-17 16:08:16 +08:00

33 lines
933 B
Perl

###
### simple-expr-parser-2.pl
###
## Chapter 8 section 4
use Parser ':all';
use Lexer ':all';
my ($expression, $term, $factor);
my $Expression = parser { $expression->(@_) };
my $Term = parser { $term ->(@_) };
my $Factor = parser { $factor ->(@_) };
$expression = alternate(concatenate($Term,
lookfor(['OP', '+']),
$Expression),
$Term);
$term = alternate(concatenate($Factor,
lookfor(['OP', '*']),
$Term),
$Factor);
$factor = alternate(lookfor('INT'),
concatenate(lookfor(['OP', '(']),
$Expression,
lookfor(['OP', ')']))
);
$entire_input = concatenate($Expression, \&End_of_Input);