Created
September 24, 2019 21:06
-
-
Save JoelCodes/8e9db24c9c1f55d327ca6aaa8dbb9613 to your computer and use it in GitHub Desktop.
Jison Demo
This file contains hidden or 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
const {Parser} = require('jison'); | |
const parser = new Parser(` | |
/* lexical grammar */ | |
%lex | |
%% | |
\\s+ /* skip whitespace */ | |
"var" return 'VAR' | |
[a-zA-Z_$][a-zA-Z_$0-9]* return 'VAR_NAME' | |
"=" return '=' | |
[0-9]+ return 'NUMBER' | |
<<EOF>> return 'EOF' | |
. return 'INVALID' | |
/lex | |
/* operator associations and precedence */ | |
%start file | |
%% /* language grammar */ | |
var_expression | |
: VAR VAR_NAME "=" NUMBER | |
{$$ = {type: "VarExp", "name": $2, "value": $4}} | |
| VAR VAR_NAME | |
{$$ = {type: "VarExp", "name": $2}}; | |
set_expression | |
: VAR_NAME "=" NUMBER | |
{$$ = {type: "SetExp", "name": $1, "value": $3}}; | |
expression | |
: var_expression | |
{$$ = $1} | |
| set_expression | |
{$$ = $1}; | |
expression_list | |
: expression | |
{$$ = [$1];} | |
| expression expression_list | |
{$$ = [$1, ...$2];} | |
; | |
file | |
: expression_list EOF | |
{return $1;} | |
; | |
`); | |
const program = parser.parse(` | |
var x | |
var y = 4 | |
z = 3 | |
`); | |
console.log(program); |
This file contains hidden or 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
{ | |
"name": "jison-doodle", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"keywords": [], | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"jison": "^0.4.18" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment