-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexer.py
43 lines (34 loc) · 1.36 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
import re
class LEXER :
TOKENS = [ ("KEYWORD" , r"\b(printf|scanf|int|void|return)\b") ,
("IDENTEFIER" , r"[a-zA-Z_][\w]*\b") ,
("STRING", r'"[^"\n]*"'),
("COMMA", r","),
("CONSTANT" , r"\b[0-9]+\b"),
("OPEN_PARANTHESE" , r"\("),
("CLOSE_PARANTHESE" , r"\)") ,
("OPEN_BRACE" , r"\{") ,
("CLOSE_BRACE" , r"\}") ,
("SEMICOLON" , r";")
]
def __init__(self , source_code):
self.source_code = source_code
self.tokens = []
self.curIdx = 0
def tokenize(self):
while(self.curIdx < len(self.source_code)):
if self.source_code[self.curIdx].isspace():
self.curIdx += 1
continue
match = None
for token_type , regex in self.TOKENS :
pattern = re.compile(regex)
match = pattern.match(self.source_code , self.curIdx)
if match :
lexem = match.group(0)
self.tokens.append((token_type , lexem))
self.curIdx += len(lexem)
break
if not match :
raise SyntaxError(f"Lexem is not defined at index {self.curIdx} , the lexem is {self.source_code[self.curIdx]}")
return self.tokens