Skip to content

Instantly share code, notes, and snippets.

@cammckinnon
Created April 23, 2012 16:50
Show Gist options
  • Save cammckinnon/2472235 to your computer and use it in GitHub Desktop.
Save cammckinnon/2472235 to your computer and use it in GitHub Desktop.
/*
* Classic example grammar, which recognizes simple arithmetic expressions like
* "2*(3+4)". The parser generated from this grammar then computes their value.
*/
start
= additive
additive
= left:multiplicative "+" right:additive { return {op: '+', toks: [left, right]}; }
/ multiplicative
multiplicative
= left:primary "*" right:multiplicative { return {op: '*', toks: [left, right]}; }
/ primary
primary
= integer
/ "(" additive:additive ")" { return additive }
integer
= digits:[0-9]+ { return parseInt(digits.join("")) }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment