Skip to content

Instantly share code, notes, and snippets.

@quadrupleslap
Created April 24, 2018 15:17
Show Gist options
  • Save quadrupleslap/21afbc2dd4bc8857d6cef8e4c0302886 to your computer and use it in GitHub Desktop.
Save quadrupleslap/21afbc2dd4bc8857d6cef8e4c0302886 to your computer and use it in GitHub Desktop.
Questionable math expression grammar.
defn =_{ soi ~ variable ~ "=" ~ expr ~ eoi }
main =_{ soi ~ expr ~ eoi }
add = { "+" }
sub = { "-" }
mul = { "*" }
div = { "/" }
rem = { "%" }
exp = { "^" }
atom = { function ~ "(" ~ expr ~ ")"
| "(" ~ expr ~ ")"
| float
| variable
| "i"
}
fact = { atom ~ (exp ~ term)? }
join = { fact+ }
term = { (add | sub)* ~ join }
prod = { term ~ ((mul | div | rem) ~ term)* }
expr = { prod ~ ((add | sub) ~ prod)* }
alpha = { 'a'..'z' | 'A'..'Z' }
digit = { '0'..'9' }
float =@{ digit+ ~ ("." ~ digit+)? }
variable =@{ alpha }
function =@{ alpha ~ (alpha | digit)* }
whitespace = _{ " " }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment