-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
728 lines (601 loc) · 23.1 KB
/
app.py
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
from flask import Flask, render_template, request, url_for
from flask_bootstrap import Bootstrap
app = Flask(__name__)
Bootstrap(app)
def convert(a):
final_ans = []
# Starting latex lines
final_ans.append("\\documentclass{article}")
final_ans.append("\\usepackage[utf8]{inputenc}")
final_ans.append("\\usepackage{algorithm}")
final_ans.append("\\usepackage{algpseudocode}")
final_ans.append("\n")
final_ans.append("\\title{<Title>}")
final_ans.append("\\author{<Your Name>}")
final_ans.append("\n")
final_ans.append("\\begin{document}")
final_ans.append("\\maketitle")
final_ans.append("\n")
final_ans.append("\n")
final_ans.append("\\begin{algorithm}")
# Removing empty spaces from lines
for i in range(0, len(a)):
a[i] = a[i].strip()
# Extracting useless lines before function name like blank lines and comments
while len(a) > 0:
# Removing blank lines
if len(a[0]) == 0:
a.pop(0)
else:
if len(a[0]) >= 2:
# Removing single line comments
if a[0][0] == "/" and a[0][1] == "/":
strin = ""
a[0] = a[0][2:]
a[0] = a[0].strip()
strin += a[0]
strin = strin.strip()
strin = "\\Comment{" + strin + "}"
final_ans.append(strin)
a.pop(0)
else:
# Removing multiple line comments
if a[0][0] == "/" and a[0][1] == "*":
strin = ""
# Removing multiple line comments that appear on single line
if len(a[0]) >= 4 and a[0][-2] == "*" and a[0][-1] == "/":
a[0] = a[0][2:-2]
a[0] = a[0].strip()
strin += a[0]
strin += " "
a.pop(0)
# Removing multiple line comments that appear on different multiple line
else:
a[0] = a[0][2:]
a[0] = a[0].strip()
strin += a[0]
strin += " "
a.pop(0)
while len(a) > 0:
if len(a[0]) >= 2:
if a[0][-2] == "*" and a[0][-1] == "/":
a[0] = a[0][0:-2]
a[0] = a[0].strip()
strin += a[0]
strin += " "
a.pop(0)
break
else:
a[0] = a[0].strip()
strin += a[0]
strin += " "
a.pop(0)
else:
a.pop(0)
strin = strin.strip()
strin = "\\Comment{" + strin + "}"
final_ans.append(strin)
else:
break
else:
break
# Extracting function from code
function_name = ""
index = 0
for i in range(0, len(a[0])):
if a[0][i] == "(":
index = i
break
function_name = a[0][:index]
function_name = function_name.strip()
a[0] = a[0][index:]
index = 0
for i in range(len(function_name) - 1, -1, -1):
if function_name[i] == " ":
index = i
break
function_name = function_name[index + 1 :]
function_name1 = "\\Procedure{" + function_name + "}{"
function_name = "\\caption{" + function_name + "}"
final_ans.append(function_name)
final_ans.append("\\begin{algorithmic}")
# Extracting function parameters from code
a[0] = a[0][1:]
func_parameters = ""
while len(a) > 0:
index = 0
a[0] = a[0].strip()
if len(a[0]) == 0:
a.pop(0)
continue
for i in range(0, len(a[0])):
if a[0][i] == "," or a[0][i] == ")":
index = i
break
parame = a[0][:index]
a[0] = a[0][index:]
parame = parame.strip()
for i in range(len(parame) - 1, -1, -1):
if parame[i] == " ":
index = i
break
parame = parame[index + 1 :]
if len(parame) > 0:
if parame[0] == "&":
parame = parame[1:]
parame = parame + ","
func_parameters = func_parameters + parame
if a[0][0] == ")":
a[0] = a[0][1:]
break
else:
a[0] = a[0][1:]
func_parameters = func_parameters[:-1]
function_name1 = function_name1 + func_parameters + "}"
final_ans.append(function_name1)
a[0] = a[0].strip()
if len(a) > 0 and len(a[0]) >= 2:
if a[0][0] == "/" and a[0][1] == "/":
a[0] = a[0][2:]
a[0] = a[0].strip()
strin = ""
strin = "\\Comment{" + a[0] + "}"
final_ans.append(strin)
a.pop(0)
if len(a) > 0 and len(a[0]) >= 4:
if a[0][0] == "/" and a[0][1] == "*" and a[0][-2] == "*" and a[0][-1] == "/":
a[0] = a[0][2:-2]
a[0] = a[0].strip()
strin = ""
strin = "\\Comment{" + a[0] + "}"
final_ans.append(strin)
a.pop(0)
# Pseudo Code for main code inside function
procedure_list = ["\\EndProcedure"]
while len(a) > 0:
a[0] = a[0].strip()
if len(a[0]) == 0:
a.pop(0)
continue
if a[0][0] == "{":
a[0] = a[0][1:]
continue
if a[0][0] == "}":
final_ans.append(procedure_list[-1])
procedure_list.pop()
a[0] = a[0][1:]
continue
if len(a[0]) >= 2:
# Removing single line comments
if a[0][0] == "/" and a[0][1] == "/":
strin = ""
a[0] = a[0][2:]
a[0] = a[0].strip()
strin += a[0]
strin = strin.strip()
strin = "\\Comment{" + strin + "}"
final_ans.append(strin)
a.pop(0)
continue
if len(a[0]) >= 6:
if a[0][0:6] == "return":
a[0] = a[0][:-1]
a[0] = a[0].strip()
a[0] = a[0][6:]
a[0] = a[0].strip()
retur = "\\State \\Return {" + a[0] + "}"
a.pop(0)
final_ans.append(retur)
continue
if len(a[0]) >= 4:
# Removing multiple line comments that appear on single line
if a[0][0] == "/" and a[0][1] == "*":
strin = ""
if a[0][-2] == "*" and a[0][-1] == "/":
a[0] = a[0][2:-2]
a[0] = a[0].strip()
strin += a[0]
strin += " "
strin = strin.strip()
strin = "\\Comment{" + strin + "}"
final_ans.append(strin)
a.pop(0)
continue
if len(a[0]) >= 2:
# Removing multiple line comments
if a[0][0] == "/" and a[0][1] == "*":
strin = ""
a[0] = a[0][2:]
a[0] = a[0].strip()
strin += a[0]
strin += " "
a.pop(0)
while len(a) > 0:
if len(a[0]) >= 2:
if a[0][-2] == "*" and a[0][-1] == "/":
a[0] = a[0][0:-2]
a[0] = a[0].strip()
strin += a[0]
strin += " "
a.pop(0)
break
else:
a[0] = a[0].strip()
strin += a[0]
strin += " "
a.pop(0)
else:
a.pop(0)
strin = strin.strip()
strin = "\\Comment{" + strin + "}"
final_ans.append(strin)
continue
# If Statement
if len(a[0]) >= 2:
# break
if a[0][0] == "i" and a[0][1] == "f":
procedure_list.append("\\EndIf")
a[0] = a[0][2:]
if_param = ""
count_para = 0
while len(a) > 0:
check = False
a[0] = a[0].strip()
for i in range(0, len(a[0])):
if a[0][i] == "(":
count_para = count_para + 1
if a[0][i] == ")":
count_para = count_para - 1
if count_para == 0:
check = True
if_param = if_param + a[0][:i]
a[0] = a[0][i + 1 :]
break
if check:
break
else:
if_param = if_param + a[0]
a.pop(0)
if_param = if_param[1:]
if_param = if_param.strip()
if_param = "$ " + if_param + " $"
if_param = "\\If{" + if_param + "}"
final_ans.append(if_param)
continue
# Else Statement
if len(a[0]) >= 4:
if a[0][0:4] == "else":
# break
procedure_list.append("\\EndIf")
final_ans = final_ans[:-1]
a[0] = a[0][4:]
a[0] = a[0].strip()
if len(a[0]) >= 2 and a[0][0:2] == "if":
a[0] = a[0][2:]
if_param = ""
count_para = 0
while len(a) > 0:
check = False
a[0] = a[0].strip()
for i in range(0, len(a[0])):
if a[0][i] == "(":
count_para = count_para + 1
if a[0][i] == ")":
count_para = count_para - 1
if count_para == 0:
check = True
if_param = if_param + a[0][:i]
a[0] = a[0][i + 1 :]
break
if check:
break
else:
if_param = if_param + a[0]
a.pop(0)
if_param = if_param[1:]
if_param = if_param.strip()
if_param = "$ " + if_param + " $"
if_param = "\\ElsIf{" + if_param + "}"
final_ans.append(if_param)
else:
final_ans.append("\\Else")
continue
# While Loop
if len(a[0]) >= 5:
if (
a[0][0] == "w"
and a[0][1] == "h"
and a[0][2] == "i"
and a[0][3] == "l"
and a[0][4] == "e"
):
procedure_list.append("\\EndWhile")
a[0] = a[0][5:]
if_param = ""
count_para = 0
while len(a) > 0:
check = False
a[0] = a[0].strip()
for i in range(0, len(a[0])):
if a[0][i] == "(":
count_para = count_para + 1
if a[0][i] == ")":
count_para = count_para - 1
if count_para == 0:
check = True
if_param = if_param + a[0][:i]
a[0] = a[0][i + 1 :]
break
if check:
break
else:
if_param = if_param + a[0]
a.pop(0)
if_param = if_param[1:]
if_param = if_param.strip()
if_param = " " + if_param + " "
if_param = "\\While{" + if_param + "}"
final_ans.append(if_param)
continue
# For Loop
if len(a[0]) >= 3:
if a[0][0] == "f" and a[0][1] == "o" and a[0][2] == "r":
procedure_list.append("\\EndFor")
a[0] = a[0][3:]
if_param = ""
count_para = 0
while len(a) > 0:
check = False
a[0] = a[0].strip()
for i in range(0, len(a[0])):
if a[0][i] == "(":
count_para = count_para + 1
if a[0][i] == ")":
count_para = count_para - 1
if count_para == 0:
check = True
if_param = if_param + a[0][:i]
a[0] = a[0][i + 1 :]
break
if check:
break
else:
if_param = if_param + a[0]
a.pop(0)
if_param = if_param[1:]
if_param = if_param.strip()
if len(if_param) >= 4 and if_param[:4] == "auto":
if_param = if_param[4:]
if_param = if_param.strip()
x11 = ""
x12 = ""
for j1 in range(0, len(if_param)):
if if_param[j1] == ":":
x11 = if_param[:j1]
x12 = if_param[j1 + 1 :]
break
if_param = x11 + " in " + x12
if_param = "\\For{" + if_param + "}"
final_ans.append(if_param)
continue
temp = if_param.split(";")
if_param = ""
temp1 = temp[0].split("=")
temp1[0].strip()
temp2 = temp1[0].split(" ")
if_param = temp2[1] + " $\\leftarrow$ "
temp1[1].strip()
if_param = if_param + temp1[1]
temp[1].strip()
for j1 in range(len(temp[1]) - 1, -1, -1):
if temp[1][j1] == "=" or temp[1][j1] == ">" or temp[1][j1] == "<":
temp[1] = temp[1][j1 + 1 :]
break
temp[2].strip()
if temp[2][-1] == "+":
if_param = if_param + " to "
if temp[2][-1] == "-":
if_param = if_param + " downto "
if_param = if_param + temp[1]
if_param = " " + if_param + " "
if_param = "\\For{" + if_param + "}"
final_ans.append(if_param)
continue
# Input statement
if len(a[0]) >= 3:
if a[0][0:3] == "cin":
a[0] = a[0][0:-1]
temp = a[0].split(">>")
entered_element = ""
for i in range(1, (len(temp))):
temp[i] = temp[i].strip()
entered_element += temp[i]
entered_element += " "
if i != (len(temp) - 1):
entered_element += "and "
entered_element = "\\State \\textbf{input} " + entered_element
final_ans.append(entered_element)
a.pop(0)
continue
# Output Statement
if len(a[0]) >= 4:
if a[0][0:4] == "cout":
a[0] = a[0][0:-1]
temp = a[0].split("<<")
temp[-1] = temp[-1].strip()
if temp[-1] == "endl":
temp = temp[0:-1]
entered_element = "\\State \\textbf{print} "
for i in range(1, (len(temp))):
temp[i] = temp[i].strip()
entered_element += temp[i]
entered_element += " "
if i != (len(temp) - 1):
entered_element += "and "
final_ans.append(entered_element)
a.pop(0)
continue
if (
(len(a[0]) >= 3 and a[0][0:3] == "int")
or (
len(a[0]) >= 4
and (a[0][0:4] == "long" or a[0][0:4] == "auto" or a[0][0:4] == "char")
)
or (len(a[0]) >= 5 and (a[0][0:5] == "float" or a[0][0:5] == "short"))
or (len(a[0]) >= 6 and (a[0][0:6] == "signed" or a[0][0:6] == "double"))
or (len(a[0]) >= 8 and a[0][0:8] == "unsigned")
):
while len(a[0]) >= 6 and (a[0][0:6] == "signed"):
a[0] = a[0][6:]
a[0] = a[0].strip()
while len(a[0]) >= 8 and a[0][0:8] == "unsigned":
a[0] = a[0][8:]
a[0] = a[0].strip()
while len(a[0]) >= 5 and (a[0][0:5] == "float" or a[0][0:5] == "short"):
a[0] = a[0][5:]
a[0] = a[0].strip()
while len(a[0]) >= 4 and (
a[0][0:4] == "long" or a[0][0:4] == "auto" or a[0][0:4] == "char"
):
a[0] = a[0][4:]
a[0] = a[0].strip()
while len(a[0]) >= 6 and (a[0][0:6] == "double"):
a[0] = a[0][6:]
a[0] = a[0].strip()
while len(a[0]) >= 3 and a[0][0:3] == "int":
a[0] = a[0][3:]
a[0] = a[0].strip()
while len(a[0]) >= 6 and (a[0][0:6] == "signed"):
a[0] = a[0][6:]
a[0] = a[0].strip()
while len(a[0]) >= 8 and a[0][0:8] == "unsigned":
a[0] = a[0][8:]
a[0] = a[0].strip()
while len(a[0]) >= 5 and (a[0][0:5] == "float" or a[0][0:5] == "short"):
a[0] = a[0][5:]
a[0] = a[0].strip()
while len(a[0]) >= 4 and (
a[0][0:4] == "long" or a[0][0:4] == "auto" or a[0][0:4] == "char"
):
a[0] = a[0][4:]
a[0] = a[0].strip()
while len(a[0]) >= 6 and (a[0][0:6] == "double"):
a[0] = a[0][6:]
a[0] = a[0].strip()
while len(a[0]) >= 3 and a[0][0:3] == "int":
a[0] = a[0][3:]
a[0] = a[0].strip()
a[0] = a[0][0:-1]
temp = a[0].split(",")
for j in temp:
flag = False
j = j.strip()
for k in range(1, (len(j) - 1)):
if j[k] == "=":
flag = True
temp = "\\State " + j[0:k] + " $\\leftarrow$ " + j[k + 1 :]
final_ans.append(temp)
break
if not flag:
temp = "\\State Define a variable " + j
final_ans.append(temp)
a.pop(0)
continue
variable = ["vector", "list", "stack", "map", "queue", "priority_queue"]
exist = False
for var in variable:
if len(a[0]) >= len(var):
if a[0][: len(var)] == var:
a[0] = a[0][len(var) :]
paren_count = 0
a[0] = a[0].strip()
for i in range(0, len(a[0])):
if a[0][i] == "<":
paren_count = paren_count + 1
if a[0][i] == ">":
paren_count = paren_count - 1
if paren_count == 0:
a[0] = a[0][i + 1 :]
break
a[0] = a[0].strip()
a[0] = a[0][:-1]
a[0] = a[0].strip()
temp = a[0].split(",")
for i in temp:
i = i.strip()
strin = "\\State Define a " + var + " " + i
final_ans.append(strin)
a.pop(0)
exist = True
break
if exist:
continue
statement = ""
for i in range(0, len(a[0])):
if a[0][i] == ";":
statement = a[0][:i]
a[0] = a[0][i + 1 :]
break
statement = statement.strip()
temp = statement.split("=")
statement = ""
for x12 in temp:
statement = statement + x12 + " $\\leftarrow$ "
if len(statement) >= 14:
statement = statement[:-14]
statement = "\\State " + statement
final_ans.append(statement)
final_ans.append("\\end{algorithmic}")
final_ans.append("\\end{algorithm}")
final_ans.append("\n")
final_ans.append("\\end{document}")
for i in range(0, len(final_ans)):
temp = final_ans[i].split("_")
strin = ""
for x in temp:
strin = strin + x + "\\_"
if len(strin) >= 2:
strin = strin[:-2]
final_ans[i] = strin
for i in range(0, len(final_ans)):
if len(final_ans[i]) >= 2:
if final_ans[i][-1] == "+" and final_ans[i][-2] == "+":
final_ans[i] = final_ans[i][:-2]
final_ans[i] = (
final_ans[i] + " $ \\leftarrow $ " + final_ans[i][7:] + " + 1"
)
continue
if final_ans[i][-1] == "-" and final_ans[i][-2] == "-":
final_ans[i] = final_ans[i][:-2]
final_ans[i] = (
final_ans[i] + " $ \\leftarrow $ " + final_ans[i][7:] + " - 1"
)
continue
# vect = ["<", ">", "=", "&"]
# for sig in vect:
# for i in range(0,len(final_ans)):
# temp = final_ans[i].split(sig)
# strin = ""
# for x in temp:
# strin = strin + x + "$ " + sig + " $"
# if(len(strin)>=5):
# strin = strin[:-5]
# final_ans[i]=strin
return final_ans
@app.route("/")
def index():
return render_template("index.html")
@app.route("/analyse", methods=["POST"])
def analyse():
message = "Input code is not in format as required, please change and retry."
try:
if request.method == "POST":
rawtext = request.form["rawtext"].split("\n")
a = rawtext
message = convert(a)
except:
render_template("index.html", message=message)
return render_template("index.html", message=message)
# return render_template('index.html', message=message)
if __name__ == "__main__":
app.run(debug=True)