Best Python code snippet using pyatom_python
main.py
Source:main.py
1import sys2from lexical import Lexical3from miscl import Token4import re5class Main():6 _tokens=[];_tokensIndex=07 _temp = "";_lineNum = 1;_codeFile="code.txt";_outputFile="output.txt";_fileData=""8 _breakers = ['(', ')', '[', ']', '{', '}', '=', ',', ' ', '\n', '\r'9 , '<', '>', '-', '+', '*', '/', '%', ':', ';', '.', '!', '&', '|', '#' , '"' , '\'' , '\t']10 _invalidPrint = ['=', ' ', '\n', '\r', '<', '>', '-', '+', '*', '/', '%', '!', '&', '|', '#' , '"' , '\'' , '\t']11 def __init__(self):12 Main._fileData=""13 Main._lineNum=114 Main._tokens=[]15 Main._tokensIndex=016 #region CFG methods17 def PROG(self):18 if Main._tokens[Main._tokensIndex].CP == "class":19 if self.CLASS():20 return True21 if Main._tokens[Main._tokensIndex].CP in ['ID', 'self', 'while', 'for', 'if', 'def']:22 if self.M_ST():23 return True24 def CLASS(self):25 if Main._tokens[Main._tokensIndex].CP == "class":26 Main._tokensIndex += 127 if Main._tokens[Main._tokensIndex].CP == "ID":28 Main._tokensIndex += 129 if self.CLASS1():30 return True31 else:32 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))33 else:34 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP,Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))35 def CLASS1(self):36 if Main._tokens[Main._tokensIndex].CP=='(':37 Main._tokensIndex += 138 if self.PARENT():39 Main._tokensIndex += 140 if Main._tokens[Main._tokensIndex].CP==')':41 Main._tokensIndex += 142 if Main._tokens[Main._tokensIndex].CP == ':':43 Main._tokensIndex += 144 if Main._tokens[Main._tokensIndex].CP == '{':45 Main._tokensIndex += 146 if self.M_ST():47 Main._tokensIndex += 148 if Main._tokens[Main._tokensIndex].CP == '}':49 return True50 else:51 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP,Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))52 else:53 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))54 else:55 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))56 else:57 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))58 else:59 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP,Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))60 def PARENT(self):61 if Main._tokens[Main._tokensIndex].CP == 'ID':62 Main._tokensIndex += 163 if self.PARENT():64 return True65 elif Main._tokens[Main._tokensIndex].CP == ',':66 Main._tokensIndex += 167 if Main._tokens[Main._tokensIndex].CP == 'ID':68 Main._tokensIndex += 169 if self.PARENT():70 return True71 else:72 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))73 else:74 Main._tokensIndex -= 175 return True76 def FUNC_DEF(self):77 if Main._tokens[Main._tokensIndex].CP == 'def':78 Main._tokensIndex += 179 if Main._tokens[Main._tokensIndex].CP == 'ID':80 Main._tokensIndex += 181 if Main._tokens[Main._tokensIndex].CP == '(':82 Main._tokensIndex += 183 if self.ARGS():84 Main._tokensIndex += 185 if Main._tokens[Main._tokensIndex].CP == ')':86 Main._tokensIndex += 187 if Main._tokens[Main._tokensIndex].CP == ':':88 Main._tokensIndex += 189 if Main._tokens[Main._tokensIndex].CP == '{':90 Main._tokensIndex += 191 if self.M_ST():92 Main._tokensIndex += 193 if self.RET():94 Main._tokensIndex += 195 if Main._tokens[Main._tokensIndex].CP == '}':96 return True97 else:98 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP,Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))99 else:100 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP,Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))101 else:102 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP,Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))103 else:104 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))105 else:106 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))107 else:108 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))109 else:110 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP,Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))111 def ARGS(self):112 if Main._tokens[Main._tokensIndex].CP in ['self', 'ID']:113 if self.SELF():114 Main._tokensIndex += 1115 if self.DECL_ASGN():116 return True117 else:118 Main._tokensIndex -= 1119 return True120 def SELF(self):121 if Main._tokens[Main._tokensIndex].CP=='self':122 Main._tokensIndex += 1123 if Main._tokens[Main._tokensIndex].CP == ',':124 return True125 else:126 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))127 else:128 Main._tokensIndex -= 1129 return True130 def RET(self):131 if Main._tokens[Main._tokensIndex].CP=='return':132 Main._tokensIndex += 1133 if self.E():134 return True135 else:136 Main._tokensIndex -= 1137 return True138 def M_ST(self):139 if Main._tokens[Main._tokensIndex].CP in ['ID', 'self', 'while', 'for', 'if', 'def']:140 if self.S_ST():141 Main._tokensIndex += 1142 if self.M_ST():143 return True144 else:145 Main._tokensIndex -= 1146 return True147 def S_ST(self):148 if Main._tokens[Main._tokensIndex].CP in ['ID','self', 'while','for','if','def']:149 if Main._tokens[Main._tokensIndex].CP =='ID':150 Main._tokensIndex += 1151 if self.S_ST2():152 return True153 elif Main._tokens[Main._tokensIndex].CP == 'self':154 Main._tokensIndex += 1155 if Main._tokens[Main._tokensIndex].CP == '.':156 Main._tokensIndex += 1157 if Main._tokens[Main._tokensIndex].CP == 'ID':158 Main._tokensIndex += 1159 if Main._tokens[Main._tokensIndex].CP == '(':160 Main._tokensIndex += 1161 if self.DECL_ASGN():162 Main._tokensIndex += 1163 if Main._tokens[Main._tokensIndex].CP == ')':164 return True165 else:166 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP,Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))167 else:168 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))169 else:170 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))171 else:172 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))173 elif Main._tokens[Main._tokensIndex].CP == 'while':174 if self.WHILE_ST():175 return True176 elif Main._tokens[Main._tokensIndex].CP == 'for':177 if self.FOR_ST():178 return True179 elif Main._tokens[Main._tokensIndex].CP == 'if':180 if self.IF_ELSE():181 return True182 elif Main._tokens[Main._tokensIndex].CP == 'def':183 if self.FUNC_DEF():184 return True185 else:186 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))187 else:188 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP,Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))189 def S_ST2(self):190 # if Main._tokens[Main._tokensIndex].CP in ['=',',','ASGN_OPT',',','(','elif','else','ID', 'self', 'while', 'for', 'if','}']:191 if Main._tokens[Main._tokensIndex].CP in ['=',',','ASGN_OPT']:192 if self.LIST():193 return True194 elif Main._tokens[Main._tokensIndex].CP in ['.','(']:195 if self.FUNC_CALL1():196 return True197 else:198 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP,Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))199 def WHILE_ST(self):200 if Main._tokens[Main._tokensIndex].CP == 'while':201 Main._tokensIndex += 1202 if Main._tokens[Main._tokensIndex].CP == '(':203 Main._tokensIndex += 1204 if self.E():205 Main._tokensIndex += 1206 if Main._tokens[Main._tokensIndex].CP == ')':207 Main._tokensIndex += 1208 if Main._tokens[Main._tokensIndex].CP == ':':209 Main._tokensIndex += 1210 if self.BODY():211 return True212 else:213 sys.exit(214 self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))215 else:216 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))217 else:218 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))219 else:220 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP,Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))221 def FOR_ST(self):222 if Main._tokens[Main._tokensIndex].CP == 'for':223 Main._tokensIndex += 1224 if Main._tokens[Main._tokensIndex].CP == '(':225 Main._tokensIndex += 1226 if self.X():227 Main._tokensIndex += 1228 if Main._tokens[Main._tokensIndex].CP == ';':229 Main._tokensIndex += 1230 if self.Y():231 Main._tokensIndex += 1232 if Main._tokens[Main._tokensIndex].CP == ';':233 if self.X():234 Main._tokensIndex += 1235 if Main._tokens[Main._tokensIndex].CP == ')':236 Main._tokensIndex += 1237 if self.BODY():238 return True239 else:240 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP,Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))241 else:242 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP,Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))243 else:244 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))245 else:246 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))247 else:248 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP,Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))249 def X(self):250 if self.DECL_ASGN():251 return True252 else:253 Main._tokensIndex -= 1254 return True255 def Y(self):256 if self.E():257 return True258 else:259 Main._tokensIndex += 1260 return True261 def DECL_ASGN(self):262 if Main._tokens[Main._tokensIndex].CP=="ID":263 Main._tokensIndex+=1264 if self.LIST():265 return True266 else:267 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP,Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))268 def LIST(self):269 if Main._tokens[Main._tokensIndex].CP in ['=',',','ASGN_OPT']:270 if self.INIT():271 Main._tokensIndex += 1272 if self.LIST2():273 return True274 elif Main._tokens[Main._tokensIndex].CP=="ASGN_OPT":275 Main._tokensIndex += 1276 if self.E():277 Main._tokensIndex += 1278 if self.LIST2():279 return True280 else:281 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))282 else:283 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP,Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))284 def INIT(self):285 if Main._tokens[Main._tokensIndex].CP == "=":286 Main._tokensIndex += 1287 if self.INIT2():288 return True289 else:290 Main._tokensIndex -= 1291 return True292 def LIST2(self):293 if Main._tokens[Main._tokensIndex].CP==',':294 Main._tokensIndex += 1295 if self.DECL_ASGN():296 return True297 else:298 Main._tokensIndex -= 1299 return True300 def INIT2(self):301 if self.E():302 Main._tokensIndex += 1303 if self.INIT3():304 return True305 else:306 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP,Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))307 def INIT3(self):308 if Main._tokens[Main._tokensIndex].CP =='=':309 if self.INIT():310 return True311 else:312 Main._tokensIndex -= 1313 return True314 def E(self):315 if self.F():316 Main._tokensIndex += 1317 if self.E1():318 return True319 else:320 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP,Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))321 def E1(self):322 if Main._tokens[Main._tokensIndex].VP == '||':323 Main._tokensIndex += 1324 if self.F():325 Main._tokensIndex += 1326 if self.E1():327 return True328 else:329 Main._tokensIndex -= 1330 return True331 def F(self):332 if self.G():333 Main._tokensIndex += 1334 if self.F1():335 return True336 else:337 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP,Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))338 def F1(self):339 if Main._tokens[Main._tokensIndex].VP == '&&':340 Main._tokensIndex += 1341 if self.G():342 Main._tokensIndex += 1343 if self.F1():344 return True345 else:346 Main._tokensIndex -= 1347 return True348 def G(self):349 if self.H():350 Main._tokensIndex += 1351 if self.G1():352 return True353 else:354 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP,Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))355 def G1(self):356 if Main._tokens[Main._tokensIndex].CP=='RO':357 Main._tokensIndex += 1358 if self.H():359 Main._tokensIndex += 1360 if self.G1():361 return True362 else:363 Main._tokensIndex -= 1364 return True365 def H(self):366 if self.I():367 Main._tokensIndex += 1368 if self.H1():369 return True370 else:371 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP,Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))372 def H1(self):373 if Main._tokens[Main._tokensIndex].CP=='ADD_SUB':374 Main._tokensIndex += 1375 if self.I():376 Main._tokensIndex += 1377 if self.H1():378 return True379 else:380 Main._tokensIndex -= 1381 return True382 def I(self):383 if self.J():384 Main._tokensIndex += 1385 if self.I1():386 return True387 else:388 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP,Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))389 def I1(self):390 if Main._tokens[Main._tokensIndex].CP == 'DIV_MUL':391 Main._tokensIndex += 1392 if self.J():393 Main._tokensIndex += 1394 if self.I1():395 return True396 else:397 Main._tokensIndex -= 1398 return True399 def J(self):400 if Main._tokens[Main._tokensIndex].CP in ['ID', 'LO', '(', 'INC_DEC', 'self', 'INT_CONST', 'FLT_CONST','STR_CONST', 'CHAR_CONST','[']:401 if Main._tokens[Main._tokensIndex].CP=="ID":402 Main._tokensIndex += 1403 if self.J2():404 return True405 elif Main._tokens[Main._tokensIndex].CP=="(":406 Main._tokensIndex += 1407 if self.E():408 Main._tokensIndex += 1409 if Main._tokens[Main._tokensIndex].CP==")":410 return True411 else:412 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))413 elif Main._tokens[Main._tokensIndex].VP=="!":414 Main._tokensIndex += 1415 if self.J1():416 return True417 elif Main._tokens[Main._tokensIndex].CP=="INC_DEC":418 Main._tokensIndex += 1419 if Main._tokens[Main._tokensIndex].CP=="ID":420 return True421 else:422 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))423 elif Main._tokens[Main._tokensIndex].CP in ['self', 'ID']:424 if self.FUNC_CALL():425 return True426 elif Main._tokens[Main._tokensIndex].CP in ['INT_CONST', 'FLT_CONST', 'STR_CONST', 'CHAR_CONST']:427 if self.CONSTANT():428 return True429 elif Main._tokens[Main._tokensIndex].CP == "[":430 if self.ARRAY_LIST():431 return True432 else:433 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))434 else:435 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP,Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))436 def J1(self):437 if self.J():438 return True439 else:440 Main._tokensIndex -= 1441 return True442 def J2(self):443 if Main._tokens[Main._tokensIndex].CP=='INC_DEC':444 return True445 else:446 Main._tokensIndex -= 1447 return True448 def ARRAY_LIST(self):449 if Main._tokens[Main._tokensIndex].CP == "[":450 Main._tokensIndex += 1451 if self.VALS():452 Main._tokensIndex += 1453 if Main._tokens[Main._tokensIndex].CP == "]":454 return True455 else:456 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))457 else:458 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP,Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))459 def VALS(self):460 if Main._tokens[Main._tokensIndex].CP in ['ID', 'LO', '(', 'INC_DEC', 'self', 'INT_CONST', 'FLT_CONST','STR_CONST', 'CHAR_CONST', '[',',']:461 if Main._tokens[Main._tokensIndex].CP == ",":462 Main._tokensIndex += 1463 if self.E():464 Main._tokensIndex += 1465 if self.VALS():466 return True467 elif self.E():468 Main._tokensIndex += 1469 if self.VALS():470 return True471 else:472 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))473 else:474 Main._tokensIndex -= 1475 return True476 def CONSTANT(self):477 if Main._tokens[Main._tokensIndex].CP in ['INT_CONST', 'FLT_CONST','STR_CONST', 'CHAR_CONST']:478 return True479 else:480 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP,Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))481 def FUNC_CALL(self):482 if Main._tokens[Main._tokensIndex].CP in ['self','ID']:483 if Main._tokens[Main._tokensIndex].CP == "self":484 Main._tokensIndex += 1485 if Main._tokens[Main._tokensIndex].CP == ".":486 Main._tokensIndex += 1487 if Main._tokens[Main._tokensIndex].CP == "ID":488 Main._tokensIndex += 1489 if Main._tokens[Main._tokensIndex].CP == "(":490 Main._tokensIndex += 1491 if self.PARAMS():492 Main._tokensIndex += 1493 if Main._tokens[Main._tokensIndex].CP == ")":494 return True495 else:496 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP,Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))497 else:498 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))499 else:500 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))501 else:502 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))503 elif Main._tokens[Main._tokensIndex].CP == "ID":504 Main._tokensIndex += 1505 if self.FUNC_CALL1():506 return True507 else:508 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))509 else:510 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP,Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))511 def FUNC_CALL1(self):512 if Main._tokens[Main._tokensIndex].CP==".":513 Main._tokensIndex += 1514 if Main._tokens[Main._tokensIndex].CP == "ID":515 Main._tokensIndex += 1516 if Main._tokens[Main._tokensIndex].CP == "(":517 Main._tokensIndex += 1518 if self.PARAMS():519 Main._tokensIndex += 1520 if Main._tokens[Main._tokensIndex].CP == ")":521 return True522 else:523 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))524 else:525 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))526 else:527 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))528 elif Main._tokens[Main._tokensIndex].CP == "(":529 Main._tokensIndex += 1530 if self.FUNC_CALL2():531 return True532 else:533 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP,Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))534 def FUNC_CALL2(self):535 if Main._tokens[Main._tokensIndex].CP == ")":536 Main._tokensIndex += 1537 if self.FUNC_CALL3():538 return True539 elif self.DECL_ASGN():540 return True541 else:542 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP,Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))543 def FUNC_CALL3(self):544 if Main._tokens[Main._tokensIndex].CP=='.':545 Main._tokensIndex += 1546 if Main._tokens[Main._tokensIndex].CP=='ID':547 Main._tokensIndex += 1548 if Main._tokens[Main._tokensIndex].CP == '(':549 Main._tokensIndex += 1550 if self.PARAMS():551 Main._tokensIndex += 1552 if Main._tokens[Main._tokensIndex].CP == ')':553 return True554 else:555 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))556 else:557 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))558 else:559 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))560 else:561 Main._tokensIndex -= 1562 return True563 def PARAMS(self):564 if self.DECL_ASGN():565 return True566 else:567 Main._tokensIndex -= 1568 return True569 def IF_ELSE(self):570 if Main._tokens[Main._tokensIndex].CP=="if":571 Main._tokensIndex += 1572 if Main._tokens[Main._tokensIndex].CP == "(":573 Main._tokensIndex += 1574 if self.E():575 Main._tokensIndex += 1576 if Main._tokens[Main._tokensIndex].CP == ")":577 if Main._tokens[Main._tokensIndex].CP == ":":578 Main._tokensIndex += 1579 if self.BODY():580 Main._tokensIndex += 1581 if self.ELIF():582 Main._tokensIndex += 1583 if self.O_ELSE():584 return True585 else:586 sys.exit(587 self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))588 else:589 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))590 else:591 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))592 else:593 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP,Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))594 def ELIF(self):595 if Main._tokens[Main._tokensIndex].CP == "elif":596 Main._tokensIndex += 1597 if self.E():598 Main._tokensIndex += 1599 if Main._tokens[Main._tokensIndex].CP == ")":600 Main._tokensIndex += 1601 if Main._tokens[Main._tokensIndex].CP == ":":602 Main._tokensIndex += 1603 if self.BODY():604 return True605 else:606 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))607 else:608 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))609 else:610 Main._tokensIndex -= 1611 return True612 def O_ELSE(self):613 if Main._tokens[Main._tokensIndex].CP == "else":614 Main._tokensIndex += 1615 if Main._tokens[Main._tokensIndex].CP == ":":616 Main._tokensIndex += 1617 if self.BODY():618 return True619 else:620 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))621 else:622 Main._tokensIndex -= 1623 return True624 def BODY(self):625 if self.S_ST():626 return True627 elif Main._tokens[Main._tokensIndex].CP == "{":628 Main._tokensIndex += 1629 if self.M_ST():630 Main._tokensIndex += 1631 if Main._tokens[Main._tokensIndex].CP == "}":632 return True633 else:634 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP, Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))635 else:636 sys.exit(self.errorPrint(Main._tokens[Main._tokensIndex].CP,Main._tokens[Main._tokensIndex].VP,Main._tokens[Main._tokensIndex].LN))637 #endregion638 def mainMethod(self,f):639 lex = Lexical()640 self.flagStr=0;self.flagChar=0;self.flagFloat=0641 while True:642 ch = str(f.read(1), 'utf-8')643 if not ch: break644 # working for separation of float and ID645 if ch == '.':646 if self.flagFloat:647 if lex.chk_FLT_CONST(Main._temp, Main._lineNum):648 self.printToken("FLT_CONST", Main._temp, Main._lineNum)649 Main._tokens.append(Token("FLT_CONST", Main._temp, Main._lineNum))650 f.seek(-1,1)651 Main._temp = ""652 self.flagFloat=0653 continue654 elif Main._temp=="":655 OneRight = str(f.read(1), 'utf-8')656 if re.match("[0-9]",OneRight):657 self.flagFloat=1658 Main._temp+=ch+OneRight659 continue660 else:661 f.seek(-1,1)662 elif Main._temp!="":663 f.seek(-2, 1)664 OneLeft = str(f.read(1), 'utf-8')665 f.seek(1, 1)666 OneRight = str(f.read(1), 'utf-8')667 if (re.match("[0-9]", OneLeft) or OneLeft in ['+', '-']) and re.match("[0-9]", OneRight):668 self.flagFloat=1669 Main._temp += ch+OneRight670 continue671 else:672 f.seek(-1,1)673 elif ch == '+' or ch == '-':674 OneRight = str(f.read(1), 'utf-8')675 if ch == OneRight:676 if Main._temp=="":677 Main._temp = ch + OneRight678 self.printToken("INC_DEC", Main._temp, Main._lineNum)679 Main._tokens.append(Token("INC_DEC", Main._temp, Main._lineNum))680 Main._temp = ""681 continue682 else:683 f.seek(-1, 1)684 else:685 f.seek(-1, 1)686 f.seek(-2, 1)687 OneLeft = str(f.read(1), 'utf-8')688 f.seek(1, 1)689 OneRight = str(f.read(1), 'utf-8')690 TwoRight = str(f.read(1), 'utf-8')691 f.seek(-2, 1)692 if (not re.match("[0-9A-Za-z]", OneLeft)) and (re.match("[0-9]", OneRight) or re.match("[0-9]", TwoRight)):693 Main._temp += ch694 continue695 if ch in Main._breakers:696 if ch =='"' and self.flagStr==1:697 Main._temp+=ch698 if lex.chk_FLT_CONST(Main._temp, Main._lineNum):699 self.printToken("FLT_CONST", Main._temp, Main._lineNum)700 Main._tokens.append(Token("FLT_CONST", Main._temp, Main._lineNum))701 self.flagFloat=0702 Main._temp = ""703 if lex.chk_keywords(Main._temp, Main._lineNum):704 if Main._temp in ['True','False']:705 self.printToken("BOOL", Main._temp, Main._lineNum)706 Main._tokens.append(Token("BOOL", Main._temp, Main._lineNum))707 else:708 self.printToken(Main._temp,"-", Main._lineNum)709 Main._tokens.append(Token(Main._temp,"-", Main._lineNum))710 Main._temp = ""711 elif lex.chk_ID(Main._temp, Main._lineNum):712 self.printToken("ID", Main._temp, Main._lineNum)713 Main._tokens.append(Token("ID", Main._temp, Main._lineNum))714 Main._temp = ""715 elif lex.chk_INT_CONST(Main._temp, Main._lineNum):716 self.printToken("INT_CONST", Main._temp, Main._lineNum)717 Main._tokens.append(Token("INT_CONST", Main._temp, Main._lineNum))718 Main._temp = ""719 elif lex.chk_STR_CONST(Main._temp, Main._lineNum):720 self.flagStr=0721 ch=''722 self.printToken("STR_CONST", Main._temp[1:-1], Main._lineNum)723 Main._tokens.append(Token("STR_CONST", Main._temp[1:-1], Main._lineNum))724 Main._temp = ""725 elif Main._temp != "":726 Main._temp = "Error at " + str(Main._lineNum) + " where value is " + Main._temp727 Main._fileData += Main._temp + "\n"728 Main._temp = ""729 if ch not in Main._invalidPrint and ch!='':730 self.printToken(str(ch), '-', Main._lineNum)731 Main._tokens.append(Token(str(ch), '-', Main._lineNum))732 if ch == '\n':733 Main._lineNum += 1734 else:735 Main._temp += str(ch)736 if ch == '"':737 Main._temp+=ch738 while True:739 ch = str(f.read(1), 'utf-8')740 if ch =='\\':741 OneRight = str(f.read(1), 'utf-8')742 if OneRight=='"' or OneRight=='\\':743 Main._temp += OneRight744 else:745 f.seek(-1, 1)746 Main._temp += ch747 elif ch not in ['"','\n']:748 Main._temp+=ch749 elif ch == '"':750 f.seek(-1,1)751 self.flagStr = 1752 break753 elif ch == '\n':754 f.seek(-1, 1)755 break756 if ch == '\'':757 Main._temp+=ch758 char1 = str(f.read(1), 'utf-8')759 char2 = str(f.read(1), 'utf-8')760 if char1=='\\':761 char3 = str(f.read(1), 'utf-8')762 if char2=='\'':763 Main._temp += char2 + char3764 elif char3!='\n':765 Main._temp += char1 + char2 + char3766 else:767 Main._temp += char1768 if char3=='\n':769 Main._lineNum+=1770 elif char2!='\n':771 Main._temp += char1 + char2772 elif char2 == '\n':773 Main._lineNum += 1774 if lex.chk_CHAR_CONST(Main._temp, Main._lineNum):775 self.printToken("CHAR_CONST", Main._temp[1:-1], Main._lineNum)776 Main._tokens.append(Token("CHAR_CONST", Main._temp[1:-1], Main._lineNum))777 Main._temp = ""778 else:779 Main._temp = "Error at " + str(Main._lineNum) + " where value is " + Main._temp780 Main._fileData += Main._temp + "\n"781 Main._temp = ""782 if ch =='#':783 OneRight = str(f.read(1), 'utf-8')784 if OneRight == '*':785 while True:786 first = str(f.read(1), 'utf-8')787 if not first:788 break789 if first == '*':790 second = str(f.read(1), 'utf-8')791 if second == '#':792 break793 elif first == '\n':794 Main._lineNum+=1795 else:796 f.readline()797 Main._lineNum+=1798 continue799 # check for inc_DEc and add_sub and asgn800 if ch in ['+', '-', '*', '/', '%']:801 OneRight = str(f.read(1), 'utf-8')802 if ch == OneRight:803 Main._temp = ch + ch804 self.printToken("INC_DEC", Main._temp, Main._lineNum)805 Main._tokens.append(Token("INC_DEC", Main._temp, Main._lineNum))806 Main._temp = ""807 elif OneRight == '=':808 Main._temp = ch + OneRight809 self.printToken("ASGN_OPT", Main._temp, Main._lineNum)810 Main._tokens.append(Token("ASGN_OPT", Main._temp, Main._lineNum))811 Main._temp = ""812 elif ch in ['+', '-']:813 f.seek(-1, 1)814 self.printToken("ADD_SUB", str(ch), Main._lineNum)815 Main._tokens.append(Token("ADD_SUB", str(ch), Main._lineNum))816 elif ch in ['*','/', '%']:817 f.seek(-1, 1)818 self.printToken("DIV_MUL", str(ch), Main._lineNum)819 Main._tokens.append(Token("DIV_MUL", str(ch), Main._lineNum))820 else:821 f.seek(-1, 1)822 # check for RO823 if ch in ['<', '>', '=', '!']:824 OneRight = str(f.read(1), 'utf-8')825 if OneRight == '=':826 Main._temp = ch + OneRight827 self.printToken("RO", Main._temp, Main._lineNum)828 Main._tokens.append(Token("RO", Main._temp, Main._lineNum))829 Main._temp = ""830 elif ch == '=':831 f.seek(-1, 1)832 self.printToken('=', '-', Main._lineNum)833 Main._tokens.append(Token('=', '-', Main._lineNum))834 elif ch != '!':835 f.seek(-1, 1)836 self.printToken("RO", str(ch), Main._lineNum)837 Main._tokens.append(Token("RO", str(ch), Main._lineNum))838 elif ch == '!':839 f.seek(-1, 1)840 self.printToken("LO", str(ch), Main._lineNum)841 Main._tokens.append(Token("LO", str(ch), Main._lineNum))842 # self.printToken("LO", str(ch), Main._lineNum)843 # check for LO844 if ch in ['&', '|']:845 OneRight = str(f.read(1), 'utf-8')846 if ch == OneRight:847 Main._temp = ch + OneRight848 self.printToken("LO", Main._temp, Main._lineNum)849 Main._tokens.append(Token("LO", Main._temp, Main._lineNum))850 Main._temp = ""851 elif ch != OneRight:852 temp = "Error at " + str(Main._lineNum) + " where value is " + str(ch)853 Main._fileData += temp + "\n"854 f.seek(-1, 1)855 Main._temp = ""856 self.printToken("$", "-", Main._lineNum-1)857 Main._tokens.append(Token("$", "-", Main._lineNum-1))858 def printToken(self,CPart, VPart, line):859 string = "( " + CPart + " , " + VPart + " , " + str(line) + " )"860 print(string)861 Main._fileData += string + "\n"862 def errorPrint(self,classPart,valuePart,lineNum):...
Alg-NearestPoint.py
Source:Alg-NearestPoint.py
1# -*- coding: utf-8 -*-2# ÌâÄ¿£ºÉèp1=(x1, y1), p2=(x2, y2), ¡, pn=(xn, yn)ÊÇƽÃæÉÏn¸öµã¹¹³ÉµÄ¼¯ºÏS£¬Éè¼ÆËã·¨ÕÒ³ö¼¯ºÏSÖоàÀë×î½üµÄµã¶Ô¡£3import math,sys,random4# ½â·¨Ò»£º5# ÒÑÖª¼¯ºÏSÖÐÓÐn¸öµã£¬Ò»¹²¿ÉÒÔ×é³Én(n-1)/2¶Ôµã¶Ô£¬ÂùÁ¦·¨¾ÍÊǶÔÕân(n-1)/2¶Ôµã¶ÔÖð¶Ô½øÐоàÀë¼ÆË㣬ͨ¹ýÑ»·ÇóµÃµã¼¯ÖеÄ×î½üµã¶Ô.6def nearestPoint1(Points):7 nearestDistance=sys.maxint8 nearestPointA=[]9 nearestPointB=[]10 for onePoint in Points:11 for anotherPoint in Points:12 if onePoint != anotherPoint:13 distance=math.sqrt(math.pow((onePoint[0]-anotherPoint[0]),2)+math.pow((onePoint[1]-anotherPoint[1]),2))14 if distance<nearestDistance:15 nearestDistance=distance16 nearestPointA=onePoint17 nearestPointB=anotherPoint18 print 'F',nearestPointA,'T',nearestPointB,'=',nearestDistance19# ½â·¨¶þ£º20# Ëã·¨ÃèÊö£ºÒÑÖª¼¯ºÏSÖÐÓÐn¸öµã£¬·ÖÖ稵Ä˼Ïë¾ÍÊǽ«S½øÐвð·Ö£¬·ÖΪ2²¿·ÖÇó×î½üµã¶Ô¡£Ë㷨ÿ´ÎÑ¡ÔñÒ»Ìõ´¹ÏßL£¬½«S²ð·Ö×óÓÒÁ½²¿·ÖΪSLºÍSR£¬LÒ»°ãÈ¡µã¼¯SÖÐËùÓеãµÄÖмäµãµÄx×ø±êÀ´»®·Ö£¬ÕâÑù¿ÉÒÔ±£Ö¤SLºÍSRÖеĵãÊýÄ¿¸÷Ϊn/221# ÒÀ´ÎÕÒ³öÕâÁ½²¿·ÖÖеÄ×îСµã¶Ô¾àÀ룺¦ÄLºÍ¦ÄR£¬¼ÇSLºÍSRÖÐ×îСµã¶Ô¾àÀë¦Ä = min£¨¦ÄL£¬¦ÄR£©£¬22# ÒÔLΪÖÐÏߣ¬¦ÄΪ°ë¿í»®·ÖÒ»¸ö³¤´ø£¬×îС¾àÀë¶Ô»¹ÓпÉÄÜ´æÔÚÓÚSLºÍSRµÄ½»½ç´¦£¬pµãºÍqµã·Ö±ðλÓÚSLºÍSRµÄÐéÏß·¶Î§ÄÚ¡£pµã²»±ØÓëËùÓеÄqµã¼ÆËã¾àÀ룬ֻÐè¼ÆËãÓëÂú×ã¹Øϵʽ(q[x]-p[x])<=2*¦Ä and |q[y]-p[y]|<=¦ÄµÄqµãµÄ¾àÀë¡£23def nearestPoint2(Points):24 if len(Points)==1:return sys.maxint25 if len(Points)==2:return math.sqrt(math.pow((Points[0][0]-Points[1][0]),2)+math.pow((Points[0][1]-Points[1][1]),2))26 27 DivideX=sum([onePoint[0] for onePoint in Points])/len(Points)28 leftPoints=[onePoint for onePoint in Points if onePoint[0]<=DivideX]29 rightPoints=[onePoint for onePoint in Points if onePoint[0]>DivideX]30 31 leftNearestDistance=nearestPoint2(leftPoints)32 rightNearestDistance=nearestPoint2(rightPoints)33 34 MDist=min(leftNearestDistance,rightNearestDistance)35 leftMidPoints=[onePoint for onePoint in leftPoints if (DivideX-onePoint[0])<=MDist]36 rightMidPoints=[onePoint for onePoint in rightPoints if (onePoint[0]-DivideX)<=MDist]37 38 midNearestDistance=sys.maxint39 for oneleftPoint in leftMidPoints:40 partRightPoints=[onerightPoint for onerightPoint in rightMidPoints if (onerightPoint[0]-oneleftPoint[0])<=2*MDist and abs(onerightPoint[1]-oneleftPoint[1])<=MDist]41 for onerightPoint in partRightPoints:42 distance=math.sqrt(math.pow((oneleftPoint[0]-onerightPoint[0]),2)+math.pow((oneleftPoint[1]-onerightPoint[1]),2))43 if distance<midNearestDistance:44 midNearestDistance=distance45 46 return min(midNearestDistance,MDist)47if __name__=='__main__': 48 # È¡30¸öµã£¬x,y×ø±ê·¶Î§¶¼ÔÚ1-100Ö®¼ä49 Points=[[random.randint(1,100),random.randint(1,100)] for one in range(30)]50 nearestPoint1(Points)51 print '*'*2052 Points=sorted(Points,key=lambda x:(x[0],x[1]),reverse=False)...
postfixlogic.py
Source:postfixlogic.py
1__author__ = "Dmitry Philippov"2import sys3sys.stdout = open("postfixlogic.out", "w")4def main():5 print("""16S 0 -> now 0 >7S 1 -> now 1 >8now _ -> fillblank _ <9fillblank * -> fillblank _ <10fillblank 0 -> AC 0 ^11fillblank 1 -> AC 1 ^12now 0 -> zeroleft * <13zeroleft _ -> setzero _ >14zeroleft * -> zeroleft * <15zeroleft _ -> now _ ^16zeroleft 0 -> setzero 0 >17zeroleft 1 -> setzero 1 >18setzero * -> zeroright 0 >19zeroright * -> zeroright * >20zeroright _ -> now _ ^21zeroright 0 -> now 0 ^22zeroright 1 -> now 1 ^23zeroright o -> now o ^24zeroright a -> now a ^25now 1 -> oneleft * <26oneleft _ -> setone _ >27oneleft * -> oneleft * <28oneleft 0 -> setone 0 >29oneleft 1 -> setone 1 >30setone * -> oneright 1 >31oneright * -> oneright * >32oneright 0 -> now 0 ^33oneright 1 -> now 1 ^34oneright o -> now o ^35oneright a -> now a ^36now a -> andleft * <37andleft * -> andleft * <38andleft 0 -> andleftzero * <39andleftzero 0 -> andright 0 >40andleftzero 1 -> andright 0 >41andright * -> andright * >42andright _ -> now _ ^43andright 0 -> now 0 ^44andright 1 -> now 1 ^45andright o -> now o ^46andright a -> now a ^47andleft 1 -> andleftone * <48andleftone 0 -> andright 0 >49andleftone 1 -> andright 1 >50now o -> orleft * <51orleft * -> orleft * <52orleft 0 -> orleftzero * <53orleftzero 0 -> orright 0 >54orleftzero 1 -> orright 1 >55orright * -> orright * >56orright _ -> now _ ^57orright 0 -> now 0 ^58orright 1 -> now 1 ^59orright o -> now o ^60orright a -> now a ^61orleft 1 -> orleftone * <62orleftone 0 -> orright 1 >63orleftone 1 -> orright 1 >""")64if __name__ == "__main__":...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!