| 1 |
wakaba |
1.1 |
<!DOCTYPE HTML> |
| 2 |
|
|
<html lang=en> |
| 3 |
|
|
<title>SAMI LR(1) Parser Generator</title> |
| 4 |
|
|
<link rel=stylesheet href="http://suika.fam.cx/www/style/html/xhtml"> |
| 5 |
|
|
<style> |
| 6 |
|
|
output { |
| 7 |
|
|
display: block; |
| 8 |
|
|
white-space: pre; |
| 9 |
|
|
} |
| 10 |
|
|
textarea { |
| 11 |
wakaba |
1.2 |
height: 20em; |
| 12 |
wakaba |
1.1 |
} |
| 13 |
|
|
p, output { |
| 14 |
|
|
text-indent: 0; |
| 15 |
|
|
} |
| 16 |
|
|
</style> |
| 17 |
|
|
|
| 18 |
|
|
<h1>LR(1) Parser Generator</h1> |
| 19 |
|
|
|
| 20 |
|
|
<p><label><strong>Grammer</strong>:<br> |
| 21 |
|
|
<textarea id=grammer></textarea></label> |
| 22 |
|
|
|
| 23 |
|
|
<p><label><strong>Parsing table (as text)</strong>:<br> |
| 24 |
|
|
<textarea id=parsing-table-text></textarea></label> |
| 25 |
|
|
|
| 26 |
|
|
<p><label><strong>Parsing table (as JavaScript object)</strong>:<br> |
| 27 |
|
|
<textarea id=parsing-table-js></textarea></label> |
| 28 |
|
|
|
| 29 |
wakaba |
1.2 |
<p><label><strong>Parsed string</strong>:<br> |
| 30 |
|
|
<textarea id=parsed-string></textarea></label> |
| 31 |
|
|
|
| 32 |
|
|
<p><label><strong>Parse tree</strong>:<br> |
| 33 |
|
|
<textarea id=parse-tree></textarea></label> |
| 34 |
|
|
|
| 35 |
wakaba |
1.1 |
<script src="http://suika.fam.cx/www/js/sami/script/sami-core.js"></script> |
| 36 |
|
|
<script src="http://suika.fam.cx/www/js/sami/script/sami-pg.js"></script> |
| 37 |
|
|
<script> |
| 38 |
|
|
SAMI.Global.install (); |
| 39 |
|
|
|
| 40 |
|
|
new SAMI.Observer ('change', $('grammer'), function () { |
| 41 |
|
|
var grammer = $('grammer').value; |
| 42 |
|
|
var pt = SAMI.PG.LR1.rulesStringToParsingTable (grammer, null, function (ev) { |
| 43 |
|
|
SAMI.Element.setTextContent ($('parsing-table-text'), |
| 44 |
|
|
'Error: Unexpected token type {' + ev.token.type + ', ' + ev.token.value + '}; ' + ev.value); |
| 45 |
|
|
}); |
| 46 |
|
|
|
| 47 |
|
|
if (pt) { |
| 48 |
|
|
SAMI.Element.setTextContent ($('parsing-table-text'), pt.toString ()); |
| 49 |
|
|
SAMI.Element.setTextContent ($('parsing-table-js'), pt.toSource ()); |
| 50 |
wakaba |
1.2 |
window.parsingTable = pt; |
| 51 |
wakaba |
1.1 |
} |
| 52 |
|
|
}); |
| 53 |
|
|
|
| 54 |
wakaba |
1.2 |
new SAMI.Observer ('change', $('parsed-string'), function () { |
| 55 |
|
|
var p = new SAMI.Parser.LR1 (); |
| 56 |
|
|
p._parsingTable = window.parsingTable; |
| 57 |
|
|
p._processLR1StackObjects = function (key, objs) { |
| 58 |
|
|
return {type: key, value: /* key + */ "{\n " + objs.map (function (s) { |
| 59 |
|
|
return s.type + ': ' + s.value; |
| 60 |
|
|
}).list.join (',\n').replace (/\n/g, "\n ") + '\n}'}; |
| 61 |
|
|
}; |
| 62 |
|
|
new SAMI.Observer ('error', p, function (ev) { |
| 63 |
|
|
SAMI.Element.setTextContent ($('parse-tree'), |
| 64 |
|
|
'Error: Unexpected token type {' + ev.token.type + ', ' + ev.token.value + '}; ' + ev.value); |
| 65 |
|
|
}); |
| 66 |
|
|
|
| 67 |
|
|
var t = new SAMI.Parser.SimpleTokenizer (); |
| 68 |
|
|
t._patterns = new SAMI.List ([ |
| 69 |
|
|
{pattern: /[\w_]+/, type: 'atom', code: function (t, v) { t.value = v }}, |
| 70 |
|
|
{pattern: /[^\s\w_]+/}, |
| 71 |
|
|
{pattern: /\s+/, ignore: true} |
| 72 |
|
|
]); |
| 73 |
|
|
var tokens = t.tokenizeString ($('parsed-string').value); |
| 74 |
|
|
var r = p._parseTokens (tokens); |
| 75 |
|
|
|
| 76 |
|
|
if (r) { |
| 77 |
|
|
SAMI.Element.setTextContent ($('parse-tree'), r.value); |
| 78 |
|
|
} |
| 79 |
|
|
}); |
| 80 |
wakaba |
1.1 |
</script> |