예제 #1
0
def main():
    initial_state = "init",
    accepting_states = ["final"],
    transition_function = {("init","0"):("init", "1", "R"),
                           ("init","1"):("init", "0", "R"),
                           ("init"," "):("final"," ", "N"),
                           }
    final_states = {"final"}

    tm = TuringMachine("010011 ",
                      initial_state = initial_state,
                      final_states = final_states,
                      transition_function=transition_function)

    print("Input on Tape:{}\n".format(tm.get_tape()))

    while not tm.final():
        tm.step()

    print("Result of the Turing machine calculation:")
    print(tm.get_tape())
        print("Invalid input string.")
        exit()

initial_state = "q_0"
final_states = ["q_acc", "q_rej"]

# transition functions
transition_function = {
    ("q_0", "a"): ("q_0", "a", "R"),  # stay in q_0
    ("q_0", "b"):
    ("q_1", "b", "R"),  # transition to q_1 when 'b' is encountered
    ("q_0", "*"):
    ("q_rej", "*",
     "L"),  # no 'b's found and we reached the end. Reject and halt.
    ("q_1", "a"): ("q_1", "a", "R"),  # stay in q_1
    ("q_1", "b"):
    ("q_acc", "b", "R"),  # encountered second 'b'! Accept and halt.
    ("q_1", "*"): ("q_rej", "*", "L")  # only 1 'b' found. Reject and halt.
}

turing = TuringMachine(alphabet, input_string, "*", initial_state,
                       final_states, transition_function)

# step through Turing machine until a final state is reached.
while not turing.final_state():
    turing.step()

if turing.current_state == "q_acc":
    print(f"String {input_string} is accepted")
else:
    print(f"String {input_string} is rejected")