1 # ----------------------------------------------------------------------------- 2 # lex_opt_alias.py 3 # 4 # Tests ability to match up functions with states, aliases, and 5 # lexing tables. 6 # ----------------------------------------------------------------------------- 7 8 import sys 9 if ".." not in sys.path: sys.path.insert(0,"..") 10 11 tokens = ( 12 'NAME','NUMBER', 13 ) 14 15 states = (('instdef','inclusive'),('spam','exclusive')) 16 17 literals = ['=','+','-','*','/', '(',')'] 18 19 # Tokens 20 21 def t_instdef_spam_BITS(t): 22 r'[01-]+' 23 return t 24 25 t_NAME = r'[a-zA-Z_][a-zA-Z0-9_]*' 26 27 def NUMBER(t): 28 r'\d+' 29 try: 30 t.value = int(t.value) 31 except ValueError: 32 print("Integer value too large %s" % t.value) 33 t.value = 0 34 return t 35 36 t_ANY_NUMBER = NUMBER 37 38 t_ignore = " \t" 39 t_spam_ignore = t_ignore 40 41 def t_newline(t): 42 r'\n+' 43 t.lexer.lineno += t.value.count("\n") 44 45 def t_error(t): 46 print("Illegal character '%s'" % t.value[0]) 47 t.lexer.skip(1) 48 49 t_spam_error = t_error 50 51 # Build the lexer 52 import ply.lex as lex 53 lex.lex(optimize=1,lextab="aliastab") 54 lex.runmain(data="3+4") 55