Ο προηγούμενος κώδικας είναι κάπως αλλαγμένος.
def getchar(text, pos):
""" returns char at pos of words, or None if out of bounds """
if pos < 0 or pos >= len(text):
return None
return text[pos]
def scan(text, transition_table, accept_states):
""" scans `text` while transitions exist in
'transition_table'. After that, if in a state belonging to
`accept_states`, it returns the corresponding token, else ERROR.
"""
# initial state
pos = 0
state = 's0'
while True:
if len(text) > 8: # more characters than it is accepted
return 'ERROR', 8
c = getchar(text, pos) # get next char
if c in transition_table[state]:
state = transition_table[state][c] # set new state
pos += 1 # advance to next char
# check if new state is accepting
if state in accept_states and pos == len(text):
return 'TIME_TOKEN', pos
else: # no transition found
return 'ERROR', pos
# the transition table, as a dictionary
td = {'s0': {'0': 's1', '1': 's1', '2': 's2', '3': 's3', '4': 's3', '5': 's3',
'6': 's3', '7': 's3', '8': 's3', '9': 's3'},
's1': {'0': 's3', '1': 's3', '2': 's3', '3': 's3', '4': 's3', '5': 's3', '6': 's3',
'7': 's3', '8': 's3', '9': 's3', '.': 's4', ',': 's4', ':': 's4'},
's2': {'0': 's3', '1': 's3', '2': 's3', '3': 's3', '.': 's4', ',': 's4', ':': 's4'},
's3': {'.': 's4', ',': 's4', ':': 's4'},
's4': {'0': 's5', '1': 's5', '2': 's5', '3': 's5', '4': 's5', '5': 's5'},
's5': {'0': 's6', '1': 's6', '2': 's6', '3': 's6', '4': 's6', '5': 's6',
'6': 's6', '7': 's6', '8': 's6', '9': 's6'},
's6': {'.': 's7', ',': 's7', ':': 's7'},
's7': {'0': 's8', '1': 's8', '2': 's8', '3': 's8', '4': 's8', '5': 's8'},
's8': {'0': 's9', '1': 's9', '2': 's9', '3': 's9', '4': 's9', '5': 's9',
'6': 's9', '7': 's9', '8': 's9', '9': 's9'}}
# the accepted states
ad = ['s6', 's9']
# get a string from input
words = input('give some input>')
# scan text until no more input
while len(words) > 0:
# get next token and position after last char recognized
tok, pos = scan(words, td, ad)
if tok == 'ERROR':
msg = 'unrecognized input at pos', pos, 'of', words
print(msg)
# with open("log.txt", "a") as log:
# log.write('Input: {}\nOutput: {}\n\n'.format(words, msg))
break
msg = "token:", tok, "text:", words[:pos]
print(msg)
# with open("log.txt", "a") as log:
# log.write('Input: {}\nOutput: {}\n\n'.format(words, msg))
# new text for next scan
words = words[pos:]