Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
executable file 121 lines (104 sloc) 3.87 KB
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Top Down Operator Precedence</title>
<style>
th {background-color: thistle; border: black solid 1px; text-align: left;
padding-left: 10px; padding-right: 10px; vertical-align: top;}
td {background-color: white; border: black solid 1pt; padding-left: 10px;
padding-right: 10px; vertical-align: top;}
table {width: 90%; border: 0px; cellpadding: 0px;}
</style>
</head>
<body bgcolor=linen>
<h1>Top Down Operator Precedence</h1>
<p><a href="http://www.crockford.com/">Douglas Crockford</a></p>
<p>2007-08-05</p>
<h2>Demonstration</h2>
<p>This is the companion file to my paper on <a href="http://javascript.crockford.com/tdop/tdop.html">Vaughan
Pratt's Top Down Operator Precedence</a>. The paper presents a parser
for Simplified JavaScript that is written in Simplified Javascript. It
is chapter 9 of <a href="http://www.amazon.com/exec/obidos/ASIN/0596510047/wrrrldwideweb">Beautiful
Code</a>. This page loads that parser and runs the parser on itself. </p>
<p>This page loads three JavaScript files:</p>
<table border="0" align="center">
<tr>
<td valign="top"><a href="tokens.js">tokens.js</a></td>
<td>This file installs the <code>String.prototype.tokens</code> method. It
produces an array of simple tokens from a string.</td>
</tr>
<tr>
<td valign="top"><a href="parse.js">parse.js</a></td>
<td>This file installs the <code>make_parse</code> function which produces
the parse function. The indirection was done so that we could conveniently
recover the source of the function.</td>
</tr>
<tr>
<td valign="top"><a href="http://json.org/json2.js">json2.js</a></td>
<td>This file installs the <code>JSON.stringify</code> method.
It is used to display the parse tree.</td>
</tr>
</table>
<p>The text that follows is the parse tree that the parser generated by
parsing itself. </p>
<textarea id="INPUT" style="
border: 2px solid black;
color: black;
font-family: monospace;
height: 3in;
overflow: auto;
padding: 0.5em;
width: 100%;"></textarea>
<input id="PARSE" value=parse type=button>
<noscript><p>You may not see it because you have JavaScript turned off. Uffff!</p></noscript>
<script src="tokens.js"></script>
<script src="parse.js"></script>
<script src="json2.js"></script>
<pre id=OUTPUT></pre>
<script>
/*jslint evil: true */
/*members create, error, message, name, prototype, stringify, toSource,
toString, write
*/
/*global JSON, make_parse, parse, source, tree */
// Make a new object that inherits members from an existing object.
if (typeof Object.create !== 'function') {
Object.create = function (o) {
function F() {}
F.prototype = o;
return new F();
};
}
// Transform a token object into an exception object and throw it.
Object.prototype.error = function (message, t) {
t = t || this;
t.name = "SyntaxError";
t.message = message;
throw t;
};
(function () {
var parse = make_parse();
function go(source) {
var string, tree;
try {
tree = parse(source);
string = JSON.stringify(tree, ['key', 'name', 'message',
'value', 'arity', 'first', 'second', 'third', 'fourth'], 4);
} catch (e) {
string = JSON.stringify(e, ['name', 'message', 'from', 'to', 'key',
'value', 'arity', 'first', 'second', 'third', 'fourth'], 4);
}
document.getElementById('OUTPUT').innerHTML = string
.replace(/&/g, '&amp;')
.replace(/[<]/g, '&lt;');
}
go("var make_parse = " + (make_parse.toSource ?
make_parse.toSource() : make_parse.toString()) + ";");
document.getElementById('PARSE').onclick = function (e) {
go(document.getElementById('INPUT').value);
};
}());
</script>
</pre>
</body>
</html>