-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
274 lines (249 loc) · 9.81 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>CS164 Final Proj Ex1: JVM</title>
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script type="text/javascript" src="http://documentcloud.github.com/underscore/underscore-min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js" type="text/javascript"></script>
<style src="graph.css"></style>
<style>
#left {
border:1px solid #d4d4d4;
width:62%;
height:1000px;
float:left;
padding-top:10px;
padding-left:15px;
}
#right {
border:1px solid #d4d4d4;
width:13%;
height:1000px;
float:left;
padding-left:20px;
}
#syntax-guide {
width:20%;
height:1000px;
float:left;
border:1px solid #d4d4d4;
padding-right:10px;
padding-left:20px;
margin-left:0px;
}
ul {
padding-left:5px;
}
</style>
</head>
<div id='root'></div>
<!-- Temporarily textbox for user input -->
<div id="left">
<div id='inputline' style="font-family: sans-serif; font-size: 12px; height=50px;">
Enter your input here : <input type="textbox" id="userinput" value="input if ( a ) if ( a ) a else a" style="width: 570px"></input>
<button id="Get">Enter</button>
</div>
<div id="complete-edge" style="font-family: sans-serif; font-size: 12px; float:right; padding-right:30px">
</div>
<div style="font-family: sans-serif; font-size: 16px; height=50px;">
<p id="errormessage" style="color: #ff0000;"></p>
</div>
</div>
<div id="right">
<p style="font-family: sans-serif; font-size: 13px;"><u>Assocs</u></p>
<ul id="assocs" style="font-family: sans-serif; font-size: 12px;">
</ul>
<p style="font-family: sans-serif; font-size: 13px;"><u>Grammars</u></p>
<ul id="grammars" style="font-family: sans-serif; font-size: 12px;">
</ul>
</div>
<div id="syntax-guide">
<div id="grammar" style="font-family: sans-serif; font-size: 12px;">
<ul>
<li>This program supports three identifiers for input:
<ul>
<li>grammar: grammar E -> V</li>
<li>input: input 2+3+4</li>
<li>assoc: assoc left +</li>
</ul>
</li>
<li> You should always <b>put a space between identifier and context</b>
<ul>
<li>e.g. grammar E -> V ✓</li>
<li>e.g. grammarE -> V ✗</li>
</ul>
</li>
<li> You should always <b>put a space between the arrow and each side of grammar</b>
<ul>
<li>e.g. grammar E -> V ✓</li>
<li>e.g. grammar E->V ✗</li>
</ul>
</li>
<li> You can use %prec and %dprec as following (don't forget <b>space</b>!):
<ul>
<li>e.g. grammar E -> V %dprec 1 ✓</li>
<li>e.g. grammar E -> V %dprec1 ✗</li>
<li>e.g. grammar E -> V%dprec 1 ✗</li>
<li>e.g. grammar E -> V %prec ! ✓</li>
<li>e.g. grammar E -> V %prec '!' ✗</li>
</ul>
</li>
<li> For assoc, you can have multiple procedures for the same tier:
<ul>
<li>e.g. assoc left +,!,- ✓</li>
<li>e.g. assoc right +, !, - ✓</li>
<li>e.g. assoc left +!- ✗</li>
</ul>
</li>
<li> For grammar, always <b style="color: #ff0000;">make S as the starting terminal</b>. Otherwise, the program will think the starting terminal doesn't exist. </li>
<li><b style="color: #ff0000;">This is very white-space sensitive!</b></li>
</ul>
</div>
<img src="eecsoooo.jpg" alt="Team JVM!" style="">
</div>
<body>
<script type="text/javascript" src="graph.js"></script>
<script type="text/javascript" src="interpreter.js"></script>
<script type="text/javascript" src="parser.js"></script>
<script type="text/javascript" src="earleyparser.js"></script>
<script type="text/javascript">
//the current graph being displayed
// width and height of svg
var w = parseFloat(d3.select("#left").style("width").substring(0, d3.select("#left").style("width").length-2));
var h = 910;
var circleDist = 150;
var svg = d3.select("#left").append("svg").attr("width", w).attr("height", h);
var root = d3.select('#inputline')
// creates a back button that calls stepBack with nowgraph
root.append('button')
.text('Back')
.on('click', function() { stepBack(nowgraph); });
// creates a forward button that calls stepForward with nowgraph
root.append('button')
.text('Forward')
.on('click', function() { stepForward(nowgraph); });
d3.select('#complete-edge').append('label')
.text("Draw complete edges only")
.append('input')
.attr('type', 'checkbox')
.text("Show only Completed edges");
// Receive input from REPL, parse it into AST
function handleUserInput(text){
switch(text) {
case "demo 1":
handleUserInput("grammar S -> E E");
handleUserInput("grammar E -> hello");
handleUserInput("grammar E -> world");
break;
case "demo 2":
handleUserInput("grammar S -> E");
handleUserInput("grammar E -> E + E");
handleUserInput("grammar E -> E - E");
handleUserInput("grammar E -> a");
break;
case "demo 3":
handleUserInput("grammar S -> E");
handleUserInput("grammar E -> E + E");
handleUserInput("grammar E -> E - E");
handleUserInput("grammar E -> a");
handleUserInput("assoc left +,-");
break;
case "demo 4":
handleUserInput("grammar S -> E");
handleUserInput("grammar E -> if ( E ) E else E");
handleUserInput("grammar E -> if ( E ) E");
handleUserInput("grammar E -> a")
break;
case "demo 5":
handleUserInput("grammar S -> E");
handleUserInput("grammar E -> if ( E ) E else E %dprec1");
handleUserInput("grammar E -> if ( E ) E %dprec2");
handleUserInput("grammar E -> a")
break;
default:
try {
var ast = parse(text);
var obj = eval(ast); // Returns an object if ast is type input. Otherwise null
displayGrammar(ast.type, text);
// $('#userinput').val("");
setErrorMessage("");
} catch (err){
setErrorMessage(err.message);
}
if (obj){
try {
var input = obj.input;
var completeEdgesOnly = d3.select('#complete-edge').select('label').select('input')[0][0].checked;
var earleyResult = earleyParse(obj.grammars, input, completeEdgesOnly);
var marshalledEdges = marshallEdges(earleyResult.edges); // Changes edge representation from Earley's, to D3's
} catch (err){
setErrorMessage(err.message);
}
nowgraph = new Graph(marshalledEdges, earleyResult.nodes, earleyResult.initEdges);
drawGraph(nowgraph);
}
}
}
// Calls Jisoo's parser
function parse(text){
return parseInput(text);
}
// Calls Vivian;s earley parser
function earleyParse(grammars, input, completeEdgesOnly){
return earleyParseInput(grammars, input, completeEdgesOnly);
}
// Converts Earley's edge representation which is an obejct to D3's, which is an array
function marshallEdges(edges){
var marshalled = []
for (i = 0; i < edges.length; i++) {
var edge = edges[i];
var edgeProgression = edge.edgeProgression;
var lhs = edgeProgression.N;
var rhs = ""
var pos = edgeProgression.pos;
for (index in edgeProgression.RHS) {
if(index == pos){
rhs = rhs + "."
}
rhs = rhs + edgeProgression.RHS[index] + " " ;
}
marshalled.push([edge.src, edge.dst, lhs + " -> " +rhs]);
}
return marshalled;
}
var nodelist = [0, 1, 2, 3, 4, 5];
var initEdgeList = [[0,1,"1"], [1,2,"+"], [2,3,"2"], [3,4,"*"], [4,5,"3"]];
var steplist = [[0,1,"E->Id"], [0,1,"Id->1"], [2,3,"E->Id"], [2,3,"Id->2"], [4,5,"E->Id"], [4,5,"Id->3"], [0,3,"E->E+E"], [2,5,"E->E*E"], [0,5,"E->E+E"], [0,5,"E->E*E"]];
var nowgraph = new Graph([], [], []);
</script>
<script>
function setErrorMessage(msg){
document.getElementById("errormessage").innerHTML = msg;
}
var assocText = [];
var grammarText = [];
function displayGrammar(type, text){
switch (type){
case "grammar":
grammarText.push(text);
items = [];
$('#right > #grammars').append('<li>' + text.substring(8, text.length) + '</li>');
break;
case "assoc":
assocText.push(text);
items = [];
$('#right > #assocs').append('<li>' + text.substring(6, text.length) + '</li>');
break;
}
}
function setupTextBox(){
$("button:#Get").click(function () {
handleUserInput($('#userinput').val());
});
}
$(document).ready(setupTextBox);
d3.select('#complete-edge').select('label').select('input')[0][0].checked = true;
</script>
</body>
</html>