Skip to content

Instantly share code, notes, and snippets.

@saravanak
Last active January 7, 2016 01:45
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 saravanak/9a09aa257f5f6234e89d to your computer and use it in GitHub Desktop.
Save saravanak/9a09aa257f5f6234e89d to your computer and use it in GitHub Desktop.
Parsing an example language
/* description: Parses a simple Template spec*/
/* lexical grammar */
%lex
%%
\s+ /* skip whitespace */
[a-zA-Z0-9_]+\b return 'WORD';
"<" return '<';
">" return '>';
"[" return '[';
"]" return ']';
<<EOF>> return 'EOF';
/lex
%start template
%% /* */
template
: words EOF
{console.log($1); return $1;}
;
words
:word
{$$=$1;}
| word words
{$$=$1+$2;}
| '[' words ']'
{$$ = $1+$2+$3;}
;
word
: WORD
{$$ = $1;}
| '<' WORD '>'
{console.log('special word:'+$2); $$ = $1+$2+$3}
;
npm install -g jison # install jison if not present
# will generate generator.js in cwd, which is the parser for our language.
jison general.jison
# generate input file
echo "I have to shop for <item> [with <accomplishments> [which as also <discounted>]]" > general
# parse the example using our generated sheeny parser
node general.js general
#profit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment