Last active
January 7, 2016 01:45
-
-
Save saravanak/9a09aa257f5f6234e89d to your computer and use it in GitHub Desktop.
Parsing an example language
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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} | |
; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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