Last active
May 18, 2017 11:57
Star
You must be signed in to star a gist
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