calclex.py (4479:61d3ed46e373) | calclex.py (6498:e21e9ab5fad0) |
---|---|
1# ----------------------------------------------------------------------------- 2# calclex.py 3# ----------------------------------------------------------------------------- 4import sys 5 | 1# ----------------------------------------------------------------------------- 2# calclex.py 3# ----------------------------------------------------------------------------- 4import sys 5 |
6sys.path.append("..") | 6if ".." not in sys.path: sys.path.insert(0,"..") |
7import ply.lex as lex 8 9tokens = ( 10 'NAME','NUMBER', 11 'PLUS','MINUS','TIMES','DIVIDE','EQUALS', 12 'LPAREN','RPAREN', 13 ) 14 --- 8 unchanged lines hidden (view full) --- 23t_RPAREN = r'\)' 24t_NAME = r'[a-zA-Z_][a-zA-Z0-9_]*' 25 26def t_NUMBER(t): 27 r'\d+' 28 try: 29 t.value = int(t.value) 30 except ValueError: | 7import ply.lex as lex 8 9tokens = ( 10 'NAME','NUMBER', 11 'PLUS','MINUS','TIMES','DIVIDE','EQUALS', 12 'LPAREN','RPAREN', 13 ) 14 --- 8 unchanged lines hidden (view full) --- 23t_RPAREN = r'\)' 24t_NAME = r'[a-zA-Z_][a-zA-Z0-9_]*' 25 26def t_NUMBER(t): 27 r'\d+' 28 try: 29 t.value = int(t.value) 30 except ValueError: |
31 print "Integer value too large", t.value | 31 print("Integer value too large %s" % t.value) |
32 t.value = 0 33 return t 34 35t_ignore = " \t" 36 37def t_newline(t): 38 r'\n+' 39 t.lineno += t.value.count("\n") | 32 t.value = 0 33 return t 34 35t_ignore = " \t" 36 37def t_newline(t): 38 r'\n+' 39 t.lineno += t.value.count("\n") |
40 | 40 |
41def t_error(t): | 41def t_error(t): |
42 print "Illegal character '%s'" % t.value[0] | 42 print("Illegal character '%s'" % t.value[0]) |
43 t.lexer.skip(1) | 43 t.lexer.skip(1) |
44 | 44 |
45# Build the lexer 46lex.lex() 47 48 49 | 45# Build the lexer 46lex.lex() 47 48 49 |