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-03-14 18:46:53 +01:00
|
|
|
start : additive
|
2010-03-07 20:41:02 +01:00
|
|
|
|
2010-03-14 18:46:53 +01:00
|
|
|
additive : multiplicative "+" additive { return $1 + $3; }
|
|
|
|
/ multiplicative
|
2010-03-07 20:41:02 +01:00
|
|
|
|
2010-03-14 18:46:53 +01:00
|
|
|
multiplicative : primary "*" multiplicative { return $1 * $3; }
|
|
|
|
/ primary
|
2010-03-07 20:41:02 +01:00
|
|
|
|
2010-03-14 18:46:53 +01:00
|
|
|
primary : integer
|
|
|
|
/ "(" additive ")" { return $2; }
|
2010-03-07 20:41:02 +01:00
|
|
|
|
2010-03-18 20:06:55 +01:00
|
|
|
integer "integer" : [0-9]+ { return parseInt($1.join(""), 10); }
|