Exemplo n.º 1
0
def loop(p):
    p += 1
    endPosition = None
    condition = []
    while listaDeTokens[p][0] != 'do':
        condition.append(listaDeTokens[p][0])
        p += 1
    conditionValue = calc(condition)
    p += 1
    i = p

    while conditionValue == "True":
        while listaDeTokens[i][0] != 'end':
            i = check(i) + 1
            conditionValue = calc(condition)

        endPosition = i
        i = p

    # caso o while inicie com condição falsa
    if endPosition == None:
        pilha_bloco = Stack()
        while listaDeTokens[p][0] != 'end' or not pilha_bloco.isEmpty():
            if listaDeTokens[p][0] == 'if' or listaDeTokens[p][
                    0] == "to" or listaDeTokens[p][0] == "while":
                pilha_bloco.push(listaDeTokens[p][0])
            elif listaDeTokens[p][0] == 'end' or listaDeTokens[p][0] == "fi":
                pilha_bloco.pop()

            p += 1
        endPosition = p

    return endPosition
Exemplo n.º 2
0
def infixToPostfix(tokenList):
    prec = {}
    prec["*"] = 3
    prec["/"] = 3
    prec["+"] = 2
    prec["-"] = 2
    prec["("] = 1
    prec[">"] = 0
    prec["<"] = 0
    prec["="] = 0
    prec[">="] = 0
    prec["<="] = 0

    opStack = Stack()
    postfixList = []
    for token in tokenList:
        if token.isnumeric() or token not in [
                '+', '-', '*', '/', '(', ')', '>', '<', '=', '<=', '>='
        ]:
            if not token.isnumeric():
                token = tabela_sintatica.tabela[token]['value']
                if not token.isnumeric():
                    print("erro! variável sem valor atribuído." + token)
                    exit()
            postfixList.append(token)
        elif token == '(':
            opStack.push(token)
        elif token == ')':
            topToken = opStack.pop()
            while topToken != '(':
                postfixList.append(topToken)
                topToken = opStack.pop()
        else:
            while (not opStack.isEmpty()) and \
                    (prec[opStack.peek()] >= prec[token]):
                postfixList.append(opStack.pop())
            opStack.push(token)

    while not opStack.isEmpty():
        postfixList.append(opStack.pop())
    return postfixList
Exemplo n.º 3
0
def condicional(p):
    p += 1
    condition = []
    while listaDeTokens[p][0] != 'then':
        condition.append(listaDeTokens[p][0])
        p += 1
    conditionValue = calc(condition)
    pilha_bloco = Stack()
    if conditionValue == "False":
        while (listaDeTokens[p][0] != 'else'
               and listaDeTokens[p][0] != 'fi') or not pilha_bloco.isEmpty():
            if listaDeTokens[p][0] == 'if' or listaDeTokens[p][
                    0] == "to" or listaDeTokens[p][0] == "while":
                pilha_bloco.push(listaDeTokens[p][0])
            elif listaDeTokens[p][0] == 'end' or listaDeTokens[p][0] == "fi":
                pilha_bloco.pop()

            p += 1

        if listaDeTokens[p][0] == 'fi':
            return p
        p += 1
    while listaDeTokens[p][0] != 'else' and listaDeTokens[p][0] != 'fi':
        p = check(p) + 1

    if listaDeTokens[p][0] != "fi":
        pilha_bloco = Stack()
        pilha_bloco.push("if")
        while not pilha_bloco.isEmpty():
            if listaDeTokens[p][0] == 'if' or listaDeTokens[p][
                    0] == "to" or listaDeTokens[p][0] == "while":
                pilha_bloco.push(listaDeTokens[p][0])
            elif listaDeTokens[p][0] == 'end' or listaDeTokens[p][0] == "fi":
                pilha_bloco.pop()

            p += 1
        p = p - 1
    return p