Skip to content

Instantly share code, notes, and snippets.

@grondilu
Last active June 14, 2017 10:56
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/8353432388ad8e0da44ff8bc78daca50 to your computer and use it in GitHub Desktop.
Save grondilu/8353432388ad8e0da44ff8bc78daca50 to your computer and use it in GitHub Desktop.
# https://en.wikipedia.org/wiki/Parsing_expression_grammar
grammar Arithmetic {
rule TOP { ^^ <Exp> $$ }
rule Exp { <Sum> }
rule Sum { <Product>+ % <[+-]> }
rule Product { <Value>+ % <[*/]> }
rule Value { <ident>+ | '('<Exp>')' }
}
say Arithmetic.parse($_) for
'a',
'a+b',
'a*b',
'(a-b)*(a+b)',
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment