HSG |
|
Der Scanner zerlegt eine Eingabe in ihre Token.
a = 23 b = 5 q = 0 r = a while r>=b: r = r-b q = q+1 #end print(q)
Der Minipython-Scanner
import ply.lex as lex reserved = {'while' : 'WHILE', 'if' : 'IF', 'else' : 'ELSE', 'pass' : 'PASS'} tokens = ['VAR','NUM','END','OP'] + list(reserved.values()) literals = "=:+-" def t_IGN(t): r'print\(.*' # kein return, damit die Zeile vom Parser nicht gesehen wird def t_VAR(t): r'[a-zA-Z_][a-zA-Z_0-9]*' t.type = reserved.get(t.value,'VAR') return t def t_NUM(t): r'\d+' t.value = int(t.value) return t def t_END(t): r'\#end' return t def t_OP(t): r'=?|==|!=' return t def t_newline(t): r'\n+' t.lexer.lineno = t.lexer.lineno + len(t.value) t_ignore = ' \t' def t_error(t): print('Unerwartetes Zeichen:',t.value[0],',Zeile:',s.lineno) t.lexer.skip(1) s = lex.lex()
Test
>>> s.input(""" a = 23 b = 5 q = 0 r = a while r>=b: r = r-b q = q+1 #end print(q) print(r)""") >>> while True: t = s.token() print(t) if t == None: break LexToken(VAR,'a',2,1) LexToken(=,'=',2,3) LexToken(NUM,23,2,5) LexToken(VAR,'b',3,8) LexToken(=,'=',3,10) LexToken(NUM,5,3,12) LexToken(VAR,'q',5,15) LexToken(=,'=',5,17) LexToken(NUM,0,5,19) LexToken(VAR,'r',6,21) LexToken(=,'=',6,23) LexToken(VAR,'a',6,25) LexToken(WHILE,'while',7,27) LexToken(VAR,'r',7,33) LexToken(OP,'>=',7,34) LexToken(VAR,'b',7,36) LexToken(:,':',7,37) LexToken(VAR,'r',8,43) LexToken(=,'=',8,45) LexToken(VAR,'r',8,47) LexToken(-,'-',8,48) LexToken(VAR,'b',8,49) LexToken(VAR,'q',9,55) LexToken(=,'=',9,57) LexToken(VAR,'q',9,59) LexToken(+,'+',9,60) LexToken(NUM,1,9,61) LexToken(END,'#end',10,63) None >>>
Teste weitere Eingaben, z.B.
a=17b=345if b=7 #end while :
oder
JamesBond 007
oder
42;otto
oder
...