Help for this page
term -> atom operator term term -> '(' term ')' term -> atom operator -> '*' | '/'| '+' | '-' atom -> \d+
term -> atom operator term ... '2' '*' ('3' '+' term) '2' '*' ('3' '+' (atom)) '2' '*' ('3' '+' ('4'))
expression -> (term add_op)* term term -> (factor mul_op)* factor ... add_op -> '+' | '-' mul_op -> '*' | '/' atom -> \d+
expression term add_op term ... (factor mulop factor) '+' ('4') ((atom) mulop (atom)) '+' ('4') (('2') '*' ('3') ) '+' ('4')
#!/usr/bin/perl use strict; ... [ 'AddOp', '+' ], [ 'Number', '4' ] ];
sub match { my $expected_token = shift; ... } return 1; }
sub expression { my @res = (term()); ... sub atom { match('Number'); }
my $parse_tree = expression(); print Dumper $parse_tree; ... '+', [ '4' ] ];
# parse tree for '2 * (3+4)': [ ... '*', [ '3', '+', '4' ], ];
my $parse_tree = expression(); match('EOF');
sub execute { my $tree = shift; ... } print execute($parse_tree), "\n";
sub make_rule { my ($operator, $next) = @_; ... *term = make_rule('MulOp', \&factor); *expression = make_rule('AddOp', \&term);
grammar Arithmetic { ... # match it: $input ~~ Grammar.expression;
rule factor { | <atom> | '(' <expression> [ ')' || <panic: Expected ')'> ] }
# this example mixes official Perl 6 syntax an PGE syntax, # I don't really know if they are 100% compatible ... rule expression is optable { ... }; # yes, '...' is valid Perl 6 ;-) proto 'term:' is tighter('infix:*') is parsed(&factor) { ... };
ModernArithmetic is Arithmetc { token infix:</> { '÷' } # different symbol, same rule name }
www.com | www.net | www.org
5.003 (Camel Book 2nd Edition) 5.6 (Camel Book 3rd Edition) 5.8 (stable version for >5 years) 5.10 (many new features now in common use) 5.16 (Camel Book 4th Edition) 5.18 (hash overhaul) 5.26 (no "." in @INC) 5.36 (subroutine signatures) Dynamic (no standard; code dictates VERSION) Other (discuss in comments)
Results (37 votes). Check out past polls.