# PCASTLI version 1.8 auto = names("S", "Sigma", "delta", "iota", "F") # States auto.S = array("A", "B", "C") # Alphabet auto.Sigma = array("a", "b", "c") makerule = function(start, symbol, end) { a = names("start", "symbol", "end") a.start = start a.symbol = symbol a.end = end a } # Transition function auto.delta = array( makerule("A", "a", "B"), makerule("A", "b", "C"), makerule("A", "c", "A"), makerule("B", "a", "A"), makerule("B", "b", "B"), makerule("B", "c", "C"), makerule("C", "a", "A"), makerule("C", "b", "B"), makerule("C", "c", "C") ) # Start state auto.iota = "A" # Set of accept states auto.F = array("A", "B") run_automaton = function(automaton, string) { stringlen = strlen(string) state = automaton.iota i = 0 found_rule = 1 while (found_rule && i < stringlen) { j = 0 deltaLen = length(automaton.delta) found_rule = 0 while (!found_rule && j < deltaLen) { if (state == automaton.delta[j].start && string[i] == automaton.delta[j].symbol) { state = automaton.delta[j].end found_rule = 1 } j++ } i++ } i = 0 foundFinal = 0 nbFinals = length(automaton.F) while (i < nbFinals && !foundFinal) { if (state == automaton.F[i]) foundFinal = 1 i++ } if (foundFinal) return("accepted") "refused" } run_automaton(auto, "c") run_automaton(auto, "ac") run_automaton(auto, "aca")