Simplified the example arithmetics grammar once more + addded comment.

redux
David Majda 15 years ago
parent 8f3fa8436d
commit 9d239db2d6

@ -1,25 +1,19 @@
start : _ additive { return $2; }
/*
* Classic example grammar, which recognizes simple arithmetic expressions like
* "2*(3+4)". The parser generated from this grammar then computes their value.
*/
additive : multiplicative plus additive { return $1 + $3; }
/ multiplicative minus additive { return $1 - $3; }
start : additive
additive : multiplicative "+" additive { return $1 + $3; }
/ multiplicative "-" additive { return $1 - $3; }
/ multiplicative
multiplicative : primary times multiplicative { return $1 * $3; }
/ primary divide multiplicative { return $1 / $3; }
multiplicative : primary "*" multiplicative { return $1 * $3; }
/ primary "/" multiplicative { return $1 / $3; }
/ primary
primary : integer
/ lparen additive rparen { return $2 }
integer "integer"
: [0-9]+ _ { return parseInt($1.join("")); }
plus : "+" _ { return $1; }
minus : "-" _ { return $1; }
times : "*" _ { return $1; }
divide : "/" _ { return $1; }
lparen : "(" _
rparen : ")" _
/ "(" additive ")" { return $2; }
_ "whitespace"
: [ \t\n\r]*
integer "integer" : [0-9]+ { return parseInt($1.join("")); }

Loading…
Cancel
Save