-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
498 lines (461 loc) · 21.2 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
<!--
* @Author: LetMeFly
* @Date: 2022-06-20 22:21:32
* @LastEditors: LetMeFly
* @LastEditTime: 2022-06-22 22:45:26
-->
<!DOCTYPE html>
<html>
<head>
<title>语法分析</title>
<meta charset="utf-8">
<script src="https://letmefly.xyz/Links/Common.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="/static/css/bootstrap.min.css">
<script src="/static/js/jquery.min.js"></script>
<script src="/static/js/bootstrap.min.js"></script>
<script src="/LSA.js"></script>
</head>
<body>
<div class="container">
<div class="jumbotron">
<h1>语法分析</h1>
<div class="row">
<div class="col-sm-2"><h3>LL(1)</h3></div>
<div class="col-sm-2"><h3>LR(0)</h3></div>
<div class="col-sm-2"><h3>SLR(1)</h3></div>
<div class="col-sm-2"><h3>LR(1)</h3></div>
<div class="col-sm-2"><h3>SALR(1)</h3></div>
</div>
</div>
<div class="row">
<div class="col-sm-4">
<h3>请选择要分析的文法</h3>
<form id="GrammarChoiceForm">
<label><input name="GrammarChoice" type="radio" value="LL1"> LL(1)</label> <br>
<label><input name="GrammarChoice" type="radio" value="LR0"> LR(0)</label> <br>
<label><input name="GrammarChoice" type="radio" value="SLR1"> SLR(1)</label> <br>
<label><input name="GrammarChoice" type="radio" value="LR1"> LR(1)</label> <br>
<label><input name="GrammarChoice" type="radio" value="SALR1"> SALR(1)</label> <br>
</form>
</div>
<div class="col-sm-4">
<h3>请输入文法规则</h3>
<textarea name="GrammarInput" id="GrammarInput" cols="30" rows="10"></textarea>
</div>
<div class="col-sm-4">
<h3>请输入要匹配的字符串</h3>
<input type="text" id="stringInput">
</div>
</div>
<div class="row" style="margin-top: 50px;">
<div class="col-sm-3"></div>
<div class="col-sm-6">
<button class="btn btn-primary" style="width: 100%;" onclick="analysis()"><h4>开始分析</h4></button>
</div>
<div class="col-sm-3"></div>
</div>
<div id="processDiv" style="margin-top: 20px;"></div>
</div>
<div class="beforeFooter" id="beforeFooter"></div>
<footer class="navbar-fixed-bottom" id="footer">
<div class="container" style="background-color: #eeeeee">
<div class="row">
<div class="col-sm-6">项目地址: <a href="https://github.com/LetMeFly666/SyntaxAnalysis" target="blank">https://github.com/LetMeFly666/SyntaxAnalysis</a></div>
<div class="col-sm-6">在线地址:<a href="https://yffx.letmefly.xyz" target="blank">https://yffx.letmefly.xyz (语法分析.letmefly.xyz)</a></div>
</div>
<div class="row" style="text-align: center;">
<span>DFA可视化:<a href="https://github.com/mermaid-js/mermaid" target="blank">mermiad</a> </span>
<span>使用 <a href="https://www.bootcss.com" target="blank">BootStrap</a>、<a href="https://jquery.com" target="blank">jQuery</a></span>
<span>By <a href="https://letmefly.xyz" target="blank">LetMeFly</a></span>
</div>
<!-- <div class="row" style="text-align: center;">
Copyright 2022 ©<a href="https://letmefly.xyz">LetMeFly</a> all rights reserved.
</div> -->
</div>
</footer>
<script>
const grammarChoiceForm = $("#GrammarChoiceForm");
const grammars = grammarChoiceForm.find("input");
const grammarInput = $("#GrammarInput");
const stringInput = $("#stringInput");
const processDiv = $("#processDiv");
function getChoosedGrammar() { // 获取选择的语法
for (var i = 0; i < grammars.length; i++) {
if (grammars[i].checked) {
return grammars[i].value;
}
}
}
function choose1grammar() { // 选择一个文法
grammarInput[0].placeholder = grammarSamples[this.value].grammar + "\n----------------------------------------\n空格可作为分隔符,不可作为终结符\n“|”代表“或”\n“ε”代表“空”\n“->”作为推导符号\n“->”左边出现过的为非终结符,其余未提到的为终结符";
stringInput[0].placeholder = grammarSamples[this.value].string;
};
function adjustFooter() { // 调整底部距离
const height = $("#footer").height();
$("#beforeFooter").height(height);
}
function analysis() { // 分析
processDiv.empty();
if (getChoosedGrammar() != "LL1") {
alert("待开发!当前仅支持LL(1),当前" + getChoosedGrammar() + "也会按LL(1)的方法进行分析");
}
if (!grammarInput.val()) {
grammarInput.val(grammarSamples[getChoosedGrammar()].grammar);
}
const NT = getNTG(grammarInput.val());
// console.log(NT);
if (NT.error.length) {
const errorDiv = document.createElement("div");
errorDiv.setAttribute("id", "errorDiv");
processDiv.append(errorDiv);
const h3 = document.createElement("h3");
h3.innerText = "错误列表:";
errorDiv.append(h3);
const ul = document.createElement("ul");
ul.setAttribute("id", "errorUl");
ul.setAttribute("class", "list-group");
errorDiv.append(ul);
NT.error.forEach(e => {
const li = document.createElement("li");
li.setAttribute("class", "list-group-item");
li.innerText = e;
ul.append(li);
});
return;
}
function showNT(NT) {
const rowDiv = document.createElement("div");
rowDiv.setAttribute("class", "row");
processDiv.append(rowDiv);
// const formattedGrammarsDiv = document.createElement("div");
// formattedGrammarsDiv.setAttribute("id", "formattedGrammarsDiv");
// rowDiv.append(formattedGrammarsDiv);
const divLeft = document.createElement("div");
divLeft.setAttribute("class", "col-sm-6");
rowDiv.append(divLeft);
const h3 = document.createElement("h3");
h3.innerText = "格式化文法规则:";
divLeft.append(h3);
const ul = document.createElement("ul");
ul.setAttribute("id", "formattedGrammarsUl");
ul.setAttribute("class", "list-group");
divLeft.append(ul);
NT.formattedGrammars.forEach(e => {
const li = document.createElement("li");
li.setAttribute("class", "list-group-item");
li.innerText = e.N + " ->";
e.T.forEach(thisT => {
li.innerText += " " + thisT;
});
ul.append(li);
});
const divMiddle = document.createElement("div");
divMiddle.setAttribute("class", "col-sm-3");
rowDiv.append(divMiddle);
const TDiv = document.createElement("div");
divMiddle.append(TDiv);
const h3_2 = document.createElement("h3");
h3_2.innerText = "非终结符";
TDiv.append(h3_2);
const ul_2 = document.createElement("ul");
ul_2.setAttribute("id", "formattedGrammarsUl");
ul_2.setAttribute("class", "list-group");
TDiv.append(ul_2);
NT.N.forEach(e => {
const li = document.createElement("li");
li.setAttribute("class", "list-group-item");
li.innerText = e;
ul_2.append(li);
});
const divRight = document.createElement("div");
divRight.setAttribute("class", "col-sm-3");
rowDiv.append(divRight);
const NDiv = document.createElement("div");
// divRight.setAttribute("id", "formattedGrammarsDiv");
processDiv.append(NDiv);
const h3_3 = document.createElement("h3");
h3_3.innerText = "终结符";
divRight.append(h3_3);
const ul_3 = document.createElement("ul");
ul_3.setAttribute("id", "formattedGrammarsUl");
ul_3.setAttribute("class", "list-group");
divRight.append(ul_3);
NT.T.forEach(e => {
const li = document.createElement("li");
li.setAttribute("class", "list-group-item");
li.innerText = e;
ul_3.append(li);
});
}
showNT(NT);
// 消除左递归
const NTLRed = leftRecursion(NT);
const LeftedRecTitleDiv = document.createElement("div");
LeftedRecTitleDiv.setAttribute("class", "text-align: center");
processDiv.append(LeftedRecTitleDiv);
const LeftedRecTitleCenter = document.createElement("center");
LeftedRecTitleDiv.append(LeftedRecTitleCenter);
const LeftedRecTitleh3 = document.createElement("h3");
LeftedRecTitleh3.innerText = "消除左递归";
LeftedRecTitleCenter.append(LeftedRecTitleh3);
showNT(NTLRed);
// 提取左因子
const NTLefted = leftFactoring(NT);
const LeftedTitleDiv = document.createElement("div");
LeftedTitleDiv.setAttribute("class", "text-align: center");
processDiv.append(LeftedTitleDiv);
const LeftedTitleCenter = document.createElement("center");
LeftedTitleDiv.append(LeftedTitleCenter);
const LeftedTitleh3 = document.createElement("h3");
LeftedTitleh3.innerText = "提取左因子";
LeftedTitleCenter.append(LeftedTitleh3);
showNT(NTLefted);
// First Follow集合
const First = getFirst(NT);
const FirstFollowRowDiv = document.createElement("div");
FirstFollowRowDiv.setAttribute("class", "row");
processDiv.append(FirstFollowRowDiv);
const FirstDiv = document.createElement("div");
FirstDiv.setAttribute("class", "col-sm-6");
FirstFollowRowDiv.append(FirstDiv);
const FirstTitle = document.createElement("h3");
FirstTitle.innerText = "First集合";
FirstDiv.append(FirstTitle);
const FirstUl = document.createElement("ul");
FirstUl.setAttribute("class", "list-group");
FirstDiv.append(FirstUl);
for (key in First) {
const li = document.createElement("li");
li.setAttribute("class", "list-group-item");
FirstUl.append(li);
li.innerText = "First(" + key + ") = {";
firstElement = true;
First[key].forEach(thisElement => {
if (firstElement)
firstElement = false;
else
li.innerText += ", "; // BootStrap把空格吞了
li.innerText += thisElement;
});
li.innerText += "}";
}
// Follow集合
const Follow = getFollow(NT, First);
const FollowDiv = document.createElement("div");
FollowDiv.setAttribute("class", "col-sm-6");
FirstFollowRowDiv.append(FollowDiv);
const FollowTitle = document.createElement("h3");
FollowTitle.innerText = "Follow集合";
FollowDiv.append(FollowTitle);
const FollowUl = document.createElement("ul");
FollowUl.setAttribute("class", "list-group");
FollowDiv.append(FollowUl);
for (key in Follow) {
const li = document.createElement("li");
li.setAttribute("class", "list-group-item");
FollowUl.append(li);
li.innerText = "Follow(" + key + ") = {";
firstElement = true;
Follow[key].forEach(thisElement => {
if (firstElement)
firstElement = false;
else
li.innerText += ", "; // BootStrap把空格吞了
li.innerText += thisElement;
});
li.innerText += "}";
}
// LL(1)分析表
const LL1Table = getLL1Table(NT, First, Follow);
// console.log(LL1Table);
if (Array.isArray(LL1Table)) { // 冲突!!error
console.log(LL1Table);
alert("冲突!,详情请见控制台,格式[[N, T, LL1Table[N][T], thisGrammar], ..]");
return;
}
const TableTitleCenterDiv = document.createElement("div");
TableTitleCenterDiv.setAttribute("class", "text-align: center");
processDiv.append(TableTitleCenterDiv);
const TableTitle = document.createElement("h3");
TableTitle.innerText = "LL1分析表";
TableTitleCenterDiv.append(TableTitle);
const table = document.createElement("table");
table.setAttribute("class", "table");
processDiv.append(table);
// const caption = document.createElement("caption");
// caption.innerText = "LL1分析表";
// table.append(caption);
const thead = document.createElement("thead");
table.append(thead);
const tr = document.createElement("tr");
thead.append(tr);
const firstLeft = document.createElement("th");
firstLeft.innerText = "M[N, T]";
tr.append(firstLeft);
NT.T.forEach(thisT => {
const thisTr = document.createElement("th");
thisTr.innerText = thisT;
tr.append(thisTr);
});
const Dollar = document.createElement("th");
Dollar.innerText = "$";
tr.append(Dollar);
const tbody = document.createElement("tbody");
table.append(tbody);
NT.N.forEach(thisN => {
const tr = document.createElement("tr");
tbody.append(tr);
const thisNT = document.createElement("td");
thisNT.innerText = thisN;
tr.append(thisNT);
NT.T.forEach(thisT => {
const thisTr = document.createElement("td");
if (LL1Table[thisN][thisT]) {
thisTr.innerText = LL1Table[thisN][thisT].N + " -> ";
LL1Table[thisN][thisT].T.forEach(e => {
thisTr.innerText += e + " ";
});
}
tr.append(thisTr);
});
const Dollar = document.createElement("th");
if (LL1Table[thisN]["$"]) {
thisTr.innerText = LL1Table[thisN]["$"];
}
tr.append(Dollar);
});
// LL1分析栈
if (!$("#stringInput").val()) {
$("#stringInput").val($("#stringInput")[0].getAttribute("placeholder"));
}
// LL1stack(table, $("#stringInput").val());
const StackDivCenter = document.createElement("div");
StackDivCenter.setAttribute("class", "text-align: center");
processDiv.append(StackDivCenter);
const StackTableTitle = document.createElement("h3");
StackTableTitle.innerText = "LL1分析栈";
StackDivCenter.append(StackTableTitle);
const StackTable = document.createElement("table");
StackTable.setAttribute("class", "table");
processDiv.append(StackTable);
const StackHead = document.createElement("thead");
StackTable.append(StackHead);
StackHead.innerHTML = "<tr><th>分析栈</th><th>输入</th><th>动作</th></tr>";
const tbodyStack = document.createElement("tbody");
StackTable.append(tbodyStack);
function getFirstN() { // 获取最初的非终结符 // TODO: 和LSA重复
let isFirst = true;
let result;
NT.N.forEach(e => {
if (isFirst) {
result = e;
isFirst = false;
}
});
return result;
}
function insert1Line(stack1, stack2, action) {
const tr = document.createElement("tr");
tbodyStack.append(tr);
const td1 = document.createElement("td");
tr.append(td1)
for (var i = stack1.length - 1; i >= 0; i--) {
td1.innerText += stack1[i];
}
td1.innerText += "$";
const td2 = document.createElement("td");
for (var i = stack2.length - 1; i >= 0; i--) {
td2.innerText += stack2[i];
}
td2.innerText += "$";
tr.append(td2)
const td3 = document.createElement("td");
tr.append(td3)
td3.innerText = action;
}
const stackAna = [];
stackAna.push(getFirstN());
const stackInput = [];
const stringInputVal = $("#stringInput").val();
const stringInputList = [];
const stringInputValSplited = stringInputVal.split(" ");
stringInputValSplited.forEach(e => {
if (e)
stringInputList.push(e);
});
for (var i = stringInputList.length - 1; i >= 0; i--) {
stackInput.push(stringInputList[i]);
}
function accepted(stack1, stack2) {
if (stack1.length != 1 || stack2.length != 1)
return false;
return stack1[0] == "$" && stack1[1] == "$";
}
var times = 0;
while (!accepted(stackAna, stackInput)) {
const S1Copy = JSON.parse(JSON.stringify(stackAna));
const S2Copy = JSON.parse(JSON.stringify(stackInput));
times++;
if (times > 100)
break;
var action = "22";
var exit = false;
if (stackAna.length < 1 || stackInput.length < 1) {
if (stackAna.length == 0 && stackInput.length == 0) {
action = "接受!!";
exit = true;
}
else {
action = "错误!有栈为空";
exit = true;
}
}
else {
const FT = stackAna.pop();
const IT = stackInput.pop();
try {
if (FT == IT) {
action = "匹配";
}
else {
const grammar = LL1Table[FT][IT];
if (!grammar) {
action = "LL1表[" + FT + "][" + IT + "]为空!";
exit = true;
}
else {
action = grammar.N + " -> ";
grammar.T.forEach(e => {
action += e + " ";
});
if (grammar.T.length == 1 && grammar.T[0] == KONG) {
}
else {
for (var i = grammar.T.length - 1; i >= 0; i--) {
stackAna.push(grammar.T[i]);
}
}
stackInput.push(IT);
}
}
}
catch {
action = "错误!";
exit = true;
}
}
insert1Line(S1Copy, S2Copy, action);
if (exit)
break;
}
}
$(() => { // Onload
grammars.click(choose1grammar);
grammars[0].click();
adjustFooter();
});
$(window).resize(() => adjustFooter());
</script>
</body>
</html>