-
Notifications
You must be signed in to change notification settings - Fork 2
/
rhs_lexer.py
48 lines (39 loc) · 977 Bytes
/
rhs_lexer.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
from sly import Lexer
class BasicLexer(Lexer):
tokens = { NAME, NUMBER, STRING, IF, PRINT, THEN, ELSE, FOR, FUN, TO, ARROW, EQEQ }
ignore = '\t '
literals = { '=', '+', '-', '/', '*', '(', ')', ',', ';' }
# Define tokens
IF = r'FI'
PRINT = r'TNIRP'
THEN = r'NEHT'
ELSE = r'ESLE'
FOR = r'ROF'
FUN = r'NUF'
TO = r'OT'
ARROW = r'->'
NAME = r'[a-zA-Z_][a-zA-Z0-9_]*'
STRING = r'\".*?\"'
EQEQ = r'=='
@_(r'\d+')
def NUMBER(self, t):
t.value = int(t.value)
return t
@_(r'#.*')
def COMMENT(self, t):
pass
@_(r'\n+')
def newline(self,t ):
self.lineno = t.value.count('\n')
if __name__ == '__main__':
lexer = BasicLexer()
env = {}
while True:
try:
text = input('rhs > ')
except EOFError:
break
if text:
lex = lexer.tokenize(text)
for token in lex:
print(token)