-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.py
128 lines (97 loc) · 3.96 KB
/
Main.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
from HulkGrammar import *
from Automata import *
from Lexer import *
from Parser import *
from Grammar import *
from ParserLR1 import *
from Regex import *
from Semantic_checker import *
from Visitor import *
from ParserSLR1 import *
def init():
#region Inicialization of the grammar
G,lexer= HulkGrammar()
parser = SLR1Parser(G,True)
#Pruebas sin errores
texts=['42;' ,'print(42);','print((((1 + 2) ^ 3) * 4) / 5);','print("Hello World");','print("The message is \"Hello World\"");',
'print("The meaning of life is " @ 42);','print(sin(2 * PI) ^ 2 + cos(3 * PI / log(4, 64)));',
'function tan(x) => sin(x) / cos(x);',
'function operate(x, y) {print(x + y);print(x - y);print(x * y);print(x / y);}',
'let ifmsg = "Hello World" in print(ifmsg);','let number = 42, text = "The meaning of life is" in print(text @ number);',
'let number = 42 in let text = "The meaning of life is" in print(text @ number);',
'let number = 42 in (let text = "The meaning of life is" in (print(text @ number)));',
'let a = 6, b = a * 7 in print(b);','let a = 6 in let b = a * 7 in print(b);',
'let a = 5, b = 10, c = 20 in {print(a+b);print(b*c);print(c/a);};','let a = (let b = 6 in b * 7) in print(a);',
'print(let b = 6 in b * 7);','let a = 20 in {let a = 42 in print(a);print(a);};','let a = 7, a = 7 * 6 in print(a);',
'let a = 7 in let a = 7 * 6 in print(a);','let a = 0 in {print(a);a := 1;print(a);};',
'let a = 42 in if (a % 2 == 0) print("Even") else print("odd");',
'if(5>4){print("es mayor");}else{print("es menor");};',
'let a=2 in while(a<5){print(a);a:=a+1;};',
'let b=[1,2,3,4,5] in for(i in b){print(i);};',
'function tan(x) {sin(x) / cos(x);} tan(23);',
'function lala(x) {print(x);print(x+1);} lala(5);',
'let a=2,b=3 in if(a<b & a!=5){print("hola");}else{print("adios");};'
]
#Pruebas con errores
# texts=['while(5){print("hola");};',
# 'if(5){print("hola");}else{print("la");};',
# ' 5 + (b & c);',
# '(200< sqrt(25)) + 20;',
# 'let a=2 in if(a<3){print("hola");}elif(4){print("adios");} else{print("hola");};'
# ]
#endregion
#region Parsing
parserslist = []
operationslist=[]
tokenslist = []
for i in texts:
l=1
lines=[]
tokens = lexer(i)
tokenslist.append(tokens)
tokens_type = []
for j in tokens:
if j.token_type!='space' and j.token_type!='line':
tokens_type.append(j.token_type)
lines.append(l)
if j.token_type=='line':
l+=1
parse,operations = parser(tokens_type, lines, get_shift_reduce=True)
parserslist.append(parse)
operationslist.append(operations)
#endregion
#region Semantic Checker
astlist = []
for j in range(len(parserslist)):
new_tokens = []
for i in tokenslist[j]:
if(i.token_type!='space' and i.token_type!='line'):
new_tokens.append(i)
ast = evaluate_reverse_parse(parserslist[j], operationslist[j], new_tokens)
astlist.append(ast)
print("AST: \n")
formatter = FormatVisitor()
for i in range(len(astlist)):
print(formatter.visit(astlist[i]))
print("\n")
print("Semantic Checker Errors: \n")
for i in range(len(astlist)):
print(texts[i])
semantic_checker = SemanticCheckerVisitor()
errors = semantic_checker.visit(astlist[i])
for j, error in enumerate(errors,1):
print(f'{j}.', error)
#endregion
#region Semantic Checker Evaluate
print("\nSemantic Checker Evaluate: \n")
for i in range(len(astlist)):
print(texts[i]+" : \n")
semantic_checker = SemanticCheckerEvaluate()
results = semantic_checker.visit(astlist[i])
results=semantic_checker.results
for j, res in enumerate(results,1):
if(res != None):
print(res)
print ("\n")
#endregion
init()