1# ----------------------------------------------------------------------------- 2# lex_module_import.py 3# 4# A lexer defined in a module, but built in lex_module.py 5# ----------------------------------------------------------------------------- 6 7tokens = ( 8 'NAME','NUMBER', 9 'PLUS','MINUS','TIMES','DIVIDE','EQUALS', 10 'LPAREN','RPAREN', 11 ) 12 13# Tokens 14 15t_PLUS = r'\+' 16t_MINUS = r'-' 17t_TIMES = r'\*' 18t_DIVIDE = r'/' 19t_EQUALS = r'=' 20t_LPAREN = r'\(' 21t_RPAREN = r'\)' 22t_NAME = r'[a-zA-Z_][a-zA-Z0-9_]*' 23 24def t_NUMBER(t): 25 r'\d+' 26 try: 27 t.value = int(t.value) 28 except ValueError: 29 print("Integer value too large %s" % t.value) 30 t.value = 0 31 return t 32 33t_ignore = " \t" 34 35def t_newline(t): 36 r'\n+' 37 t.lineno += t.value.count("\n") 38 39def t_error(t): 40 print("Illegal character '%s'" % t.value[0]) 41 t.lexer.skip(1) 42 43