This repo contains four examples:
- The first one in folder
minus
is a simple interpreter for infix arithmetic expressions with the minus operator only- See files
minus.jison
,minus.l
anduse_minus.js
- See files
- The example in folder
location
shows how to access to the location of tokens, how to pass information from outside to an action, how to deal with lexical errors and a few tricks to improve error messages- See files
minus-error-management.jison
,minus-error-management.l
andmain.js
- The
err.js
file contains the error message function
- See files
➜ ast git:(master) ✗ npx jison minus-ast.jison minus-ast.l -o minus.js
The file use-minus.js
outputs the AST in JSON format:
➜ ast git:(master) ✗ cat use_minus.js
#!/usr/bin/env node
const p = require("./minus").parser;
const t = p.parse(process.argv[2] || "2-1-1");
console.log(JSON.stringify(t, null, 2));
Here is an execution example:
➜ ast git:(master) ✗ ./use_minus.js > ast.json; cat ast.json
{
"type": "Program",
"body": [
{
"type": "ExpressionStatement",
"expression": {
"type": "CallExpression",
"callee": {
"type": "MemberExpression",
"object": {
"type": "Identifier",
"name": "console"
},
"property": {
"type": "Identifier",
"name": "log"
},
"computed": false
},
"arguments": [
{
"type": "BinaryExpression",
"left": {
"type": "BinaryExpression",
"left": {
"type": "Literal",
"value": 2,
"raw": "2"
},
"operator": "-",
"right": {
"type": "Literal",
"value": 1,
"raw": "1"
}
},
"operator": "-",
"right": {
"type": "Literal",
"value": 1,
"raw": "1"
}
}
]
}
}
],
"sourceType": "script"
}
- The example in folder
ast
is a translator from infix arithmetic expressions to JavaScriptminus-ast.jison
builds a Espree compatible AST usingminus.l
and the helpers inast-build.js
- Check astexplorer.net demo to explore the shape of the ASTs
- The lexical analyzer
minus.l
is used by both examples
- The
ast.json
filescontain examples of Espree ASTs forconsole.log(2-1-1)
More details in ast/README.md
- The example in folder
prec
illustrates the use of the%prec
directive to resolve conflicts- See files
prec.jison
,prec.l
anduse_prec.js
- The
prec-*.json
files contain examples of Espree ASTs
- See files
hello-jison git:(master) npm run
Lifecycle scripts included in hello-jison@1.0.0:
test
npm run tojs; node ast/salida.js
available via `npm run-script`:
test-simple
npm run build; ./minus/use_minus.js
tojs
npm run build-ast; ./ast/ast2js.js | tee ast/salida.js
build-ast
npm run compile-ast; ./ast/use_minus.js > ast/ast.json; cat ast/ast.json
build
jison minus/minus.jison minus/minus.l -o minus/minus.js
loc
jison location/minus-error-management.jison location/minus-error-management.l -o location/minus.js
debug
jison minus/minus.jison minus/minus.l -o minus/minus.js --debug
dfa
bison -v minus.jison; rm -f minus.tab.jison
prec
jison prec/uminus.jison prec/minus.l -o prec/uminus.js; prec/use-prec.js
compile-ast
jison ast/minus-ast.jison ast/minus-ast.l -o ast/minus.js
clean
rm -f ast/ast.json ast/salida.js ast/minus.js location/minus.js
npm i
The landing branch:
- master
One branch per class yearmonthday
:
- 20230213
- 20230214
Start to work from branch
- initial