# 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") accept = function(string, automata) { stringlen = strlen(string) state = automata.iota for (i = 0; i < stringlen; i++) { j = 0 deltaLen = length(automata.delta) foundRule = 0 while (j < deltaLen && !foundRule) { if (state == automata.delta[j].start && string[i] == automata.delta[j].symbol) { state = automata.delta[j].end foundRule = 1 } j++ } } i = 0 foundFinal = 0 nbFinals = length(automata.F) while (i < nbFinals && !foundFinal) { if (state == automata.F[i]) foundFinal = 1 i++ } if (foundFinal) return("accepted") "refused" } accept("c", auto) accept("ac", auto) accept("aca", auto)