Path: blob/main/third_party/ply/test/calclex.py
6162 views
# -----------------------------------------------------------------------------1# calclex.py2# -----------------------------------------------------------------------------3import sys45if ".." not in sys.path: sys.path.insert(0,"..")6import ply.lex as lex78tokens = (9'NAME','NUMBER',10'PLUS','MINUS','TIMES','DIVIDE','EQUALS',11'LPAREN','RPAREN',12)1314# Tokens1516t_PLUS = r'\+'17t_MINUS = r'-'18t_TIMES = r'\*'19t_DIVIDE = r'/'20t_EQUALS = r'='21t_LPAREN = r'\('22t_RPAREN = r'\)'23t_NAME = r'[a-zA-Z_][a-zA-Z0-9_]*'2425def t_NUMBER(t):26r'\d+'27try:28t.value = int(t.value)29except ValueError:30print("Integer value too large %s" % t.value)31t.value = 032return t3334t_ignore = " \t"3536def t_newline(t):37r'\n+'38t.lineno += t.value.count("\n")3940def t_error(t):41print("Illegal character '%s'" % t.value[0])42t.lexer.skip(1)4344# Build the lexer45lex.lex()464748495051