pegjs/examples/arithmetics.pegjs

18 lines
544 B
Plaintext
Raw Normal View History

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