-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLexical_Analyzer.py
More file actions
52 lines (44 loc) · 1.63 KB
/
Lexical_Analyzer.py
File metadata and controls
52 lines (44 loc) · 1.63 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
import re
#===============================
# 1.Lexical Analysis
#===============================
class Token:
def __init__(self, type_, value):
self.type = type_
self.value = value
def __repr__(self):
return f"Token({self.type}, '{self.value}')"
class LexicalAnalyzer:
def __init__(self, text):
self.text = text
self.pos = 0
self.tokens = []
def tokenize(self):
token_specs = [
('IF', r'\bif\b'), # 'if' keyword
('ELSE', r'\belse\b'), # 'else' keyword
('NUMBER', r'\d+'), # Integer number
('ID', r'[a-zA-Z_]\w*'), # Identifiers
('OP', r'==|!=|<=|>=|<|>'), # Comparison operators
('ASSIGN', r'='), # Assignment operator
('SEMI', r';'), # Semicolon
('LPAREN', r'\('), # (
('RPAREN', r'\)'), # )
('LBRACE', r'\{'), # {
('RBRACE', r'\}'), # }
('SKIP', r'[ \t\n]+'), # Skip whitespace
('MISMATCH', r'.'), # Any other character
]
tok_regex = '|'.join('(?P<%s>%s)' % pair for pair in token_specs)
for mo in re.finditer(tok_regex, self.text):
kind = mo.lastgroup
value = mo.group()
if kind == 'SKIP':
continue
elif kind == 'MISMATCH':
raise RuntimeError(f"Lexical Error: Unexpected character {value!r}")
token = Token(kind, value)
self.tokens.append(token)
print(f"[Found Token] Type: {kind:<10} Value: {value}")
self.tokens.append(Token('EOF', None))
return self.tokens