%% S : A ; A : /* empty */ | A x ;
[~/jison/examples/basic2_lex(develop)]$ cat basic2_lex.jison /* description: Basic grammar that contains a nullable A nonterminal. */ %lex %% \s+ {/* skip whitespace */} [a-zA-Z_]\w* {return 'x';} /lex %% S : A { return $1+" identifiers"; } ; A : /* empty */ { console.log("starting"); $$ = 0; } | A x { $$ = $1 + 1; console.log($$) } ;
$ cat basic2_lex.html <!DOCTYPE HTML> <html lang="en"> <head> <meta charset="utf-8"> <title>Jison</title> <link rel="stylesheet" href="global.css" type="text/css" media="screen" charset="utf-8" /> </head> <body> <h1>basic2_lex demo</h1> <div id="content"> <script src="jquery/jquery.js"></script> <script src="basic2_lex.js"></script> <script src="main.js"></script> <p> <input type="text" value="x x x x" /> <button>parse</button> <span id="output"></span> <!-- Output goes here! --> </p> </div> </body> </html>
$ cat Rakefile # install package: # sudo npm install beautifier # # more about beautifier: # https://github.com/rickeyski/node-beautifier dec "compile the grammar basic2_lex_ugly.jison" task :default => %w{basic2_lex_ugly.js} do sh "mv basic2_lex.js basic2_lex_ugly.js" sh "jsbeautify basic2_lex_ugly.js > basic2_lex.js" sh "rm -f basic2_lex_ugly.js" end file "basic2_lex_ugly.js" => %w{basic2_lex.jison} do sh "jison basic2_lex.jison -o basic2_lex.js" end
[~/jison/examples/basic2_lex(develop)]$ cat global.css html * { font-size: large; /* The !important ensures that nothing can override what you've set in this style (unless it is also important). */ font-family: Arial; } .thumb { height: 75px; border: 1px solid #000; margin: 10px 5px 0 0; } h1 { text-align: center; font-size: x-large; } th, td { vertical-align: top; text-align: left; } /* #finaltable * { color: white; background-color: black; } */ /* #finaltable table { border-collapse:collapse; } */ /* #finaltable table, td { border:1px solid white; } */ #finaltable:hover td { background-color: blue; } tr:nth-child(odd) { background-color:#eee; } tr:nth-child(even) { background-color:#00FF66; } input { text-align: right; border: none; } /* Align input to the right */ textarea { border: outset; border-color: white; } table { border: inset; border-color: white; } .hidden { display: none; } .unhidden { display: block; } table.center { margin-left:auto; margin-right:auto; } #result { border-color: red; } tr.error { background-color: red; } pre.output { background-color: white; } span.repeated { background-color: red } span.header { background-color: blue } span.comments { background-color: orange } span.blanks { background-color: green } span.nameEqualValue { background-color: cyan } span.error { background-color: red } body { background-color:#b0c4de; /* blue */ }
ID = NUM
separadas
por puntos y comas, por ejemplo a = 4; b = 4.56; c = -8.57e34
.
El analizador retorna un hash/objeto
cuyas claves son los identificadores y cuyos valores
son los números.
Clone el repositorio en
https://github.com/crguezl/jison-basic2.
Modifique los analizadores léxico y sintáctico de forma conveniente.
Añada acciones semánticas para que el analizador devuelva una tabla de símbolos con los identificadores y sus valores.
Casiano Rodríguez León