Perl 6 Perl 6 today #38

Grammars

Regexes, rules, tokens can be combined into grammars:

grammar ABC {
    token integer  { \d+ }
    token float    { \d+ '.' \d* }
    token variable { <alpha>+ }
    token term     { <float> | <integer> | <variable> }
    token addop    { '+' | '-' }

    rule expr        { <term> [ <addop> <term> ]* }
    rule if_stmt     { 'if' <expr> <stmt> [ 'else' <stmt> ]? }
    rule print_stmt  { 'print' <expr> }
    rule assign_stmt { <variable> '=' <expr> }
    rule stmt        { <if_stmt> | <print_stmt> | <assign_stmt> }

    rule TOP { <stmt>* }
}
Copyright © 2009
http://www.pmichaud.com/2009/pres/