Browse Source

Simplified the example arithmetics grammar once more + addded comment.

redux
David Majda 12 years ago
parent
commit
9d239db2d6
  1. 34
      examples/arithmetics.pegjs

34
examples/arithmetics.pegjs

@ -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; }
/ multiplicative
start : additive
multiplicative : primary times multiplicative { return $1 * $3; }
/ primary divide multiplicative { return $1 / $3; }
/ primary
additive : multiplicative "+" additive { return $1 + $3; }
/ multiplicative "-" additive { return $1 - $3; }
/ multiplicative
primary : integer
/ lparen additive rparen { return $2 }
multiplicative : primary "*" multiplicative { return $1 * $3; }
/ primary "/" multiplicative { return $1 / $3; }
/ primary
integer "integer"
: [0-9]+ _ { return parseInt($1.join("")); }
primary : integer
/ "(" additive ")" { return $2; }
plus : "+" _ { return $1; }
minus : "-" _ { return $1; }
times : "*" _ { return $1; }
divide : "/" _ { return $1; }
lparen : "(" _
rparen : ")" _
_ "whitespace"
: [ \t\n\r]*
integer "integer" : [0-9]+ { return parseInt($1.join("")); }
Loading…
Cancel
Save