Skip to content

Commit 54612ce

Browse files
committed
* index.html: add parsing demo.
1 parent 901b2c2 commit 54612ce

File tree

2 files changed

+101
-11
lines changed

2 files changed

+101
-11
lines changed

demo.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
var forest_to_html;
2+
(function(){
3+
'use strict';
4+
5+
// -------------------------------------------------------------------
6+
// Convert parse forest to nested HTML tables
7+
8+
forest_to_html = function forest_to_html(f) {
9+
if(!f) return;
10+
11+
var result;
12+
if(f.hasOwnProperty('next')) { // Derivation list
13+
result = table();
14+
while(f) {
15+
var child = children_to_html(f);
16+
if(child.nodeName.toLowerCase() !== 'tr') {
17+
child = tr(td(child));
18+
}
19+
result.appendChild(child);
20+
f = f.next;
21+
}
22+
} else if(f.tag.rule) { // LR(0) item
23+
result = children_to_html(f);
24+
} else { // Symbol item
25+
result = children_to_html(f);
26+
if(result) {
27+
if(result.nodeName.toLowerCase() !== 'tr') {
28+
result = tr(td(result));
29+
}
30+
var tag = td(text(f.tag));
31+
tag.colSpan = result.cells.length;
32+
tag = table(tr(tag));
33+
tag.appendChild(result);
34+
result = tag;
35+
} else result = text(f.tag);
36+
}
37+
return result;
38+
}
39+
40+
function children_to_html(f) {
41+
var left = forest_to_html(f.left);
42+
var right = forest_to_html(f.right);
43+
if(left && right) {
44+
if(left.nodeName.toLowerCase() !== 'tr') left = tr(td(left));
45+
left.appendChild(td(right));
46+
return left;
47+
} else return left || right;
48+
}
49+
50+
51+
// -------------------------------------------------------------------
52+
// Misc HTML helpers
53+
54+
function elt(name, child) {
55+
var e = document.createElement(name);
56+
if(child) e.appendChild(child);
57+
return e;
58+
}
59+
function table(node) { return elt('table', node); }
60+
function tr(node) { return elt('tr', node); }
61+
function td(node) { return elt('td', node); }
62+
function text(str) { return document.createTextNode(str); }
63+
64+
})();

index.html

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ <h1>A Pint-sized Earley Parser</h1>
4242
<li><a href="#algorithm">The Algorithm</a></li>
4343
</ul>
4444
<li><a href="#parser">The Parser</a></li>
45+
<li><a href="#demo">A Trivial Demonstration</a></li>
4546
<li><a href="#future">Future Work</a></li>
4647
</ul>
4748

@@ -288,19 +289,44 @@ <h2><a name="parser">The Parser</a></h2>
288289

289290

290291

292+
<h2><a name="demo">A Trivial Demonstration</a></h2>
293+
294+
<pre><code>g = Grammar('S',
295+
S -&gt; X X b
296+
X -&gt;
297+
X -&gt; a
298+
X -&gt; a a
299+
X -&gt; a a a)
300+
301+
parse(g, 'aaab')</code></pre>
302+
303+
<div id="forest"></div>
304+
<script src="demo.js"></script>
305+
<script>
306+
var g = new Grammar('S', [
307+
new Rule('S', ['X', 'X', 'b']),
308+
new Rule('X', []),
309+
new Rule('X', ['a']),
310+
new Rule('X', ['a', 'a']),
311+
new Rule('X', ['a', 'a', 'a'])
312+
]);
313+
var f = g.parse('aaab');
314+
315+
var div = document.getElementById('forest');
316+
var tree = forest_to_html(f);
317+
tree.className = 'center';
318+
div.appendChild(tree);
319+
</script>
320+
321+
291322
<h2><a name="future">Future Work</a></h2>
292323

293-
<p>I'm excited about finally getting this to work, so I'm writing this
294-
up and posting it right away. But this is just the core parsing
295-
algorithm: I want to at <em>least</em> write a parse forest visualizer
296-
and set up a page with a demo grammar where you can enter text and see
297-
the resulting forest.</p>
298-
299-
<p>And to be really usable at all it needs a means to enter grammars in
300-
a convenient notation, a way to choose a parse tree from the forest, and
301-
a way to attach actions to rules. I hope to get to this soon, but
302-
realistically it might be a month or three before I get around to
303-
finishing all of it.</p>
324+
<p>I'm excited about finally getting this algorithm figured out and running, so
325+
I'm writing it up and posting it right away. But to be really usable at all it
326+
needs a means to enter grammars in a convenient notation, a way to choose a
327+
parse tree from the forest, and a way to attach actions to rules. I hope to
328+
get to this soon, but realistically it might be a month or three before I get
329+
around to finishing all of it.</p>
304330

305331
</div>
306332

0 commit comments

Comments
 (0)