diff --git a/examples/arithmetics.peg b/examples/arithmetics.peg index 71e5bb9..84059c5 100644 --- a/examples/arithmetics.peg +++ b/examples/arithmetics.peg @@ -1,21 +1,15 @@ -start : _ expression { return $2; } +start : _ additive { return $2; } -expression : additive - -additive : multiplicative (plus / minus) additive { - if ($2 === "+") { return $1 + $3; } - if ($2 === "-") { return $1 - $3; } - } +additive : multiplicative plus additive { return $1 + $3; } + / multiplicative minus additive { return $1 - $3; } / multiplicative -multiplicative : primary (times / divide) multiplicative { - if ($2 === "*") { return $1 * $3; } - if ($2 === "/") { return $1 / $3; } - } +multiplicative : primary times multiplicative { return $1 * $3; } + / primary divide multiplicative { return $1 / $3; } / primary primary : integer - / lparen expression rparen { return $2 } + / lparen additive rparen { return $2 } integer "integer" : [0-9]+ _ { return parseInt($1.join("")); }