Skip to content

Commit

Permalink
optimize state class (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
Hagar-Usama authored Oct 15, 2022
1 parent 9db110e commit 5993c69
Showing 1 changed file with 2 additions and 60 deletions.
62 changes: 2 additions & 60 deletions modules/State.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import collections
from modules.color_print import print_blue, print_green, print_purple, print_red, print_yellow
import time
from collections import defaultdict

class State:
def __init__(self):
Expand Down Expand Up @@ -28,7 +26,6 @@ def simulate_dfa_2(self, input_list, prev_list):


DEAD_STATE = 0
#print_blue("enter simulation")

tokens = []
tokens_accept = []
Expand All @@ -42,21 +39,17 @@ def simulate_dfa_2(self, input_list, prev_list):

while input_list:
tok = input_list.pop(0)
#print_green(f"token is {tok}")

tokens.append(tok)

s = self.get_next_state(s, tok)

#print_green(f"next state is: {s}")

if s != DEAD_STATE:
if self.is_accepted(s):
tokens_accept.append(tokens.copy())
#print('accept')

else:
#print("Dead state")
tokens = []
input_list.insert(0,tok)

Expand All @@ -75,52 +68,30 @@ def simulate_dfa(self, input_list):
input_list = list(input_list)

DEAD_STATE = 0
#print_blue("enter simulation")

tokens = []
tokens_accept = []

s = self.init_state
for i in input_list:

#print_green(f"i is {i}")
tokens.append(i)
#print_green(tokens)

s = self.get_next_state(s,i)
#print_green(f"next state is: {s}")
if s != DEAD_STATE:
if self.is_accepted(s):
tokens_accept.append(tokens.copy())
#tokens = []
#print('accept')
#return 'accept'

else:
tokens = []
if tokens_accept:
self.accepted_tokens.append(tokens_accept[-1])
tokens_accept = []




#print("Dead state")

#print(tokens_accept)
"""
if tokens_accept:
print_purple(tokens_accept)
return tokens_accept[-1]
else:
return []
"""

def is_accepted(self, state):
return state in self.accept_states

def get_next_state(self, current_state, input_token):
#a_dict.get('missing_key', 'default value')
x = self.dfa_table.get(current_state,0)
if x:
return x.get(input_token, 0)
Expand All @@ -132,69 +103,40 @@ def get_next_state(self, current_state, input_token):
def build_DFA(DFA_dict, init_state):


#print_yellow(DFA_dict)
visited = set()
#istate = frozenset(init_state)
istate = init_state
state_list = [istate]
accept_cond = get_accept_condition(DFA_dict)
accept_states = set()
trans = {}
dfa_table = collections.defaultdict(dict)
dfa_table = defaultdict(dict)


#print("*.*"*12)
while state_list:

#print(f"state_list: {state_list}")
#print_green(f"visited: {visited}")
state = state_list.pop(0)
#print_blue(f"current state is {state}")
state_trans = set()

visited.add(frozenset(state))


ip_dict = dfa_aux(DFA_dict, state)
#print_purple(f"ip_dict is: {ip_dict}")
# time.sleep(1)
for i in ip_dict:
if i != '#':
to_state = set()

# set for each character
for j in ip_dict[i]:
# get its follow and update the set
#print_green(f"j is {j}")
#print_yellow(f"follow of j{DFA_dict[j][1]}")
to_state.update(DFA_dict[j][1])
#to_state.update({"*"})

# check if it is an accept state
if accept_cond in to_state:
#print_red(f"it accept {to_state}")
accept_states.add(frozenset(to_state))


#print(f"DFA[{state}][{i}] = {to_state}")

dfa_table[frozenset(state)][i] = frozenset(to_state)


if (to_state not in state_list) and (to_state not in visited):
state_list.append(to_state)

else:
#print("#")
pass

#print("*.*"*12)

#print(dfa_table)

for key in dfa_table:
#print_yellow(f"key: {key} ---> value{dfa_table[key]}")
pass


return dfa_table, accept_states
Expand Down

0 comments on commit 5993c69

Please sign in to comment.