Path: blob/main/third_party/ply/test/lex_optimize3.py
6162 views
# -----------------------------------------------------------------------------1# lex_optimize3.py2#3# Writes table in a subdirectory structure.4# -----------------------------------------------------------------------------5import sys67if ".." not in sys.path: sys.path.insert(0,"..")8import ply.lex as lex910tokens = (11'NAME','NUMBER',12'PLUS','MINUS','TIMES','DIVIDE','EQUALS',13'LPAREN','RPAREN',14)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 lexer47lex.lex(optimize=1,lextab="lexdir.sub.calctab",outputdir="lexdir/sub")48lex.runmain(data="3+4")495051525354