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

23 lines
522 B
Plaintext

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