-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfpbench_lexer.py
More file actions
97 lines (81 loc) · 2.37 KB
/
fpbench_lexer.py
File metadata and controls
97 lines (81 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from ply import lex
import re
tokens = (
"LPAREN",
"RPAREN",
"LBRACK",
"RBRACK",
"IF",
"LET",
"WHILE",
"NUMBER",
"CONSTANT",
"SYMBOL",
"STRING",
"OPERATION"
)
ops = ["+","-","*","/","fabs","fma","exp","exp2","expm1","log","log10","log2",
"log1p","pow","sqrt","cbrt","hypot","sin","cos","tan","asin","acos",
"atan","atan2","sinh","cosh","tanh","asinh","acosh","atanh","erf","erfc",
"tgamma","lgamma","ceil","floor","fmod","remainder","fmax","fmin","fdim",
"copysign","trunc","round","nearbyint","=>","<=",">=","==","!=","<",">",
"and","or","not","isfinite","isinf","isnan","isnormal","signbit"]
consts = ["E","LOG2E","LOG10E","LN2","LN10","PI","PI_2","PI_4","1_PI","2_PI",
"2_SQRTPI","SQRT2","SQRT1_2","INFINITY","NAN","TRUE","FALSE"]
keywords = {
'if' : 'IF',
'let' : 'LET',
'while' : 'WHILE'
}
__num_re = re.compile(r'(?:[-+]?[0-9]+(((\.)?|(\.[0-9]+)?)(e[-+]?[0-9]+)?)?\Z)')
__nums = {str(i) for i in range(10)}
def t_COMMENT(t):
r';.*\n'
t.lexer.lineno += 1
pass
def t_SYMBOL(t):
r'[a-zA-Z0-9~!@$%^&*_\-\+=<>\.\?/:][a-zA-Z0-9~!@$%^&*_\-+=<>\.\?/:]*'
if t.value in ops:
t.type = "OPERATION"
elif t.value in consts:
t.type = "CONSTANT"
elif t.value in keywords.keys():
t.type = keywords[t.value]
# Actually a number
elif __num_re.match(t.value):
try:
float(t.value)
except:
print("Floating point expression is wrong")
assert(0)
t.type = "NUMBER"
# Tentatively a symbol
else:
if str(t.value)[0] in __nums:
print("Symbol starts with number:", t.value)
assert(0)
return t
def t_newline(t):
r'\n+'
t.lexer.lineno += len(t.value)
t_ignore = ' \t\r\f\v'
t_LPAREN = r'\('
t_RPAREN = r'\)'
t_LBRACK = r'\['
t_RBRACK = r'\]'
t_STRING = r'"([\x20-\x5b\x5d-\x7e]|\\["\\n])+?"'
# Error handling rule
def t_error(t):
print("Illegal character '%s'" % t.value[0])
t.lexer.skip(1)
lexer = lex.lex()
# Test cases
if __name__ == '__main__':
lex.lex()
lex.input('(FPCore (t)\n' +
' :name "intro-example"' +
' :cite (solovyev-et-al-2015)' +
' :pre (<= 0 t 999)'+
' (/ t (+ t 1)))')
for tok in iter(lex.token, None):
print(repr(tok.type), repr(tok.value))