You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
pegjs/examples/arithmetics.pegjs

20 lines
675 B
JavaScript

/*
* Classic example grammar, which recognizes simple arithmetic expressions like
* "2*(3+4)". The parser generated from this grammar then computes their value.
*/
start : additive
additive : multiplicative "+" additive { return $1 + $3; }
/ multiplicative "-" additive { return $1 - $3; }
/ multiplicative
multiplicative : primary "*" multiplicative { return $1 * $3; }
/ primary "/" multiplicative { return $1 / $3; }
/ primary
primary : integer
/ "(" additive ")" { return $2; }
integer "integer" : [0-9]+ { return parseInt($1.join("")); }