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
<title>Top Down Operator Precedence</title>
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;}
<body bgcolor=linen>
<h1>Top Down Operator Precedence</h1>
<p><a href="">Douglas Crockford</a></p>
<p>This is the companion file to my paper on <a href="">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="">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">
<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>
<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>
<td valign="top"><a href="">json2.js</a></td>
<td>This file installs the <code>JSON.stringify</code> method.
It is used to display the parse tree.</td>
<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>
/*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; = "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) {