Path: blob/main/third_party/ply/test/lex_closure.py
6162 views
# -----------------------------------------------------------------------------1# lex_closure.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)1314def make_calc():1516# Tokens1718t_PLUS = r'\+'19t_MINUS = r'-'20t_TIMES = r'\*'21t_DIVIDE = r'/'22t_EQUALS = r'='23t_LPAREN = r'\('24t_RPAREN = r'\)'25t_NAME = r'[a-zA-Z_][a-zA-Z0-9_]*'2627def t_NUMBER(t):28r'\d+'29try:30t.value = int(t.value)31except ValueError:32print("Integer value too large %s" % t.value)33t.value = 034return t3536t_ignore = " \t"3738def t_newline(t):39r'\n+'40t.lineno += t.value.count("\n")4142def t_error(t):43print("Illegal character '%s'" % t.value[0])44t.lexer.skip(1)4546# Build the lexer47return lex.lex()4849make_calc()50lex.runmain(data="3+4")515253545556