-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexical_aux.py
202 lines (144 loc) · 4.77 KB
/
lexical_aux.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
import os
import sys
from modules.RegExp import RegExp, postfix_me
from modules.Node_AST import build_AST_tree, eval_followpos, get_node_dict, pre_followpos
from modules.State import DFA, build_DFA
from modules.color_print import print_yellow
def get_current_directory():
# TODO: check why do we need this function
# is there a better alternative?
path = os.getcwd()
return path
def write_file(path_file, output_list):
os.makedirs(os.path.dirname(path_file), exist_ok=True)
with open(path_file, 'w+') as filehandle:
for listitem in output_list:
filehandle.write('%s\n' % listitem)
def build_my_tree(exp, operators):
# build RE and concats
r = RegExp(exp, operators, star="STAR")
mod_list = r.handle_exp()
# eval postfix expression for the AST
post = r.get_postfix()
# I do not add # above to avoid some confusion
post.append("#")
post.append("CONCAT")
# now build AST
tree = build_AST_tree(post, operators)
return tree
def reverse_dict(the_dict):
# TODO: why are we reversing the dict?
keys = list(the_dict.keys())
keys.reverse()
values = list(the_dict.values())
values.reverse()
new_dict = dict(zip(keys, values))
return new_dict
def expand_my_tree(tree, REs, pn_kw, operators):
# add the REs !!
REs = postfix_me(REs, operators)
for term, exp in REs.items():
tree.attach_node(term, exp)
# add keywords and punctuations
tree.implant_node(tree, pn_kw)
tree.assign_id()
def eval_tree(tree):
# get firstpos and lastpos and nullables
pre_followpos(tree)
# store in root the ids for leaves
get_node_dict(tree)
# evaluate followpos for the DFA
eval_followpos(tree)
def dfa_mine(tree):
# get a dict for id: (name , followpos)
DFA_dict = tree.get_DFA_dict()
# prepare for building the DFA
# the firstpos of root is the first state in the DFA
root_s = tree.firstpos
# now, let's build our DFA
dfa_table, accept_states = build_DFA(DFA_dict, root_s)
# create your DFA machine
machine = DFA(dfa_table, accept_states, frozenset(root_s))
return machine
def get_tokens(machine, input_lists):
ac_tok = []
for tok in input_lists:
machine.accepted_tokens = []
machine.simulate_dfa_2(tok, [])
accepted_tokens = machine.accepted_tokens
ac_tok = ac_tok + accepted_tokens
return ac_tok
def build_ouput_file(accepted_tokens, detection_table):
symbol_table = []
for i in accepted_tokens:
str_d = detection_table[''.join(i)]
symbol_table.append(str_d)
return symbol_table
def get_tokens_sole(machine, tok):
ac_tok = []
token_temp = tok.copy()
machine.accepted_tokens = []
machine.simulate_dfa_2(tok, [])
accepted_tokens = machine.accepted_tokens
ac_tok += accepted_tokens
ac_tok2 = [''.join(x) for x in ac_tok]
ac_tok2 = ''.join(ac_tok2)
token_temp2 = ''.join(token_temp)
return ac_tok2 == token_temp2
def list_to_str(the_list):
new_list = []
for i in the_list:
new_list.append(''.join(i))
return new_list
######################################
## consider adding to Lexical Class ##
######################################
def get_table_dict(dfa_tab):
"""
takes dfa table and returns dict of unique names for states
"""
identifier = 0
table_dict = {}
for i in dfa_tab:
table_dict[i] = 's' + str(identifier)
identifier += 1
return table_dict
def print_dfa_trans(dfa_tab, table_dict):
for k, v in dfa_tab.items():
d = table_dict.get(k)
if not d:
d = 's' + str(len(dfa_tab))
print_yellow(f"\n{d}")
for key, value in v.items():
d = table_dict.get(value)
if not d:
d = 's' + str(len(dfa_tab))
t = (key, d)
print(t, end='\t')
print("")
def get_start_accept(start_state, accept_states, table_dict):
""" returns start and accept states with unique names """
s = table_dict.get(start_state)
accept = set()
for i in accept_states:
a = table_dict.get(i)
if a:
accept.add(a)
return s, accept
def get_arg(param_index, default=None):
"""
Gets a command line argument by index (note: index starts from 1)
If the argument is not supplies, it tries to use a default value.
If a default value isn't supplied, an error message is printed
and terminates the program.
"""
try:
return sys.argv[param_index]
except IndexError as e:
if default:
return default
else:
print(e)
print(
f"[FATAL] The comand-line argument #[{param_index}] is missing")
exit(-1) # Program execution failed.