-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
129 lines (91 loc) · 3.99 KB
/
main.py
File metadata and controls
129 lines (91 loc) · 3.99 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
from src.lexical_analizer import tokenize_cool_text
from src.cool_grammar import define_cool_grammar
from src.cool_visitor import FormatVisitorST
from src.type_collector import TypeCollector
from src.type_builder import TypeBuilder
from src.type_checker import TypeChecker
from src.tset_builder import TSetBuilder
from src.tsets_reducer import TSetReducer
from src.tset_merger import TSetMerger
from src.shift_reduce_parsers import LR1Parser, DerivationTree
from src.errors import parsing_table_error, Error
from src.cmp.evaluation import evaluate_reverse_parse
import streamlit as st
from src.ui import print_array, print_grammar, print_tset
st.sidebar.header("About")
st.sidebar.subheader("Segundo Proyecto de Compilacion: Cool Intrepreter")
st.sidebar.text("Amalia Ibarra Rodríguez")
st.sidebar.text("Gabriela B. Martínez Giraldo")
st.sidebar.text("Grupo: C-312")
st.sidebar.text("Curso: 2019-2020")
""" # Cool Intrepreter """
data = st.text_area("Type some code", "")
run_analysis = st.button("compile", "")
initial_ast = "Initial_AST"
final_ast = "Final_AST"
reduced_sets = "ReducedSets"
selected_options = st.multiselect(
"Select what to see", (initial_ast, final_ast, reduced_sets),
)
def run_pipeline(text):
main_error1 = ["A class Main with a method main most be provided"]
main_error2 = ['"main" method in class Main does not receive any parameters']
# define grammar
grammar, idx, string, num = define_cool_grammar()
try:
tokens = tokenize_cool_text(grammar, idx, string, num, text)
parser = LR1Parser(grammar)
parse, operations = parser([t.token_type for t in tokens])
# print("\n".join(repr(x) for x in parse))
# print(operations)
ast = evaluate_reverse_parse(parse, operations, tokens)
formatter = FormatVisitorST()
tree = formatter.visit(ast)
if initial_ast in selected_options:
st.header("Initial Tree:")
print_array(tree)
errors = []
collector = TypeCollector(errors)
collector.visit(ast)
context = collector.context
builder = TypeBuilder(context, errors)
builder.visit(ast)
checker = TypeChecker(context, errors)
checker.visit(ast, None)
if errors != [] and errors != main_error1 and errors != main_error2:
st.header("Sorry we found some errors in your code:")
print_array(errors)
else:
if errors == main_error1 or errors == main_error2:
st.header("Warning")
st.write("We will continue the analisis but note that:")
st.write(errors[0])
errors = []
tset_builder = TSetBuilder(context, errors)
tset = tset_builder.visit(ast, None)
tset_reducer = TSetReducer(context, errors)
reduced_set = tset_reducer.visit(ast, tset)
tset_merger = TSetMerger(context, errors)
tset_merger.visit(ast, reduced_set)
collector = TypeCollector(errors)
collector.visit(ast)
context = collector.context
builder = TypeBuilder(context, errors)
builder.visit(ast)
checker = TypeChecker(context, errors)
checker.visit(ast, None)
if errors != [] and errors != main_error1 and errors != main_error2:
st.header("Sorry we found some errors in your code:")
print_array(errors)
if reduced_sets in selected_options:
st.header("Reduced Sets")
print_tset(reduced_set)
if final_ast in selected_options:
tree = formatter.visit(ast)
st.header("Final Tree:")
print_array(tree)
except Error as error:
st.header("Sorry an error occur while tokenizing text:")
st.write(str(error))
if run_analysis:
run_pipeline(data)