Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/venv python
- import sys
- def check_epsilons(states):
- # ona će odma dodavati epsilon stanja u states
- next_states = []
- for state in states:
- key = state + "," + "$"
- if key in transitions:
- next_states.append(transitions[key].split(",")) # s2,$ -> s6
- return next_states
- #vraća listu sljedećih stanja za trenutno stanje i simbol
- def transition(states, symbol):
- key = state + "," + symbol
- if key in transitions:
- next_states = transitions[key].split(",") # next_states = [ p5, s4 ]
- epsilon_states = next_states
- while epsilon_states:
- #vrti se petlja, dok više nema sljedećih epsilon prijelaza
- print(epsilon_states)
- epsilon_states = check_epsilons(epsilon_states)
- next_states.extend(epsilon_states) # [ s2, s6 ]
- else:
- # ako nema definiranog prijelaza, onda ide u #
- next_states = [ '#' ]
- return next_states
- lines = sys.stdin.readlines()
- lines = [x.rstrip() for x in lines]
- #definition of the epsilon-nfa
- input_strings = lines[0].split("|")
- states = lines[1].split(",")
- symbols = lines[2].split(",")
- final_states = lines[3].split(",")
- initial_state = lines[4]
- transitions = {}
- for i in range(5, len(lines)):
- transitions[lines[i].split("->")[0]] = lines[i].split("->")[1]
- #beginning of the automaton
- for input_string in input_strings:
- input_symbols = input_string.split(",")
- current_states = []
- current_states.append(initial_state)
- output = ""
- output = initial_state + "|"
- for current_symbol in input_symbols:
- #vrtimo trenutna stanja i provjeravamo prijelaze za simbol i stanje
- next_states = set()
- for state in current_states:
- tmp = transition(state, current_symbol)
- tmp = set(tmp)
- #sljedeća stanja za to neko trenutno stanje
- #tu ide print
- for x in tmp:
- output += x + ","
- output = output[:-1]
- output += "|"
- next_states.update(tmp)
- current_states.clear()
- current_states.extend(next_states)
- output.rstrip("|")
- print(output)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement