Formal Definition of a Finite Automaton
// the five tuples
const Q = states = new Set(['q1', 'q2', 'q3'])
const Sigma = alphabet = new Set(['0', '1'])
const delta = transitionFunction = (state, symbol) => {
const table = {
'q1': {
'0': 'q1',
'1': 'q2'
},
'q2': {
'1': 'q2',
'0': 'q3'
},
'q3': {
'1': 'q2',
'0': 'q2'
}
}
return table[state][symbol]
}
const q0 = startState = 'q1'
const F = finalStates = new Set(['q2'])
const check = (string) => {
let arr = string.split("")
let currState = q0
for (let i = 0; i < arr.length; i++ ) {
currState = delta(currState, arr[i])
}
return F.has(currState) ? 'accept' : "reject"
}
no comments