Parrot/PGE, parsers Parsers, Perl 6 Rules, and the Parrot Grammar Engine #9

Example

simple.pg:

grammar Simple::Grammar ;

token variable { [ <alpha> | _ ] \w* }
token number { \d+ }

token addop { \+ | - }
token mulop { \* | / }

rule expression { <addterm> [ <addop> <addterm> ]* }
rule addterm { <multerm> [ <mulop> <multerm> ]* }
rule multerm { <variable> | <number> | \( <expression> \) }

(Note: we cover "token" vs. "rule" a bit later.)

Copyright © 2006 Patrick Michaud