Perl 6 Compiler Design and Implementation of the Perl 6 Compiler #19

Parsers

Sets of rules can be created to quickly form parsers.

Here's a simple arithmetic expression parser:

rule ident { [ <alpha> | _ ] \w+ }
rule literal { \d+ }
rule factor {:w <literal> | <ident> | \( <expr> \) }
rule term {:w <factor> [ $<mulop>:=[<[*/]>] <factor> ]* }
rule expr {:w <term> [ $<addop>:=[<[+\-]>] <term> ]* }
continued...
Copyright © 2005 Patrick R. Michaud