Skip to content

Instantly share code, notes, and snippets.

@grondilu
Last active May 18, 2017 11:57
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save grondilu/f1b52ab99dcfd503b09ef6041e641531 to your computer and use it in GitHub Desktop.
# inspired from L<http://www.engr.mun.ca/~theo/Misc/exp_parsing.htm>
grammar Algebra {
rule TOP { ^^<e>$$ }
token e { <t>+ % <[+-]> }
token t { <f>+ % <[*/]> }
token f { <p>[\^<f>]? }
token p { <v> | \(<e>\) | \-<t> }
token v { <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)
x+y a*b+c a*b*c a*(b+c)*d
>;
nok Algebra.parse($_), $_ for
<1++2 1+( 3+() a(b+c)
>;
# vim: ft=perl6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment