-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.html
More file actions
212 lines (194 loc) · 6.78 KB
/
index.html
File metadata and controls
212 lines (194 loc) · 6.78 KB
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
<!DOCTYPE html lang="en">
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>first-follow</title>
<meta name="description" content="Simple first and follow sets generator for context-free grammars">
<style>
body {
font-size: large;
}
header {
margin: 1%;
margin-bottom: 2%;
}
#run-grammar {
padding: 10px;
cursor: pointer;
font-size: medium;
}
textarea, table {
font-size: large;
}
table caption {
font-weight: bold;
}
table {
border-collapse: collapse;
margin: 10px;
margin-top: 35px;
display: inline-block;
}
th, td {
border: 1px solid black;
}
table td, table th {
padding: 10px;
}
@media (min-width: 1000px) {
#rules {
float: right;
max-width: 30%;
margin-right: 3%;
top: 0px;
margin-top: 0px;
}
#left {
float: left;
max-width: 60%;
margin-left: 3%;
}
}
h1 { display: inline;}
</style>
</head>
<body>
<header>
<h1>firstFollow</h1>
<a id="github" href="https://github.com/BrunoRB/firstFollow" title="Got to repository page">
<img src="GitHub-Mark-32px.png" />
</a>
<p>Simple <strong>first and follow sets</strong> generator for context-free grammars.</p>
</header>
<div>
<aside id="rules">
<h2>Accepted input</h2>
<ul>
<li>Nonterminals should be separated from their derivations by -> or →, ex:
<ul>
<li>A -> B</li>
<li>B → y</li>
</ul>
</li>
<li>Multiple symbols should be separated by blank spaces, ex:
<ul>
<li>X → yOne yTwo y3</li>
<li>yTwo → W thisIsConsideredAsingleSymbol</li>
<li>W → those are considered five symbols</li>
</ul>
</li>
<li>Each set of rules must be in a newline and must not break into a new
line, or optionally multiple sets of rules can be placed at same line as long as all of them,
with the exception of the last one, are terminated by a semicolon (;), ex:
<ul>
<li>A → y w B</li>
<li>B → C y; C → w</li>
</ul>
</li>
<li>
Multiple derivations can be stated in the same line using the "|" separator:
<ul>
<li>A → w y | k | f</li>
</ul>
</li>
<li>For Epsilon derivations use ε, ϵ or leave the derivation empty:
<ul>
<li>A → B | ε</li>
<li>B → | K</li>
<li>B → j | ϵ</li>
<li>K →</li>
</ul>
</li>
</aside>
<div id="left">
<label for="grammar">
Your grammar:
</label> <br>
<textarea id="grammar" rows="15" cols=60 placeholder="A -> k y x B
B -> | k"></textarea>
<br>
<button id="run-grammar" type="button">Generate</button>
<div id="parse-error"></div>
<table id="first-table" style="visibility: hidden;">
<caption>First sets</caption>
<thead>
<tr>
<th>Symbol</th>
<th>Set</th>
</tr>
</thead>
<tbody></tbody>
</table>
<table id="follow-table" style="visibility: hidden;" tabindex="-1">
<caption>Follow sets</caption>
<thead>
<tr>
<th>Symbol</th>
<th>Set</th>
</tr>
</thead>
<tbody></tbody>
</table>
</div>
</div>
<script src="src/main.js"></script>
<script>
(function() {
'use strict';
var hasStorage = typeof localStorage !== 'undefined';
var parseError = document.getElementById('parse-error');
var textarea = document.getElementById('grammar');
var firstTable = document.getElementById('first-table');
var followTable = document.getElementById('follow-table');
var firstTbody = document.querySelector('#first-table tbody');
var followTbody = document.querySelector('#follow-table tbody');
document.getElementById('run-grammar').addEventListener('click', function() {
var grammar = textarea.value.trim();
try {
var data = firstFollow(grammar);
}
catch(e) {
firstTable.style.visibility = 'hidden';
followTable.style.visibility = 'hidden';
parseError.innerHTML = 'Error: ' + e;
return;
}
parseError.innerHTML = '';
var firstSet = data.firstSet;
var followSet = data.followSet;
var fContent = [];
var foContent = [];
for (var symbol in firstSet) {
var flist = Object.keys(firstSet[symbol]).map(function(s) {
return s === '' ? 'ε' : s;
});
fContent.push(
'<tr><td>' + symbol + '</td><td>{' + flist.join(', ') + '}</td></tr>'
);
}
for (var symbol in followSet) {
foContent.push(
'<tr><td>' + symbol + '</td><td>{' + Object.keys(followSet[symbol]).join(', ') + '}</td></tr>'
);
}
firstTbody.innerHTML = fContent.join('');
followTbody.innerHTML = foContent.join('');
firstTable.style.visibility = 'visible';
followTable.style.visibility = 'visible';
followTable.focus();
if (hasStorage) {
localStorage.setItem('data', grammar);
}
}, false);
if (hasStorage) {
var grammar = localStorage.getItem('data');
if (grammar) {
textarea.value = grammar;
}
}
})();
</script>
</body>
</html>