-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparser.py
More file actions
351 lines (275 loc) · 11.7 KB
/
parser.py
File metadata and controls
351 lines (275 loc) · 11.7 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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
from errors import ParserError
import lexer as lex
import abstract_syntax_tree as AST
class Parser:
def __init__(self, tokens):
self.tokens = tokens
self.i = 0 # i serves as an index
pass
# utility methods
# returns the current token currently being pointed to by the parser using (self.i)
def _current(self):
return self.tokens[self.i]
# Returns True if the current token is EOF
def _at_end(self):
return self._current().kind is lex.TokenKind.EOF
# lookahead
def _check(self, kind, text=None):
if self._at_end():
return False
t = self._current()
if t.kind is not kind:
return False
if text is not None and t.lexeme != text:
return False
return True
# consume
def _match(self, kind, text=None):
if self._check(kind, text):
self.i += 1
return True
return False
# consume or throw ParserError eith line/col
def _expect(self, kind, text=None, msg=""):
if not self._check(kind, text):
t = self._current()
expect = kind.name + (f" {text!r}" if text else "")
got = f"{t.kind.name} {t.lexeme!r}"
raise ParserError(f"{msg}: expected {expect}, got {got}", t.line, t.col)
tok = self._current()
self.i += 1
return tok
# one token lookahead to detect assignment
def _peek_is_equals(self) -> bool:
j = self.i + 1
if j >= len(self.tokens):
return False
t = self.tokens[j]
return t.kind is lex.TokenKind.OP and t.lexeme == "="
# entry
def parse(self) -> AST.Program:
"""
Program -> FunctionDecLList
"""
functions = []
# require at least one function
if self._at_end():
t = self._current()
raise ParserError("expected a function, found enf od file", t.line, t.col)
while not self._at_end():
functions.append(self._function())
return AST.Program(functions)
# functions & blocks
def _function(self) -> AST.Function:
"""
Function -> Type ID () Block
"""
# need the position of the keyword 'int'
start_tok = self._current()
self._expect(lex.TokenKind.KEYWORD, "int", msg= "function must start with 'int'")
name_tok = self._expect(lex.TokenKind.IDENT, msg= "expected function name")
self._expect(lex.TokenKind.PUNCT, "(",msg= "expected '(' after function name ")
self._expect(lex.TokenKind.PUNCT, ")",msg= "expected ')' after function name")
body = self._block() #takes the closing '}' inside
end_tok = self.tokens[self.i - 1]
return AST.Function(name_tok.lexeme,
body = body,
start_line = start_tok.line,
start_col=start_tok.col,
end_line=end_tok.line,
end_col=end_tok.col)
def _block_empty_only(self) -> AST.Block:
"""
Block -> '{' '}'
Starting with empty blocks
"""
self._expect(lex.TokenKind.PUNCT, "{",msg= "expected '{' to start block")
self._expect(lex.TokenKind.PUNCT, "}",msg= "expected '}' to start block")
return AST.Block(items=[])
def _block(self) -> AST.Block:
"""
Block → "{" Item* "}"
Item → Declaration | Statement
"""
"""
inside a block, we can expect one or more items so add it to the list of items
while it's not the end with } or at the end keep appending if it's declaration or a statement
"""
self._expect(lex.TokenKind.PUNCT, "{",msg= "expected '{' to start block")
items: list[AST.VarDecl | AST.Stmt] = []
while not self._check(lex.TokenKind.PUNCT, "}") and not self._at_end():
if self._check(lex.TokenKind.KEYWORD, "int"):
items.append(self._declaration())
else:
items.append(self._statement())
self._expect(lex.TokenKind.PUNCT, "}",msg= "expected '}' to start block")
return AST.Block(items)
def _declaration(self) -> AST.VarDecl:
"""
Declaration → "int" id { "," id } ";"
"""
"""
expect the keyword in the declaration, then expec the name of the decl, append that token to the list of
names now start the while loop for multiple decl that would be separated by a comma
expect to end with the ; declaration
"""
self._expect(lex.TokenKind.KEYWORD, "int", msg= "declaration must start with 'int'")
names: list[str] = []
poss: list[tuple[int,int]] = []
first = self._expect(lex.TokenKind.IDENT, msg= "expected a variable name")
names.append(first.lexeme)
poss.append((first.line, first.col))
while self._match(lex.TokenKind.PUNCT, ","):
ident = self._expect(lex.TokenKind.IDENT, msg= "expected variable name after ','")
names.append(ident.lexeme)
poss.append((first.line, first.col))
self._expect(lex.TokenKind.PUNCT, ";",msg= "expected ';' after declaraation")
return AST.VarDecl(names, poss)
# statements
def _statement(self) -> AST.Stmt:
if self._check(lex.TokenKind.KEYWORD, "return"):
return self._return_stmt()
if self._check(lex.TokenKind.KEYWORD, "if"):
return self._if_stmt()
if self._check(lex.TokenKind.KEYWORD, "while"):
return self._while_stmt()
if self._check(lex.TokenKind.PUNCT, "{"):
return self._block()
# default: expression statement
return self._expr_stmt()
def _return_stmt(self) -> AST.Return:
self._expect(lex.TokenKind.KEYWORD, "return")
expr = self._expression()
self._expect(lex.TokenKind.PUNCT, ";", msg= "expected ';' after return statement")
return AST.Return(expr)
def _if_stmt(self) -> AST.If:
# IfStmt → "if" "(" Expression ")" Block [ "else" Block ]
self._expect(lex.TokenKind.KEYWORD, "if")
self._expect(lex.TokenKind.PUNCT, "(", "expected '(' after the if statement")
cond = self._expression()
self._expect(lex.TokenKind.PUNCT, ")", msg="expected ')' after condition")
then_blk = self._block() # blocks are required
else_blk = None
if self._match(lex.TokenKind.KEYWORD, "else"):
else_blk = self._block()
return AST.If(cond, then_blk, else_blk)
def _while_stmt(self) -> AST.While:
# "while" "(" Expression ")" Block
self._expect(lex.TokenKind.KEYWORD, "while")
self._expect(lex.TokenKind.PUNCT, "(", "expected '(' after while")
cond = self._expression()
self._expect(lex.TokenKind.PUNCT, ")", "expected ')' after condition")
body = self._block() # need blocks in while
return AST.While(cond, body)
def _expr_stmt(self) -> AST.ExprStmt:
# Expression ";"
expr = self._expression()
self._expect(lex.TokenKind.PUNCT, ";", msg="expected ';' after expression")
return AST.ExprStmt(expr)
# expressions
# assignment
def _expression(self) -> AST.Expr:
return self._assignment()
#return self._additive()
def _assignment(self) -> AST.Expr:
# Assignment -> id "=" Assignment | LogicalOr
if self._check(lex.TokenKind.IDENT) and self._peek_is_equals():
name_tok = self._expect(lex.TokenKind.IDENT)
self._expect(lex.TokenKind.OP, "=", "expected '=' in assignment")
value = self._assignment() # This should make it right-associative recursion
return AST.Assign(name_tok.lexeme, value)
return self._logical_or()
def _logical_or(self) -> AST.Expr:
# LogicalOr → LogicalAnd { "||" LogicalAnd }
node = self._logical_and()
while self._match(lex.TokenKind.OP, "||"):
rhs = self._logical_and()
node = AST.Binary("||", node, rhs)
return node
def _logical_and(self) -> AST.Expr:
# LogicalAnd → Equality { "&&" Equality }
node = self._equality()
while self._match(lex.TokenKind.OP, "&&"):
rhs = self._equality()
node = AST.Binary("&&", node, rhs)
return node
def _equality(self) -> AST.Expr:
# Equality → Relational { ("==" | "!=") Relational }
node = self._relational()
while True:
if self._match(lex.TokenKind.OP, "=="):
rhs = self._relational()
node = AST.Binary("==", node, rhs)
elif self._match(lex.TokenKind.OP, "!="):
rhs = self._relational()
node = AST.Binary("!=", node, rhs)
else:
break
return node
def _relational(self) -> AST.Expr:
# Relational → Additive { ("<" | "<=" | ">" | ">=") Additive }
node = self._additive()
while True:
if self._match(lex.TokenKind.OP, "<"):
rhs = self._additive()
node = AST.Binary("<", node, rhs)
elif self._match(lex.TokenKind.OP, "<="):
rhs = self._additive()
node = AST.Binary("<=", node, rhs)
elif self._match(lex.TokenKind.OP, ">"):
rhs = self._additive()
node = AST.Binary(">", node, rhs)
elif self._match(lex.TokenKind.OP, ">="):
rhs = self._additive()
node = AST.Binary(">=", node, rhs)
else:
break
return node
def _additive(self) -> AST.Expr:
node = self._multiplicative()
while True:
if self._match(lex.TokenKind.OP, "+"):
rhs = self._multiplicative()
node = AST.Binary("+", node, rhs)
elif self._match(lex.TokenKind.OP, "-"):
rhs = self._multiplicative()
node = AST.Binary("-", node, rhs)
else:
break
return node
def _multiplicative(self) -> AST.Expr:
node = self._unary()
while True:
if self._match(lex.TokenKind.OP, "*"):
rhs = self._unary()
node = AST.Binary("*", node, rhs)
elif self._match(lex.TokenKind.OP, "/"):
rhs = self._unary()
node = AST.Binary("/", node, rhs)
elif self._match(lex.TokenKind.OP, "%"):
rhs = self._unary()
node = AST.Binary("%", node, rhs)
else:
break
return node
def _unary(self) -> AST.Expr:
if self._match(lex.TokenKind.OP, "!"):
return AST.Unary("!", self._unary())
if self._match(lex.TokenKind.OP, "-"):
return AST.Unary("-", self._unary())
if self._match(lex.TokenKind.OP, "+"):
return AST.Unary("+", self._unary())
return self._primary()
def _primary(self) -> AST.Expr:
if self._match(lex.TokenKind.PUNCT, "("):
expr = self._expression()
self._expect(lex.TokenKind.PUNCT, ")", msg="expected ')'")
return expr
if self._check(lex.TokenKind.INT):
tok = self._expect(lex.TokenKind.INT)
return AST.IntLit(int(tok.lexeme))
if self._check(lex.TokenKind.IDENT):
tok = self._expect(lex.TokenKind.IDENT)
return AST.Var(tok.lexeme)
t = self._current()
raise ParserError(f"expected expression, got {t.kind.name} {t.lexeme!r}", t.line, t.col)