Spaces:
Runtime error
Runtime error
File size: 22,075 Bytes
16f1442 |
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 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 |
from dataclasses import dataclass
import sys
from lexer import *
# import sim
from dataclasses_sim import *
@dataclass
class Parser(object):
def __init__(self, lexer):
self.lexer = lexer
self.curr_token = self.lexer.get_token()
self.lexer.lineNum = 0
def check_type(self, Type):
if self.curr_token.type == Type:
self.curr_token = self.lexer.get_token()
else:
print("Expected Token Type: ", Type)
print("Got Token: ", self.curr_token)
print("At line number:", self.lexer.lineNum)
print(self.lexer.curLine)
print(" "*(self.lexer.curLinePos-1),"^")
sys.exit('Invalid character')
def parse_if(self):
self.check_type(IF)
condition = self.logical()
self.check_type(THEN)
if self.curr_token.type != BEGIN and self.curr_token.type != IF and self.curr_token.type != WHILE and self.curr_token.type != FOR and self.curr_token.type != FUNCTION:
true = self.parse()
self.check_type(SEMI)
else:
true = self.parse()
self.check_type(ELSE)
while self.curr_token.type == IF:
self.check_type(IF)
condition = self.logical()
self.check_type(THEN)
if self.curr_token.type != BEGIN and self.curr_token.type != IF and self.curr_token.type != WHILE and self.curr_token.type != FOR and self.curr_token.type != FUNCTION:
true = self.parse()
self.check_type(SEMI)
else:
true = self.parse()
self.check_type(ELSE)
if self.curr_token.type != END:
if self.curr_token.type != BEGIN and self.curr_token.type != IF and self.curr_token.type != WHILE and self.curr_token.type != FOR and self.curr_token.type != FUNCTION:
false = self.parse()
self.check_type(SEMI)
else:
false = self.parse()
else:
false = None
self.check_type(END)
node = IfElse(condition, true, false)
return node
def parse_begin(self):
"""parse_begin : """
self.check_type(BEGIN)
if self.curr_token.type == END:
self.check_type(END)
return Seq([])
# s = self.parse_list()
ignoreSEMI = False
if self.curr_token.type == IF or self.curr_token.type == WHILE or self.curr_token.type == FOR or self.curr_token.type == FUNCTION or self.curr_token.type == BEGIN:
ignoreSEMI = True
e = self.parse()
token = self.curr_token
Type = token.type
seq = [e]
while Type != END:
if not ignoreSEMI:
self.check_type(SEMI)
ignoreSEMI = False
if self.curr_token.type == END:
break
if self.curr_token.type == IF or self.curr_token.type == WHILE or self.curr_token.type == FOR or self.curr_token.type == FUNCTION or self.curr_token.type == BEGIN:
ignoreSEMI = True
e = self.parse()
seq.append(e)
token = self.curr_token
Type = token.type
# if Type == SEMI:
# self.check_type(SEMI)
self.check_type(END)
return Seq(seq)
# return s
def parse_for(self):
self.check_type(FOR)
start = self.parse()
# print("Hi", start)
self.check_type(SEMI)
condition = self.logical()
# print("Hi2", end)
self.check_type(SEMI)
increment = self.parse()
# print("Hi3",jump)
self.check_type(DO)
if self.curr_token.type != BEGIN and self.curr_token.type != IF and self.curr_token.type != WHILE and self.curr_token.type != FOR and self.curr_token.type != FUNCTION:
body = self.parse()
self.check_type(SEMI)
self.check_type(END)
return ForLoop(start, condition, increment, body)
body = self.parse()
self.check_type(END)
return ForLoop(start, condition, increment, body)
def parse_while(self):
self.check_type(WHILE)
c = self.logical()
self.check_type(DO)
if self.curr_token.type != BEGIN and self.curr_token.type != IF and self.curr_token.type != WHILE and self.curr_token.type != FOR and self.curr_token.type != FUNCTION:
b = self.parse()
self.check_type(SEMI)
self.check_type(END)
return While(c, b)
b = self.parse()
self.check_type(END)
return While(c, b)
def parse_list_append(self):
self.check_type(APPEND)
self.check_type(LPAREN)
var = self.variable()
self.check_type(COMMA)
item = self.parse()
self.check_type(RPAREN)
return list_append(var, item)
def parse_list_slice(self, c):
self.check_type(LSPAREN)
if self.curr_token.type==COMMA:
start = NumLiteral(0)
else:
start = self.parse()
if self.curr_token.type==RSPAREN:
index_type = True
self.check_type(RSPAREN)
elif self.curr_token.type==COMMA:
index_type = False
self.check_type(COMMA)
end = self.parse()
else:
index_type = False
end = self.parse()
if index_type==False:
if self.curr_token.type==COMMA:
self.check_type(COMMA)
if self.curr_token.type!=RSPAREN:
jump = self.parse()
else:
jump = NumLiteral(1)
else:
jump = NumLiteral(1)
self.check_type(RSPAREN)
else:
end = None
jump = None
return list_Slicing(c, start, end, jump)
def parse_inc(self):
self.check_type(INC)
self.check_type(LPAREN)
c = self.parse()
self.check_type(RPAREN)
return Increment(c)
def parse_dec(self):
self.check_type(DEC)
self.check_type(LPAREN)
c = self.parse()
self.check_type(RPAREN)
return Decrement(c)
def parse_len(self):
self.check_type(LPAREN)
c = self.parse()
self.check_type(RPAREN)
return length(c)
def parse_head(self):
self.check_type(LPAREN)
c = self.parse()
self.check_type(RPAREN)
return list_head(c)
def parse_tail(self):
self.check_type(LPAREN)
c = self.parse()
self.check_type(RPAREN)
return list_tail(c)
def parse_isempty(self):
self.check_type(LPAREN)
c = self.parse()
self.check_type(RPAREN)
return list_isempty(c)
def parse_slice(self, c):
self.check_type(LSPAREN)
if self.curr_token.type==COMMA:
# self.check_type(COMMA)
start = NumLiteral(0)
else:
start = self.parse()
if self.curr_token.type==RSPAREN:
index_type = True
self.check_type(RSPAREN)
elif self.curr_token.type==COMMA:
index_type = False
self.check_type(COMMA)
if self.curr_token.type!=COMMA:
end = self.parse()
else:
end = None
else:
index_type = False
end = self.parse()
if index_type==False:
if self.curr_token.type==COMMA:
self.check_type(COMMA)
if self.curr_token.type!=RSPAREN:
jump = self.parse()
else:
jump = NumLiteral(1)
else:
jump = NumLiteral(1)
self.check_type(RSPAREN)
else:
end = None
jump = None
return Slicing(c, start, end, jump)
def parse_print(self):
self.check_type(PRINT)
self.check_type(LPAREN)
if self.curr_token.type != RPAREN:
e = self.logical()
else:
e = None
# print(e)
self.check_type(RPAREN)
# if self.curr_token.type != END:
# e = self.logical()
# else:
# e = None
# # print(e)
# self.check_type(END)
return Statement("print", e)
def parse_return(self):
self.check_type(RETURN)
e = self.logical()
return Statement("return", e)
def parse_list(self, Type):
if Type != LSPAREN:
# print("ENTER")
self.check_type(COLON)
token = self.curr_token
self.check_type(INTEGER)
Type = token.type
datatype = Type
# print(datatype)
else:
datatype = NONE
self.check_type(LSPAREN)
ele = self.parse()
value =[ele]
token = self.curr_token
Type = token.type
while Type!= RSPAREN:
self.check_type(COMMA)
ele = self.parse()
value.append(ele)
token = self.curr_token
# print(token)
Type = token.type
self.check_type(RSPAREN)
# print(type(datatype))
# print("done and dusted")
return Listing(value, datatype)
def parse_func(self):
self.check_type(FUNCTION)
# name = self.variable("Variable")
name = self.variable()
self.check_type(LPAREN)
# var = self.variable("Variable")
var = self.variable()
token = self.curr_token
Type = token.type
params = [var] if var != None else []
while Type != RPAREN:
self.check_type(COMMA)
if self.curr_token.type == RPAREN:
break
# var = self.variable("Variable")
var = self.variable()
params.append(var)
token = self.curr_token
Type = token.type
self.check_type(RPAREN)
if self.curr_token.type == BEGIN:
body = self.parse()
else:
self.check_type(BEGIN)
return Function(name, params, body)
def parse_func_call(self, n):
node = n
self.check_type(LPAREN)
if self.curr_token.type == RPAREN:
self.check_type(RPAREN)
if self.curr_token.type == LPAREN:
return self.parse_func_call(FunCall(node, []))
return FunCall(node, [])
var = self.logical()
token = self.curr_token
Type = token.type
params = [var] if var != None else []
while Type != RPAREN:
self.check_type(COMMA)
if self.curr_token.type == RPAREN:
break
var = self.logical()
params.append(var)
token = self.curr_token
Type = token.type
self.check_type(RPAREN)
if self.curr_token.type == LPAREN:
return self.parse_func_call(FunCall(node, params))
return FunCall(node, params)
def variable(self, ASTtype=None):
token = self.curr_token
Type = token.type
if Type == ID:
self.check_type(ID)
if ASTtype=="Variable":
token = self.curr_token
Type = token.type
#self.parse_slice(Variable(token.value), Type)
return Variable(token.value)
else:
return MutVar(token.value)
def precedence3(self):
'''precedence3 : INTEGER | LPAREN precedence1 RPAREN | BoolLiteral | Indentifier| (+/-)precedence3 | StringLiteral'''
token = self.curr_token
Type = token.type
if Type == PLUS:
self.check_type(PLUS)
node = UnOp(operator=token.value, mid=self.precedence3())
return node
elif Type == MINUS:
self.check_type(MINUS)
node = UnOp(operator=token.value, mid=self.precedence3())
return node
elif Type == LPAREN:
self.check_type(LPAREN)
# node = self.precedence1()
node = self.logical()
self.check_type(RPAREN)
return node
elif Type == FRACTION_CONST:
self.check_type(FRACTION_CONST)
return NumLiteral(token.value)
elif Type == REAL_CONST:
self.check_type(REAL_CONST)
return FloatLiteral(token.value)
elif Type == INTEGER_CONST:
self.check_type(INTEGER_CONST)
return IntLiteral(token.value)
elif Type == TRUE or Type == FALSE:
self.check_type(Type)
return BoolLiteral(token.value)
elif Type == LEN:
self.check_type(LEN)
return self.parse_len()
elif Type == HEAD:
self.check_type(HEAD)
return self.parse_head
elif Type == TAIL:
self.check_type(TAIL)
return self.parse_tail
elif Type == ISEMPTY:
self.check_type(ISEMPTY)
return self.parse_isempty
elif Type == LIST:
self.check_type(LIST)
if self.curr_token.type == COLON:
return self.parse_list(self.curr_token.type)
return self.parse_list(self.curr_token.type)
elif Type == ID:
self.check_type(ID)
if self.curr_token.type == LPAREN:
return self.parse_func_call(MutVar(token.value))
elif self.curr_token.type == LSPAREN:
return self.parse_slice(MutVar(token.value))
return (MutVar(token.value))
elif Type == STRING:
# Nothing new here, just eat the STRING token and return the String() AST.
self.check_type(STRING)
return StringLiteral(token.value)
else:
# if Type == END:
# self.check_type(END)
# return
print("None of the suggested tokens found:", INTEGER_CONST, ID, LPAREN, STRING, TRUE, FALSE, "...")
self.check_type(INTEGER_CONST)
def exponential(self):
"""exponential : precedence3 | precedence3 POWER precedence3"""
node = self.precedence3()
token = self.curr_token
Type = token.type
l = [node]
if Type == POWER:
while (Type == POWER):
token = self.curr_token
if token.type == POWER:
self.check_type(POWER)
e = self.precedence3()
# print(e)
l.append(e)
Type = self.curr_token.type
# e = self.precedence3()
# print(l)
i = 1
while len(l) > 0 :
e = l.pop()
if i==1:
node = BinOp(left=l.pop(), operator=token.value, right=e)
else:
node = BinOp(left=e, operator=token.value, right=node)
i+=1
return node
def precedence2(self):
"""precedence2 : precedence3 | precedence3 MUL/DIV precedence3"""
node = self.exponential()
token = self.curr_token
Type = token.type
while (Type == MUL or Type ==FLOAT_DIV or Type == MODULO or Type == INT_DIV):
token = self.curr_token
if token.type == MUL:
self.check_type(MUL)
elif token.type == FLOAT_DIV:
self.check_type(FLOAT_DIV)
elif token.type == INT_DIV:
self.check_type(INT_DIV)
elif token.type == MODULO:
self.check_type(MODULO)
elif token.type == INT_DIV:
self.check_type(INT_DIV)
node = BinOp(left=node, operator=token.value, right=self.exponential())
Type = self.curr_token.type
return node
def precedence1(self):
"""precedence1 : precedence2 | precedence2 PLUS/MINUS precedence2"""
node = self.precedence2()
Type = self.curr_token.type
while (Type == PLUS or Type ==MINUS):
token = self.curr_token
if token.type == PLUS:
self.check_type(PLUS)
elif token.type == MINUS:
self.check_type(MINUS)
node = BinOp(left=node, operator=token.value, right=self.precedence2())
Type = self.curr_token.type
return node
# def parse(self):
# return self.precedence1()
def relational(self, n=None):
"""relational : precedence1 | precedence1 relationalOperator precedence1"""
if n == None:
node = self.precedence1()
else:
node = n
Type = self.curr_token.type
# print(Type)
# while(Type == GT or Type == GTEQ or Type == LT or Type == LTEQ):
if(Type == GT or Type == GTEQ or Type == LT or Type == LTEQ or Type == EQEQ or Type == NOTEQ):
token = self.curr_token
if token.type == GT:
self.check_type(GT)
elif token.type == GTEQ:
self.check_type(GTEQ)
elif token.type == LT:
self.check_type(LT)
elif token.type == LTEQ:
self.check_type(LTEQ)
elif token.type == EQEQ:
self.check_type(EQEQ)
elif token.type == NOTEQ:
self.check_type(NOTEQ)
node = BinOp(left=node, operator=token.value, right=self.precedence1())
# Type = self.curr_token.type
return node
def logical(self, n=None):
"""logical : relational | relational logicalOperator relational"""
if n == None:
node = self.relational()
else:
node = n
# node = self.relational()
Type = self.curr_token.type
if(Type == OR or Type == AND):
token = self.curr_token
if token.type == AND:
self.check_type(AND)
elif token.type == OR:
self.check_type(OR)
node = BinOp(left=node, operator= token.value, right=self.relational())
return node
def assignment(self, n=None):
"""assignment : variable ASSIGN relational"""
if n == None:
node = self.variable()
else:
node = n
Type = self.curr_token.type
# token = self.curr_token
# print(Type)
# if Type == ASSIGN:
# self.check_type(ASSIGN)
# token = self.curr_token
# node = BinOp(left=node, operator=token.value, right=self.logical())
# else:
# if isinstance(node,MutVar):
# node = self.logical(Get(node))
# else:
# node = self.logical()
if(Type == ASSIGN or Type == PLUSEQ or Type == MINUSEQ or Type == FLOAT_DIVEQ or Type == MULEQ or Type == POWEREQ):
token = self.curr_token
if Type == ASSIGN:
self.check_type(ASSIGN)
node = BinOp(left=node, operator= token.value, right=self.logical())
elif Type == MULEQ:
self.check_type(MULEQ)
# print(self.curr_token)
node = BinOp(left=node, operator= token.value, right=self.logical())
elif Type == MINUSEQ:
self.check_type(MINUSEQ)
# print(self.curr_token)
node = BinOp(left=node, operator= token.value, right=self.logical())
elif Type == FLOAT_DIVEQ:
self.check_type(FLOAT_DIVEQ)
# print(self.curr_token)
node = BinOp(left=node, operator= token.value, right=self.logical())
elif Type == PLUSEQ:
self.check_type(PLUSEQ)
# print(self.curr_token)
node = BinOp(left=node, operator= token.value, right=self.logical())
elif Type == POWEREQ:
self.check_type(POWEREQ)
# print(self.curr_token)
node = BinOp(left=node, operator= token.value, right=self.logical())
else:
if isinstance(node,MutVar):
node = self.logical(node)
else:
node = self.logical()
return node
def parse(self):
"""parse : parse_if | parse_print | parse_begin | assignment"""
match self.curr_token.type:
case 'IF':
return self.parse_if()
case 'WHILE':
return self.parse_while()
case 'FOR':
return self.parse_for()
case 'PRINT':
return self.parse_print()
case 'RETURN':
return self.parse_return()
case 'BEGIN':
return self.parse_begin()
case 'FUNCTION':
return self.parse_func()
case 'BREAK':
self.check_type(BREAK)
return Statement("break",NumLiteral(0))
case 'INC':
return self.parse_inc()
case 'DEC':
return self.parse_dec()
case 'APPEND':
return self.parse_list_append()
# case 'SEMI':
# return
# return self.parse()
case _:
node = self.variable()
if self.curr_token.type == LPAREN:
return self.parse_func_call(node)
# node = MutVar(node.name)
return self.assignment(node)
|