Skip to content

Instantly share code, notes, and snippets.

@jedp
Created May 13, 2011 06:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jedp/970079 to your computer and use it in GitHub Desktop.
Save jedp/970079 to your computer and use it in GitHub Desktop.
Jison grammar to convert numerals into an integer
/* Jison grammar for converting numerals into an integer */
/* The lexical analyzer
How we break things up into tokens */
%lex
%%
[0-9]+ return 'INT'
"-" return '-'
<<EOF>> return 'EOF'
. return 'TOTAL_DISASTER'
/lex
%left UMINUS
/* The grammar
How we figure out what the tokens mean */
%start grammar
%%
grammar
: integer EOF { return $integer; }
| EOF { return null; }
;
integer
: INT { $$ = Number(yytext); }
| '-' integer %prec UMINUS { $$ = -$2; }
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment