def termo(): left = Parser.fator() while Parser.tokens.actual.type == 'MULT' or Parser.tokens.actual.type == 'DIV' or Parser.tokens.actual.type == 'AND': if Parser.tokens.actual.type == 'MULT': Parser.tokens.selectNext() right = Parser.fator() left = nd.BinOp('*', [left, right]) elif Parser.tokens.actual.type == 'DIV': Parser.tokens.selectNext() right = Parser.fator() left = nd.BinOp('/', [left, right]) elif Parser.tokens.actual.type == 'AND': Parser.tokens.selectNext() right = Parser.fator() left = nd.BinOp('AND', [left, right]) else: print("ultimate super master error") return left
def relExpression(): left = Parser.parseExpression() if (Parser.tokens.actual.type in ['=', '>', '<']): bin_op = Parser.tokens.actual.type Parser.tokens.selectNext() right = Parser.parseExpression() return nd.BinOp(bin_op, [left, right]) else: return left
def parseExpression(): left = Parser.termo() while Parser.tokens.actual.type == 'PLUS' or Parser.tokens.actual.type == 'MINUS' or Parser.tokens.actual.type == 'OR': # if actual token is + if Parser.tokens.actual.type == 'PLUS': Parser.tokens.selectNext() right = Parser.termo() left = nd.BinOp('+', [left, right]) # if actual token is - elif Parser.tokens.actual.type == 'MINUS': Parser.tokens.selectNext() right = Parser.termo() left = nd.BinOp('-', [left, right]) elif Parser.tokens.actual.type == 'OR': Parser.tokens.selectNext() right = Parser.termo() left = nd.BinOp('OR', [left, right]) else: print("ultimate super master error") # end of while return left
def parseExpression(): node = Parser.parseTerm() current = Parser.tokens.actual while (current.type == "PLUS" or current.type == "MINUS" or current.type == "OR") and current.type != "EOF": if current.type == "PLUS": Parser.tokens.selectNext() term = Parser.parseTerm() node = n.BinOp("PLUS", [node, term]) elif current.type == "MINUS": Parser.tokens.selectNext() term = Parser.parseTerm() node = n.BinOp("MINUS", [node, term]) elif current.type == "OR": Parser.tokens.selectNext() term = Parser.parseTerm() node = n.BinOp("OR", [node, term]) current = Parser.tokens.actual return node
def parseTerm(): node = Parser.parseFactor() current = Parser.tokens.actual while (current.type == "TIMES" or current.type == "DIVIDED" or current.type == "AND") and current.type != "EOF": if current.type == "TIMES": Parser.tokens.selectNext() factor = Parser.parseFactor() node = n.BinOp("TIMES", [node, factor]) elif current.type == "DIVIDED": Parser.tokens.selectNext() factor = Parser.parseFactor() node = n.BinOp("DIVIDED", [node, factor]) elif current.type == "AND": Parser.tokens.selectNext() factor = Parser.parseFactor() node = n.BinOp("AND", [node, factor]) current = Parser.tokens.actual return node
def parseRelExpression(): node = Parser.parseExpression() current = Parser.tokens.actual while (current.type == "GREATER" or current.type == "LESS" or current.type == "EQUALS") and current.type != "EOF": if current.type == "GREATER": Parser.tokens.selectNext() exp = Parser.parseExpression() node = n.BinOp("GREATER", [node, exp]) elif current.type == "LESS": Parser.tokens.selectNext() exp = Parser.parseExpression() node = n.BinOp("LESS", [node, exp]) elif current.type == "EQUALS": Parser.tokens.selectNext() exp = Parser.parseExpression() node = n.BinOp("EQUALS", [node, exp]) current = Parser.tokens.actual return node