2010-03-14 18:46:53 +01:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
2010-05-23 15:56:57 +02:00
|
|
|
start
|
|
|
|
= additive
|
2010-03-07 20:41:02 +01:00
|
|
|
|
2010-05-23 15:56:57 +02:00
|
|
|
additive
|
2010-05-31 12:40:24 +02:00
|
|
|
= left:multiplicative "+" right:additive { return left + right; }
|
2010-05-23 15:56:57 +02:00
|
|
|
/ multiplicative
|
2010-03-07 20:41:02 +01:00
|
|
|
|
2010-05-23 15:56:57 +02:00
|
|
|
multiplicative
|
2010-05-31 12:40:24 +02:00
|
|
|
= left:primary "*" right:multiplicative { return left * right; }
|
2010-05-23 15:56:57 +02:00
|
|
|
/ primary
|
2010-03-07 20:41:02 +01:00
|
|
|
|
2010-05-23 15:56:57 +02:00
|
|
|
primary
|
|
|
|
= integer
|
2010-05-31 12:40:24 +02:00
|
|
|
/ "(" additive:additive ")" { return additive; }
|
2010-03-07 20:41:02 +01:00
|
|
|
|
2010-05-23 15:56:57 +02:00
|
|
|
integer "integer"
|
2012-12-01 16:29:41 +01:00
|
|
|
= digits:$[0-9]+ { return parseInt(digits, 10); }
|