Skip to content

Commit

Permalink
Update calculator example
Browse files Browse the repository at this point in the history
  • Loading branch information
zaach committed Dec 18, 2010
1 parent 0d9b570 commit fb0474b
Showing 1 changed file with 18 additions and 17 deletions.
35 changes: 18 additions & 17 deletions examples/calculator.jison
Expand Up @@ -5,21 +5,21 @@
%lex
%%

\s+ {/* skip whitespace */}
[0-9]+("."[0-9]+)?\b {return 'NUMBER';}
"*" {return '*';}
"/" {return '/';}
"-" {return '-';}
"+" {return '+';}
"^" {return '^';}
"!" {return '!';}
"%" {return '%';}
"(" {return '(';}
")" {return ')';}
"PI" {return 'PI';}
"E" {return 'E';}
<<EOF>> {return 'EOF';}
. {return 'INVALID';}
\s+ /* skip whitespace */
[0-9]+("."[0-9]+)?\b return 'NUMBER'
"*" return '*'
"/" return '/'
"-" return '-'
"+" return '+'
"^" return '^'
"!" return '!'
"%" return '%'
"(" return '('
")" return ')'
"PI" return 'PI'
"E" return 'E'
<<EOF>> return 'EOF'
. return 'INVALID'

/lex

Expand All @@ -38,7 +38,8 @@

expressions
: e EOF
{console.log($1); return $1;}
{ typeof console !== 'undefined' ? console.log($1) : print($1);
return $1; }
;

e
Expand All @@ -54,7 +55,7 @@ e
{$$ = Math.pow($1, $3);}
| e '!'
{{
$$ = (function(n) {if(n==0) return 1; return arguments.callee(n-1) * n})($1)
$$ = (function fact (n) { return n==0 ? 1 : fact(n-1) * n })($1);
}}
| e '%'
{$$ = $1/100;}
Expand Down

0 comments on commit fb0474b

Please sign in to comment.