-
Notifications
You must be signed in to change notification settings - Fork 0
/
Lexer.py
49 lines (37 loc) · 1.32 KB
/
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
49
# Lexer.py
import ply.lex as lex
import sys
class Lexer:
literals = "[]()*/+-"
t_ignore = " \n\t"
tokens = ("LOGIC", "NUM", "VARNAME", "VARUSE", "FD", "FORWARD", "BK", "BACK", "LT", "LEFT", "RT", "RIGHT", "SETPOS", "SETXY", "SETX",
"SETY", "HOME", "PD", "PENDOWN", "PU", "PENUP", "SETPENCOLOR", "MAKE", "IF", "IFELSE", "REPEAT", "WHILE","TO", "END", "NAMETO")
def t_COMMAND(self, t):
r"""(fd|forward|bk|back|lt|left|rt|right|setpos|setx(y)?|sety|home|pd|pendown|pu|penup|
setpencolor|make|if(else)?|repeat|while|to|end)\b"""
t.type = t.value.upper()
return t
def t_NAMETO(self, t):
r"""[a-zA-Z][A-Za-z0-9]*"""
return t
def t_NUM(self, t):
r"""[0-9]+(\.[0-9]+)?"""
t.value = float(t.value)
return t
def t_VARNAME(self, t):
r"""[\"][A-Za-z][A-Za-z0-9]*"""
return t
def t_VARUSE(self, t):
r"""[\:][A-Za-z][A-Za-z0-9]*"""
return t
def t_LOGIC(self, t):
r"""<(=)?|>(=)?|==|!="""
return t
def t_error(self, t):
print(f"Caractere inesperado: {t.value[0]}", file=sys.stderr)
exit(1)
def __init__(self):
self.lexer = None
def Build(self, content, **kwargs):
self.lexer = lex.lex(module=self, **kwargs)
self.lexer.input(content)