Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.29 KB | None | 0 0
  1. #!/usr/bin/venv python
  2.  
  3. import sys
  4.  
  5.  
  6. def check_epsilons(states):
  7.     # ona će odma dodavati epsilon stanja u states
  8.    
  9.     next_states = []
  10.  
  11.     for state in states:
  12.         key = state + "," + "$"
  13.  
  14.         if key in transitions:
  15.             next_states.append(transitions[key].split(",")) # s2,$ -> s6
  16.    
  17.     return next_states
  18.  
  19.                
  20. #vraća listu sljedećih stanja za trenutno stanje i simbol
  21. def transition(states, symbol):
  22.     key = state + "," + symbol
  23.  
  24.     if key in transitions:
  25.         next_states = transitions[key].split(",") # next_states = [ p5, s4 ]
  26.  
  27.         epsilon_states = next_states
  28.  
  29.         while epsilon_states:
  30.             #vrti se petlja, dok više nema sljedećih epsilon prijelaza
  31.             print(epsilon_states)
  32.             epsilon_states = check_epsilons(epsilon_states)
  33.             next_states.extend(epsilon_states) # [ s2, s6 ]
  34.  
  35.     else:
  36.         # ako nema definiranog prijelaza, onda ide u #
  37.         next_states = [ '#' ]
  38.        
  39.     return next_states
  40.  
  41.  
  42. lines = sys.stdin.readlines()
  43. lines = [x.rstrip() for x in lines]
  44.  
  45. #definition of the epsilon-nfa
  46.  
  47. input_strings = lines[0].split("|")
  48. states = lines[1].split(",")
  49. symbols = lines[2].split(",")
  50. final_states = lines[3].split(",")
  51. initial_state = lines[4]
  52.  
  53. transitions = {}
  54.  
  55. for i in range(5, len(lines)):
  56.     transitions[lines[i].split("->")[0]] = lines[i].split("->")[1]
  57.  
  58.  
  59. #beginning of the automaton
  60.  
  61. for input_string in input_strings:
  62.    
  63.     input_symbols = input_string.split(",")
  64.     current_states = []
  65.     current_states.append(initial_state)
  66.    
  67.     output = ""
  68.     output = initial_state + "|"
  69.  
  70.     for current_symbol in input_symbols:
  71.         #vrtimo trenutna stanja i provjeravamo prijelaze za simbol i stanje
  72.         next_states = set()
  73.         for state in current_states:
  74.             tmp = transition(state, current_symbol)
  75.            
  76.             tmp = set(tmp)
  77.             #sljedeća stanja za to neko trenutno stanje
  78.             #tu ide print
  79.             for x in tmp:
  80.                 output += x + ","
  81.             output = output[:-1]
  82.             output += "|"
  83.             next_states.update(tmp)
  84.            
  85.         current_states.clear()
  86.         current_states.extend(next_states)
  87.    
  88.     output.rstrip("|")
  89.     print(output)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement