-
Notifications
You must be signed in to change notification settings - Fork 0
/
lexems.py
executable file
·95 lines (68 loc) · 1.74 KB
/
lexems.py
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
import ply.lex as lex
reserved_words = (
'brack', # while
'bourbie', # print
'scargil', # if state
'fondeboue', # for state
'slark', # égale =
'slarky', # double égale ==
'jinyu', # pas égale !=
'littleslark', # plus Petit ou égale <=
'bigslark', # plus Grand ou égale >=
'bigy', # plus Grand
'litty', # plus Petit
'gelihast', # switch
'sir', # break du switch
'glougloug', # case du switch
'default', # default du switch
)
tokens = (
'NUMBER',
'ADD_OP',
'MUL_OP',
'IDENTIFIER',
'TYPE_DEF',
'STRING',
) + tuple(map(lambda s: s.upper(), reserved_words))
literals = '()<>;:{}'
def t_STRING(t):
r'".+"'
return t
def t_TYPE_DEF(t):
r'brglmurgl|ahlurglgr|lurgglbr|mourbile'
return t
def t_ADD_OP(t):
r'[+-]'
return t
def t_MUL_OP(t):
r'[*/]'
return t
def t_NUMBER(t):
r'\d+(\.\d+)?'
try:
t.value = float(t.value)
except ValueError:
print("Line %d: Problem while parsing %s!" % (t.lineno, t.value))
t.value = 0
return t
def t_IDENTIFIER(t):
r'[A-Za-z_]\w*'
if t.value in reserved_words:
t.type = t.value.upper()
return t
def t_newline(t):
r'\n+'
t.lexer.lineno += len(t.value)
t_ignore = ' \t'
def t_error(t):
print("Illegal character '%s'" % repr(t.value[0]))
t.lexer.skip(1)
lex.lex()
if __name__ == "__main__":
import sys
prog = open(sys.argv[1]).read()
lex.input(prog)
while 1:
tok = lex.token()
if not tok: break
print("line %d: %s(%s)" % (tok.lineno, tok.type, tok.value))