Translating Arithmetic Expressions to JavaScript
Objetivos
Usando Jison generalice el ejemplo en la carpeta ast
del Repo hello-jison constituido por los ficheros:
para escribir un programa que, recibiendo como entrada una cadena que contiene una secuencia de expresiones aritméticas de enteros separados por comas, produzca un AST compatible con el del parser espree. Use escodegen para generar el código JavaScript correspondiente (vea ast2js.js). Sigue un ejemplo de una posible ejecución:
✗ cat test/data/test1.calc 4 - 2 - 1 ✗ bin/calc2js.mjs test/data/test1.calc console.log(4 - 2 - 1); ✗ bin/calc2js.js test/data/test1.calc | node - 1
Las expresiones aritméticas deben soportar además de suma, resta, multiplicación y división el menos unario -(2+3)*2
un operador de factorial !
, un operador de potencia **
y paréntesis. Ejemplo: -(2 + 3) * 4! - 5 ** 6
.
Opciones en línea de comandos
Use commander para procesar la línea de argumentos:
$ bin/calc2js.mjs --help Usage: calc2js [options] <filename> Arguments: filename file with the original code Options: -V, --version output the version number -o, --output <filename> file in which to write the output -h, --help display help for command
Dealing with Ambiguity
Para tratar con los temas de ambigüedad en la gramática, puede consultar
- la sección Precedencia y Asociatividad de los viejos apuntes de PL
- la sección Dealing with ambiguity de la práctica hello-compiler y aplíque los conceptos aprendidos a este proyecto.
Unary minus and exponentiation in escodegen
Pruebas
Añada pruebas a este proyecto usando mocha
Covering
You can use nyc to do the covering of your mocha tests. See the notes in covering.
Activate the GitHub pages of your repo (use the default branch and the docs
folder) and be sure to include your covering report in the docs
folder.
✗ npm run cov > hello-jison@1.0.0 cov > nyc npm test > hello-jison@1.0.0 test > npm run compile; mocha test/test.mjs > hello-jison@1.0.0 compile > jison src/grammar.jison src/lexer.l -o src/calc.js ✔ transpile(test1.calc, out1.js) ✔ transpile(test2.calc, out2.js) ✔ transpile(test3.calc, out3.js) 3 passing (20ms) --------------|---------|----------|---------|---------|----------------------------------------- File | % Stmts | % Branch | % Funcs | % Lines | Uncovered Line #s --------------|---------|----------|---------|---------|----------------------------------------- All files | 58.92 | 45.83 | 47.22 | 56.63 | ast-build.js | 100 | 100 | 100 | 100 | calc.js | 57.44 | 45.78 | 40.62 | 54.92 | ...,530-539,548-569,578,580,602-607,610 transpile.js | 81.81 | 50 | 100 | 81.81 | 11-12 --------------|---------|----------|---------|---------|-----------------------------------------
References
Essentials for this lab
- See the examples in the repo crguezl/hello-jison
- https://astexplorer.net
- Tipos de Nodos del AST y nombres de las propiedades de los hijos
- Escodegen repo en GitHub
- Jison Documentation
Jison and Syntax Analysis
- Análisis Sintáctico Ascendente en JavaScript
- Jison
- Mi primer proyecto utilizando Jison por Erick Navarro
- Folder jison/examples from the Jison distribution
- Jison Debugger
- Precedencia y Asociatividad
- Construcción de las Tablas para el Análisis SLR
- Algoritmo de Análisis LR (yacc/bison/jison)
- Repo ULL-ESIT-PL-1718/jison-aSb
- Repo ULL-ESIT-PL-1718/ull-etsii-grado-pl-jisoncalc
- <a href="https://medium.com/basecs/leveling-up-ones-parsing-game-with-asts-d7a6fc2400ff" rel="nofollow">Leveling Up One’s Parsing Game With ASTs</a> by <a href="https://twitter.com/vaidehijoshi" rel="nofollow">Vaidehi Joshi</a> <em> 👍</em>