def getchar(words,pos):
""" epistrefei ton xarakthra ths le3hs h tipota an einai ektos oriwn """
if pos<0 or pos>=len(words):
return None
if words[pos]==".":
return 'DOT'
elif words[pos]>='0' and words[pos]<='9':
return 'DIGIT'
return 'other'
def scan(text,transition_table,accept_states):
"""elegxei to text otan oi metafores uparxoun sto transition_table. meta apo auto an
mesa sthn katastash anhkei to accept_states, gurnaei kai allhlepidra me to token alliws error
"""
# arxikh katastash
pos = 0
state = 's0'
lasttoken,lastpos=None,None
while True:
c = getchar(text,pos) # de3ou neo xarakthra
if c in transition_table[state]:
state = transition_table[state][c] # 8ese nea katastash
pos += 1 # proxwra ston epomeno xarakthra
if state in accept_states:
lasttoken=accept_states[state] #h teleutaia 8esh pou dexetai
lastpos=pos # h 8esh pou brisketai twra
else: # kamia metabash den bre8hke
if lasttoken is None: # an den uparxei kati pou na exoume dextei tote bgazei la8os
return 'ERROR',pos
else:
return lasttoken,lastpos
# o pinakas metabashs san le3iko
td = { 'q0':{ '0,1,2':'q1', '3,4,5,6,7,8,9':'q3'},
'q3':{ ':,.':'q4'},
'q1':{ '0,1,2,3':'q2'},
'q2':{ ':,.':'q4'},
'q4':{ '0,1,2,3,4,5':'q5'},
'q5':{ '0,1,2,3,4,5,6,7,8,9':'q6'},
'q6':{ ':,.':'q7'},
'q7':{ '0,1,2,3,4,5':'q8'},
'q8':{ '0,1,2,3,4,5,6,7,8,9':'q9'}
}
# le3iko apodoxhs katastasewn kai twn antistoixwn endei3ewn tous
ad = { 'q6':'INT_TOKEN',
'q9':'INT_TOKEN'
}
# get a string from input
words = input('dwse thn wra:>')
# eleg3e to keimeno mexri na mhn uparxei allo input
while len(words)>0:
# pare thn epomenh endei3h kai th 8esh efoson o teleutaios xarakthras anagnwristei
tok,pos = scan(words,td,ad)
if tok=='ERROR':
print('unrecognized input at pos',pos,'of',words)
break
print("token:",tok,"ERROR:",words[:pos])
# kainourgio keimeno gia neo elegxo
words = words[pos:]