Skip to content

Instantly share code, notes, and snippets.

@grondilu
Last active May 18, 2017 10:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save grondilu/09e443f28f322432752543d7eeb55677 to your computer and use it in GitHub Desktop.
Save grondilu/09e443f28f322432752543d7eeb55677 to your computer and use it in GitHub Desktop.
grammar Algebra {
rule TOP { ^^ <expression> $$ }
rule expression { <+symbol+sum+product> }
rule sum {
| <symbol>\+<expression>
}
rule product {
| <symbol>\*<symbol>
| <symbol>\*\(<sum>\)
| \(<sum>\)\*<symbol>
| <product>\*<symbol>
}
token symbol { <ident> | <number> }
token number { <.integer><.fraction>? }
token fraction { \.<.digit>+ }
token integer { <[+-]>? 0 | <[1..9]><.digit>* }
}
use Test;
plan *;
ok Algebra.parse($_), $_ for
<1 12 2 pi 1+2 1+2+3 2*2 3*(2+1)
(1+1)*2
(1+1)*2*3
>;
# vim: ft=perl6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment