Exemple #1
0
def reduce(top, token):  #Hace todo el procedimiento del reduce
    global stack
    global symbols

    state = getState(top, token.lexeme)
    semantic_function = None
    poppedList = []
    production = grammar[state]

    head = production[0]
    result = None
    rules = production[1]

    if (not (len(rules) == 1 and rules[0] == b'\xce\xb5')):
        for i in range(0, len(rules)):
            stack.pop()
            poppedList.append(symbols.pop())

    pushNextState(stack[-1], head)

    #Si tiene acción semántica
    if production[2] and not errorDetected:
        result = semantic_functions[state](head, poppedList)
    else:
        result = InputToken(head)

    symbols.append(result)

    ruleString = ""
    for rule in rules:
        ruleString += rule.decode('utf-8')

    return [head, ruleString]
def semantic62(head, poppedList):
    inputStream = ""
    if poppedList[1].value == '':
        inputStream = input()
    else:
        inputStream = input(poppedList[1].value)
    inputStream = castValue(inputStream)
    return InputToken(head, inputStream)
def semantic59(head, poppedList):
    value = poppedList[1].value
    result = InputToken(head, value)

    for operation in reversed(poppedList[3].value):
        result.value = op[operation](result.value)

    return result
def semantic58(head, poppedList):
    identifier = poppedList[0].value
    value = symbolsTable[identifier]
    result = InputToken(head, value)

    for operation in reversed(poppedList[1].value):
        result.value = op[operation](result.value)

    return result
def semantic71(head, poppedList):
    address_port = poppedList[3].value
    ip_address = address_port[:address_port.index(':')]
    print(ip_address)
    port = address_port[address_port.index(':') + 1:]
    socket.inet_aton(ip_address)
    vehicle = connect(address_port, wait_ready=True)
    height = poppedList[1].value
    arm_and_takeoff(vehicle, height)
    return InputToken(head, vehicle)
def semantic56(head,
               poppedList):  # In this instance poppedList must be [NUMBER,Si]
    number = poppedList[0].value
    lexeme = poppedList[0].lexeme
    result = InputToken(head, number)

    for operation in reversed(poppedList[1].value):
        result.value = op[operation](result.value)

    return result
Exemple #7
0
def handleError(top):
    global stack
    global tokens
    global symbols
    global errorDetected

    errorDetected = True
    inputToken = tokens[0]
    validState = False

    while not validState:
        try:
            s = stack[-1]
            orig = stack[-1]
            values = list(matrix[s].values())

            while not 'G' in ''.join(values):
                values = list(matrix[stack[-2]].values())
                stack.pop()
                s = stack[-1]

            for i in range(len(matrix[s]) - 1, -1, -1):
                l = list(matrix[s].values())
                k = list(matrix[s].keys())

                if 'G' in l[i]:
                    currVal = int(l[i][1:])
                    currKey = InputToken(k[i])

                    if 'S' in ''.join(list(matrix[currVal].values())):
                        stack.append(currVal)
                        symbols.pop()
                        symbols.append(currKey)
                        validState = True
                        break
        except:
            if inputToken.lexeme == '$':
                raise Exception(makeErrorStr(inputToken.lexeme))

            raise Exception(errorStr)
    s = currVal
    try:
        while not tokens[0].lexeme in matrix[currVal]:
            try:
                tokens.pop(0)
            except:
                if inputToken.lexeme == '$':
                    raise Exception(makeErrorStr(inputToken.lexeme))
                raise Exception(errorStr)
    except:
        if inputToken.lexeme == '$':
            raise Exception(makeErrorStr(inputToken.lexeme))
        raise Exception(makeErrorStr(inputToken.lexeme))
def semantic90(head, poppedList):
    identifier1 = poppedList[3].value
    identifier2 = poppedList[1].value
    address_port = symbolsTable[identifier1]
    print(address_port)
    ip_address = address_port[:address_port.index(':')]
    print(ip_address)
    port = address_port[address_port.index(':') + 1:]
    socket.inet_aton(ip_address)
    vehicle = connect(address_port, wait_ready=True)
    height = symbolsTable[identifier2]
    arm_and_takeoff(vehicle, height)
    return InputToken(head, vehicle)
def semantic77(head, poppedList):
    identifier = poppedList[3].value
    vehicle = symbolsTable[identifier]
    targets = poppedList[1].value
    attach_pois(targets, vehicle, vehicle.location.global_frame)
    vehicle.commands.next = 0
    vehicle.mode = VehicleMode("AUTO")
    while True:
        next_way_point = vehicle.commands.next
        print("Going to {} poi".format(next_way_point))
        if (len(targets) == next_way_point):
            print("Mission finished")
            break
        time.sleep(2)
    return InputToken(head, vehicle)
def semantic89(head, poppedList):
    vehicle = connect(poppedList[1].value, wait_ready=True)
    return InputToken(head, vehicle)
def semantic88(head, poppedList):
    return InputToken(head, '')
def semantic76(head, poppedList):
    identifier = poppedList[3].value
    vehicle = symbolsTable[identifier]
    vehicle.mode = VehicleMode("AUTO")
    vehicle.airspeed = poppedList[1].value
    return InputToken(head, vehicle)
def semantic73(head, poppedList):
    vehicle = symbolsTable[poppedList[3].value]
    coords = poppedList[1].value
    a_location = LocationGlobalRelative(coords[0], coords[1], coords[2])
    vehicle.simple_goto(a_location)
    return InputToken(head, vehicle)
def semantic72(head, poppedList):
    elements = []
    elements.append(poppedList[4].value)
    elements.append(poppedList[2].value)
    elements.append(poppedList[0].value)
    return InputToken(head, tuple(elements))
def semantic47(head, poppedList):
    sumand1 = poppedList[2].value
    sumand2 = poppedList[0].value
    return InputToken(head, sumand1 + sumand2)
def semantic45(head, poppedList):
    operator = poppedList[1].lexeme
    number = poppedList[2].value
    shift = poppedList[0].value
    return InputToken(head, op[operator](number, shift))
def semantic11(head, poppedList):
    identifier = poppedList[2].value
    value = poppedList[0].value
    symbolsTable[identifier] = value
    return InputToken(head, symbolsTable[identifier])
def semantic48(head, poppedList):
    minuend = poppedList[2].value
    substraend = poppedList[0].value
    return InputToken(head, minuend - substraend)
def semantic50(head, poppedList):
    operator = poppedList[1].lexeme
    factor1 = poppedList[2].value
    factor2 = poppedList[0].value
    return InputToken(head, op[operator](factor1, factor2))
def semantic54(head, poppedList):
    operator = poppedList[1].lexeme
    base = poppedList[2].value
    exponent = poppedList[0].value
    return InputToken(head, op[operator](base, exponent))
def semantic52(head, poppedList):
    operator = poppedList[1].lexeme
    dividend = poppedList[2].value
    divisor = poppedList[0].value
    return InputToken(head, op[operator](dividend, divisor))
def semantic9(head, poppedList):
    identifier = symbolsTable[poppedList[2].value]
    operator = poppedList[1].value
    value = poppedList[0].value
    symbolsTable[identifier] = op[operator](identifier, value)
    return InputToken(head, symbolsTable[identifier])
def semantic30(head, poppedList):
    operator = poppedList[
        1].value  # Lo is the lexeme, and the operand is stored in value
    operand1 = poppedList[2].value
    operand2 = poppedList[0].value
    return InputToken(head, op[operator](operand1, operand2))
def semantic24(head, poppedList):
    operand1 = poppedList[0].value
    operand2 = poppedList[2].value
    return InputToken(head, operand1 or operand2)
def semantic42(head, poppedList):
    operator = poppedList[1].lexeme
    operand1 = poppedList[2].value
    operand2 = poppedList[0].value
    return InputToken(head, op[operator](operand1, operand2))
def semantic70(head, poppedList):
    return InputToken(head, [])
def semantic28(head, poppedList):
    operator = poppedList[1].lexeme
    operand = poppedList[0].value
    return InputToken(head, op[operator](operand))