Simplified the example arithmetics grammar.

redux
David Majda 15 years ago
parent d810580232
commit eade133ea6

@ -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("")); }

Loading…
Cancel
Save