forked from sanya28wd/Python-Compiler-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlexer.py
More file actions
279 lines (226 loc) · 9.81 KB
/
lexer.py
File metadata and controls
279 lines (226 loc) · 9.81 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
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
"""
Lexical Analyzer (Lexer) for the custom compiler
Performs lexical analysis on source code and generates tokens
"""
from tokens import Token, TokenType
class LexicalError(Exception):
# Exception for lexical errors
pass
class Lexer:
# Lexical Analyzer - converts source code into tokens
def __init__(self, source_code):
"""
Initialize the lexer with source code.
Args:
source_code: The source program as a string
"""
self.source = source_code
self.position = 0
self.line = 1
self.column = 1
self.tokens = []
self.errors = []
# Keywords dictionary
self.keywords = {
'int': TokenType.KEYWORD_INT,
'float': TokenType.KEYWORD_FLOAT,
'if': TokenType.KEYWORD_IF,
'else': TokenType.KEYWORD_ELSE,
'while': TokenType.KEYWORD_WHILE,
'print': TokenType.KEYWORD_PRINT,
}
def current_char(self):
# Get current character
if self.position >= len(self.source):
return None
return self.source[self.position]
def peek_char(self, offset=1):
# Peak at character ahed to use for two characters
pos = self.position + offset
if pos >= len(self.source):
return None
return self.source[pos]
def advance(self):
# Move to next character
if self.position < len(self.source):
if self.source[self.position] == '\n':
self.line += 1
self.column = 1
else:
self.column += 1
self.position += 1
def skip_whitespace(self):
# Skip whitespace characters (except newlines)
while self.current_char() and self.current_char() in ' \t\r':
self.advance() #to not skip newline
def skip_newline(self):
# Skip newline characters
while self.current_char() and self.current_char() == '\n':
self.advance()
def read_number(self):
# Read a number integer or float
start_line = self.line
start_column = self.column
num_str = ''
is_float = False
while self.current_char() and (self.current_char().isdigit() or self.current_char() == '.'):
if self.current_char() == '.':
if is_float:
# Multiple decimal points
self.errors.append(f"Lexical Error at Line {self.line}, Col {self.column}: Multiple decimal points in number")
break
is_float = True
num_str += self.current_char()
self.advance()
if is_float:
return Token(TokenType.FLOAT, float(num_str), start_line, start_column)
else:
return Token(TokenType.INTEGER, int(num_str), start_line, start_column)
def read_identifier(self):
# Read identifier or keyword
start_line = self.line
start_column = self.column
ident = ''
while self.current_char() and (self.current_char().isalnum() or self.current_char() == '_'):
ident += self.current_char()
self.advance()
# Check if it's a keyword
token_type = self.keywords.get(ident, TokenType.IDENTIFIER)
return Token(token_type, ident, start_line, start_column)
def tokenize(self):
# Tokenize entire source code
while self.position < len(self.source):
self.skip_whitespace()
if self.position >= len(self.source):
break
current = self.current_char()
line = self.line
column = self.column
# Newline
if current == '\n':
self.advance()
continue
# Numbers
elif current.isdigit():
self.tokens.append(self.read_number())
# Identifiers and Keywords
elif current.isalpha() or current == '_':
self.tokens.append(self.read_identifier())
# Operators and Delimiters
elif current == '+':
self.tokens.append(Token(TokenType.PLUS, '+', line, column))
self.advance()
elif current == '-':
self.tokens.append(Token(TokenType.MINUS, '-', line, column))
self.advance()
elif current == '*':
self.tokens.append(Token(TokenType.MULTIPLY, '*', line, column))
self.advance()
elif current == '/':
self.tokens.append(Token(TokenType.DIVIDE, '/', line, column))
self.advance()
elif current == '%':
self.tokens.append(Token(TokenType.MODULO, '%', line, column))
self.advance()
elif current == '=':
if self.peek_char() == '=':
self.tokens.append(Token(TokenType.EQ, '==', line, column))
self.advance()
self.advance()
else:
self.tokens.append(Token(TokenType.ASSIGN, '=', line, column))
self.advance()
elif current == '<':
if self.peek_char() == '=':
self.tokens.append(Token(TokenType.LE, '<=', line, column))
self.advance()
self.advance()
else:
self.tokens.append(Token(TokenType.LT, '<', line, column))
self.advance()
elif current == '>':
if self.peek_char() == '=':
self.tokens.append(Token(TokenType.GE, '>=', line, column))
self.advance()
self.advance()
else:
self.tokens.append(Token(TokenType.GT, '>', line, column))
self.advance()
elif current == '!':
if self.peek_char() == '=':
self.tokens.append(Token(TokenType.NE, '!=', line, column))
self.advance()
self.advance()
else:
self.tokens.append(Token(TokenType.NOT, '!', line, column))
self.advance()
elif current == '&':
if self.peek_char() == '&':
self.tokens.append(Token(TokenType.AND, '&&', line, column))
self.advance()
self.advance()
else:
self.errors.append(f"Lexical Error at Line {line}, Col {column}: Unexpected character '&'")
self.advance()
elif current == '|':
if self.peek_char() == '|':
self.tokens.append(Token(TokenType.OR, '||', line, column))
self.advance()
self.advance()
else:
self.errors.append(f"Lexical Error at Line {line}, Col {column}: Unexpected character '|'")
self.advance()
elif current == '(':
self.tokens.append(Token(TokenType.LPAREN, '(', line, column))
self.advance()
elif current == ')':
self.tokens.append(Token(TokenType.RPAREN, ')', line, column))
self.advance()
elif current == '{':
self.tokens.append(Token(TokenType.LBRACE, '{', line, column))
self.advance()
elif current == '}':
self.tokens.append(Token(TokenType.RBRACE, '}', line, column))
self.advance()
elif current == ';':
self.tokens.append(Token(TokenType.SEMICOLON, ';', line, column))
self.advance()
elif current == ',':
self.tokens.append(Token(TokenType.COMMA, ',', line, column))
self.advance()
else:
self.errors.append(f"Lexical Error at Line {line}, Col {column}: Unexpected character '{current}'")
self.advance()
# Add EOF token
self.tokens.append(Token(TokenType.EOF, '', self.line, self.column))
return self.tokens
def print_tokens(self):
# Print all tokens
print("\n" + "="*80)
print("LEXICAL ANALYSIS - TOKEN STREAM")
print("="*80)
if not self.tokens:
print("No tokens generated")
return
print(f"{'Token Type':<20} {'Value':<20} {'Line':<8} {'Column':<8}")
print("-"*80)
for token in self.tokens:
if token.type == TokenType.EOF:
print(f"{'EOF':<20} {'<EOF>':<20} {token.line:<8} {token.column:<8}")
else:
print(f"{token.type:<20} {str(token.value):<20} {token.line:<8} {token.column:<8}")
print("="*80)
def print_errors(self):
# Print all lexical errors
if not self.errors:
print("\nNo lexical errors found")
return
print("\n" + "="*80)
print("LEXICAL ERRORS")
print("="*80)
for error in self.errors:
print(f" {error}")
print("="*80)
def has_errors(self):
# Check if any errors
return len(self.errors) > 0