-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
350 lines (331 loc) · 8.82 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
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8">
<title>Compiladores</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
</head>
<body>
<div class="container-fluid jumbotron">
<h1 class="display-4">Compilador de expressão matemática</h1>
<p class="lead">Este compilador tem por finalidade analisar simples expressões matemáticas com as operações básicas.</p>
<hr class="my-4">
<p>
<label>Expressão:</label>
<input type="text" class="form-control" name="expression" id="expression">
</p>
<p class="lead">
<div class="btn-group btn-group-toggle" data-toggle="buttons">
<label class="btn btn-secondary active" id="verify-scanner">
<input type="radio" name="options" id="option1" autocomplete="off" checked> Scanner
</label>
<label class="btn btn-secondary" id="verify-parser">
<input type="radio" name="options" id="option2" autocomplete="off"> Parser
</label>
<label class="btn btn-secondary" id="verify-assembly">
<input type="radio" name="options" id="option3" autocomplete="off"> Assembly
</label>
</div>
</p>
<p class="results">
<div class="alert alert-secondary" role="alert"><strong>Resultado Scanner:</strong> <span id="result-scanner">Aguardando entrada ...</span></div>
<div class="alert alert-secondary" role="alert"><strong>Resultado Parse:</strong> <span id="result-parser">Aguardando entrada ...</span></div>
</p>
</div>
<footer class="container-fluid p-3 bg-secondary text-white fixed-bottom">
COMPILADORES | CMP1076 (2018-1) <span class="float-right">Desenvolvido por Alaor Jr. ©</span>
</footer>
<script
src="https://code.jquery.com/jquery-2.2.4.min.js"
integrity="sha256-BbhdlvQf/xTY9gja0Dq3HiwQF8LaCRTXxZKRutelT44="
crossorigin="anonymous"></script>
<script type="text/javascript">
const isNumber = function(input) { return !isNaN(parseInt(input)); };
const Token = function(token) {
const CONST_TOKENS = {
'+': 'SOMA'
,'-': 'SUB'
,'*': 'MULT'
,'/': 'DIV'
,'ERRO': 'ERRO'
,'EOF': 'EOF'
,'(': 'ABRE_PARENTESES'
,')': 'FECHA_PARENTESES'
};
if(isNumber(token))
return {
type: 'NUM',
value: parseInt(token)
};
else if(CONST_TOKENS[token])
return {
type: CONST_TOKENS[token],
value: token
};
else
throw('ERRO LÉXICO: Token não identificado \"' + token) + '\";'
}
let Scanner = function(input){
let pos = 0;
return {
getPos: function() { return pos; },
readNext: function(){
let read_token;
if(pos === input.length){
read_token = Token('EOF');
return read_token;
}
while(input[pos] === ' ' || input[pos] === '\t' || input[pos] === '\n')
pos++;
switch(input[pos]){
case '-': {
pos++;
if(!isNumber(input[pos])){
read_token = Token('-');
}
else{
let number = '-' + input[pos];
pos++;
while(pos < input.length && isNumber(input[pos])){
number += input[pos];
pos++;
}
read_token = Token(number);
}
break;
}
case '(':
case ')':
case '+':
case '*':
case '/': {
read_token = Token(input[pos]);
pos++;
break;
}
default: {
if(isNumber(input[pos])){
let number = input[pos];
pos++;
while(pos < input.length && isNumber(input[pos])){
number += input[pos];
pos++;
}
read_token = Token(number);
}else{
read_token = Token('ERRO');
throw('ERRO LÉXICO: Token não identificado \"' + input[pos]) + '\" NA POSIÇÃO \"' + pos + '\"!';
}
break;
}
}
return read_token;
}
}
};
let Parser = function(expression){
let scn = Scanner(expression)
,current_token = null;
let validateNext = function(token_expected){
current_token = scn.readNext();
let ok = !Array.isArray(token_expected)
? current_token.type === token_expected.type
: token_expected.find(function(token){ return current_token.type === token.type; });
if(ok)
return true;
else{
let expected = !Array.isArray(token_expected)
? token_expected.type
: token_expected.map(function(token){ return token.type }).join(' ou ');
throw('ERRO SINTÁTICO: \"' + expected + '\" ESPERADO, MAS \"' + current_token.type + '\" ENCONTRADO NA POSIÇÃO \"' + scn.getPos() + '\"!')
}
};
let parseRemaining = function(){
if(current_token.type === 'SOMA' || current_token.type === 'SUB' || current_token.type === 'MULT'){
validateNext(
Array(
Token(new Number),
Token('(')
)
);
parseRemaining();
}else if(current_token.type === 'DIV'){
validateNext(Token(new Number));
if(current_token.value === 0){
throw('ERRO SINTÁTICO: DIVISÃO POR ZERO NA POSIÇÃO <br>jhgjhg"' + scn.getPos() + '\"!');
}
parseRemaining();
}else if(current_token.type === 'NUM'){
validateNext(
Array(
Token('+'),
Token('-'),
Token('*'),
Token('/'),
Token(')'),
Token('EOF')
)
);
parseRemaining();
}else if(current_token.type === 'ABRE_PARENTESES'){
validateNext(Token(new Number));
parseRemaining();
}else if(current_token.type === 'FECHA_PARENTESES'){
validateNext(
Array(
Token('+'),
Token('-'),
Token('*'),
Token('/'),
Token(')'),
Token('EOF')
)
);
parseRemaining();
}
};
validateNext(Token(new Number));
parseRemaining();
return 'EXPRESSÃO CORRETA!';
};
//--------------------------------------------------------------------------------------------------------------------------
let Assembly = function(input){
let pos = 0;
let contvaria = 0;
let cont = 0;
let cont2 = 0;
var num = [];
var expre = [];
let read_token;
while(pos < input.length){
while(input[pos] === ' ' || input[pos] === '\t' || input[pos] === '\n'){
pos++;
}
switch(input[pos]){
case '-': {
pos++;
if(!isNumber(input[pos])){
read_token = Token('-');
expre[cont2] = '-';
cont2++;
}
else{
let number = '-' + input[pos];
pos++;
while(pos < input.length && isNumber(input[pos])){
number += input[pos];
pos++;
}
num[contvaria] = number;
contvaria++;
expre[cont2] = number;
cont2++;
}
break;
}
case '(':{
expre[cont2] = input[pos];
cont2++;
pos++;
break;
}
case ')':{
expre[cont2] = input[pos];
cont2++;
pos++;
break;
}
case '+':{
expre[cont2] = input[pos];
cont2++;
pos++;
break;
}
case '*':{
expre[cont2] = input[pos];
cont2++;
pos++;
break;
}
case '/':{
expre[cont2] = input[pos];
cont2++;
pos++;
break;
}
default: {
if(isNumber(input[pos])){
let number = input[pos];
pos++;
while(pos < input.length && isNumber(input[pos])){
number += input[pos];
pos++;
}
num[contvaria] = number;
contvaria++;
expre[cont2] = number;
cont2++;
}else{
}
break;
}
}
}
let token = input[pos];
cont = 0;
let registra_variaveis = '';
while(cont < contvaria){
registra_variaveis += '\nvar' + (cont+1) +': dq ' + num[cont];
cont++;
}
cont = 0;
let expret = '';
while(cont < cont2){
expret += expre[cont];
cont++;
}
window.alert(expret + '\nCódigo em Assembly: ' +
' \n\nSECTION .data' +
' ' + registra_variaveis +
'\nfmt: db "%s%e,",10,0' +
'\n\nSECTION .bss \nc: resq 1\nr: resq 1\nop1: resq 1\nop2: resq 1' +
'\n\nSECTION .text\nglobal main\nmain: \npush rbp' +
'\n ' + //aqui vem os comados para calculo da expressão
'\npabc\npop rbp\nmov rax,0\nret');
};
//--------------------------------------------------------------------------------------------------------------------------
$(document).ready(function(){
$('.btn').click(function(){
$('.btn').removeClass('active');
$(this).addClass('active');
});
$('#verify-scanner').click(function(){
try{
let scn = Scanner($('#expression').val())
,output = '';
do{
t = scn.readNext();
output += t.type+': '+t.value+', ';
}while(t.type !== Token('EOF').type)
output = output.substring(0, output.length-2);
$('#result-scanner').html(output).parent().attr('class', 'alert alert-success');
}catch(e){
$('#result-scanner').html(e).parent().attr('class', 'alert alert-danger');
}
});
$('#verify-parser').click(function(){
try{
let prs = Parser($('#expression').val());
$('#result-parser').html(prs).parent().attr('class', 'alert alert-success');
}catch(e){
$('#result-parser').html(e).parent().attr('class', 'alert alert-danger');
}
});
$('#verify-assembly').click(function(){
let aprs = Assembly($('#expression').val());
});
});
</script>
</body>
</html>