pegjs/examples/arithmetics.pegjs

23 lines
522 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
= left:multiplicative "+" right:additive { return left + right; }
/ multiplicative
2010-03-07 20:41:02 +01:00
multiplicative
= left:primary "*" right:multiplicative { return left * right; }
/ primary
2010-03-07 20:41:02 +01:00
primary
= integer
/ "(" additive:additive ")" { return additive; }
2010-03-07 20:41:02 +01:00
integer "integer"
= digits:$[0-9]+ { return parseInt(digits, 10); }