Last active
May 18, 2017 11:57
-
-
Save grondilu/f1b52ab99dcfd503b09ef6041e641531 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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