TypeError: Cannot convert node 'object to str implicitly

For the following code -

Reg_Stack = ['R5', 'R4', 'R3', 'R2', 'R1', 'R0']
Temp_Stack = ['T5', 'T4', 'T3', 'T2', 'T1', 'T0']
operator_precedence = {'(' : 0, ')' : 0, '+' : 1, '-' : 1, '*' : 2, '/' : 2}

def gen_code(n):
    if(n.left == None and n.right == None):
        print("MOV " + n.value + "," + Reg_Stack[-1])
    else:
        if(n.right.label == 0):
            gen_code(n.left)
            print(operator(n.value) + " " + n.right.value + "," + Reg_Stack[-1])

        elif((n.left.label < n.right.label) and (n.left.label < len(Reg_Stack))):
            swap()
            gen_code(n.left)
            R = Reg_Stack.pop()
            gen_code(n.left)
            print(operator(n.value) + " " + n.left.value + "," + Reg_Stack[-1])
            Reg_Stack.append(R)
            swap()

        elif((n.right.label < n.left.label) and (n.right.label < len(Reg_Stack))):
            gen_code(n.left)
            R = Reg_Stack.pop()
            gen_code(n.right)
            print(operator(n.value) + " " + n.right.value + "," + Reg_Stack[-1])
            Reg_Stack.append(R)

        else:
            gen_code(n.right)
            T = Temp_Stack.pop()
            print("MOV " + Reg_Stack[-1] + "," + T)
            gen_code(n.left)
            Temp_Stack.append(T)
            print(operator(n.value) + " " + T + "," + Reg_Stack[-1])

def operator(v):
    if(v == "+"):
        return "ADD"
    if(v == "-"):
        return "SUB"
    if(v == "*"):
        return "MUL"

def swap():
    a1 = Reg_Stack.pop()
    b1 = Temp_Stack.pop()
    Reg_Stack.append(b)
    Temp_Stack.append(a)

class node(object):
    def __init__(self, value='', lvalue=0, node1 = None, node2 = None):
        self.value = value
        self.left = node1
        self.right = node2
        self.label = lvalue


a = node('a', 1)
b = node('b', 1)
c = node('c', 1)
d = node('d', 0)

q1 = node('*', 1, c, d)
q2 = node('-', 2, b, q1)
root = node('+', 0, a, q2)
gen_code(root)

I get an error -

C:\Python33>python main.py
Traceback (most recent call last):
  File "main.py", line 67, in <module>
    gen_code(root)
  File "main.py", line 15, in gen_code
    gen_code(n.left)
  File "main.py", line 7, in gen_code
    print("MOV " + n.value + "," + Reg_Stack[-1])
TypeError: Can't convert 'node' object to str implicitly

Function input gen_code()is a marked tree. I tried with different trees and the code works. But only for this case I received the above error. What am I doing wrong?

+4
source share
1 answer

The problem is mainly due to your function swap()-

def swap():
    a1 = Reg_Stack.pop()
    b1 = Temp_Stack.pop()
    Reg_Stack.append(b)
    Temp_Stack.append(a)

You remove stringsfrom Reg_Stackand Temp_Stackthen add to it node. So after that, when you do this -

Reg_Stack[-1]

node, swap(). , , () str.

node, node. -

def swap():
    a1 = Reg_Stack.pop()
    b1 = Temp_Stack.pop()
    Reg_Stack.append(b.value)
    Temp_Stack.append(a.value)

-

MOV a,b
MOV a,R1
ADD a,R1

, .

+3

Source: https://habr.com/ru/post/1606751/


All Articles