示例#1
0
def eval(expression, k):
    exp = InfixTopostfix.menu(expression)
    #exp = postfix.menu(expression)
    #print exp
    count = 0
    for i in exp:
        #print(i)
        if (InfixTopostfix.isOperator(i)):
            #if(postfix.isOperator(i)):
            count = count + 1
    temp = []  # temporary variable -> contains
    VarCount = 0  # to count the variables, whether it is ready to be operated on (i.e, 2 vars )
    opstack = []  # stack for operator
    varstack = []  # stack for operand
    t = ""
    exp = str(exp)
    post = re.findall('\w+|\+|-|\*|/|=', exp)
    #print post
    l = []
    for x in post:
        if (InfixTopostfix.isOperand(x)):
            l.append(x)

    f = open("symbol_original.txt", "a")
    for i in l:
        if not i.isdigit():
            f.write(i)
            f.write('\n')
    f.close()

    store_i = k
    temp_list = []
    temps = ''
    for i in post:
        if InfixTopostfix.isOperator(i):
            #if postfix.isOperator(i):
            x = varstack.pop()
            y = varstack.pop()
            t = y + i + x
            temp.append(t)
            if (i != '='):
                temps = "t" + str(k)
                #print i+"harekal"+temps
                temp_list.append(temps)
                varstack.append(temps)
                k = k + 1

        else:
            varstack.append(i)

    f = open("symbol_temp.txt", "a")
    for i in temp_list:
        f.write(i)
        f.write('\n')
    f.close()

    i = store_i
    t = []
    for l in temp:
        if i < store_i + count - 1:
            #print l
            t.append("t" + str(i) + "=" + str(l))
        else:
            t.append(str(l))
        i = i + 1

    f = open("inout.txt", "a")
    for i in t:
        print i
        f.write(i)
        f.write('\n')
    f.close()
    return k
示例#2
0
def eval(expression, k, tostr):
    exp = InfixTopostfix.menu(expression)
    # exp = postfix.menu(expression)
    # print exp
    count = 0
    for i in exp:
        # print(i)
        if InfixTopostfix.isOperator(i):
            # if(postfix.isOperator(i)):
            count = count + 1
    temp = []  # temporary variable -> contains
    VarCount = 0  # to count the variables, whether it is ready to be operated on (i.e, 2 vars )
    opstack = []  # stack for operator
    varstack = []  # stack for operand
    t = ""
    exp = str(exp)
    post = re.findall("\w+|\+|-|\*|/|=", exp)
    # print post
    l = []
    for x in post:
        if InfixTopostfix.isOperand(x):
            l.append(x)

    f = open("symbol_original.txt", "a")
    for i in l:
        if not i.isdigit():
            f.write(i)
            f.write("\n")
    f.close()

    store_i = k
    temp_list = []
    temps = ""
    # print post
    t = []
    for i in post:
        if InfixTopostfix.isOperator(i):
            if i != "=":
                # if postfix.isOperator(i):
                x = varstack.pop()
                y = varstack.pop()
                t1 = y + i + x
                temp.append(t1)
                temps = "t" + str(k)
                # print i+"harekal"+temps
                temp_list.append(temps)
                varstack.append(temps)
                k = k + 1
            elif count == 1:
                x = varstack.pop()
                y = varstack.pop()
                print var, "*" * 80
                tostr.append(var)
                t.append(str(x) + "=" + str(y))
        else:
            varstack.append(i)
    f = open("symbol_temp.txt", "a")
    inv = 0
    for i in temp_list:
        if inv < len(temp_list) - 1:
            f.write(i)
            f.write("\n")
        inv = inv + 1

    f.close()

    i = store_i
    # print temp, i, 'hellohello'
    for l in temp:
        if i < store_i + count - 2:
            t.append("t" + str(i) + "=" + str(l))
        else:
            # print varstack
            var = varstack.pop()
            var = varstack.pop()
            # print var
            print var, "*" * 80
            tostr.append(var)
            t.append(str(var) + "=" + str(l))
        i = i + 1

    f = open("inout.txt", "a")
    for i in t:
        print i
        f.write(i)
        f.write("\n")
    f.close()

    if k == 0:
        return 0
    return k - 1
示例#3
0
def eval(expression, k):
	exp = InfixTopostfix.menu(expression)
	#exp = postfix.menu(expression)
	#print exp
	count=0
	for i in exp:
		#print(i)
		if(InfixTopostfix.isOperator(i)):
      	 	#if(postfix.isOperator(i)):
		 	count=count+1
	temp = [] # temporary variable -> contains 
	VarCount=0 # to count the variables, whether it is ready to be operated on (i.e, 2 vars )
	opstack=[] # stack for operator
	varstack=[] # stack for operand
	t=""
	exp = str(exp)
	post = re.findall('\w+|\+|-|\*|/|=', exp)
	#print post
        l=[]
        for x in post:
                if(InfixTopostfix.isOperand(x)):
                        l.append(x)

        f = open("symbol_original.txt", "a")
        for i in l:
		if not i.isdigit():
                	f.write(i)
                	f.write('\n')
        f.close()

	store_i = k
	temp_list=[]
	temps = ''
	for i in post:
		if InfixTopostfix.isOperator(i):
		#if postfix.isOperator(i):
			x = varstack.pop()
			y = varstack.pop()
			t=y+i+x
			temp.append(t)
			if ( i!='=' ):
				temps = "t"+str(k)
				#print i+"harekal"+temps
				temp_list.append(temps)
				varstack.append(temps)
				k=k+1
			
		else:
			varstack.append(i)

	f = open("symbol_temp.txt", "a")
        for i in temp_list:
     		f.write(i)
        	f.write('\n')
    	f.close()

	i = store_i
	t = []
	for l in temp:
		if i<store_i+count-1:
			#print l
			t.append("t"+str(i)+"="+str(l))
		else:
			t.append(str(l))
		i=i+1
	

	f = open("inout.txt", "a")
	for i in t:
		print i
		f.write(i)
		f.write('\n')
	f.close()
	return k
示例#4
0
i = 0
m=0
for j in quad:
	#if(VarCount==2):
	#	t=str(varstack.pop())+str(opstack.pop())+str(varstack.pop())
	#	temp.append(t)
	#	varstack.append(temp[k])
	#	k=k+1
	#	VarCount=1
	
	if(InfixTopostfix.isOperator(j)):
		#print "OPERATOR INSIDE"
		opstack.append(j)
	
	elif ( VarCount !=2 and InfixTopostfix.isOperand(j)):
		#print "INSIDE"
		varstack.append(j)
		VarCount=VarCount+1

	if ( VarCount == 2 ):
		#print varstack
		t=str(varstack.pop())+str(opstack.pop())+str(varstack.pop())
		temp.append(t)
		varstack.append("t["+str(k)+"]")
		#varstack.pop()
                k=k+1
		VarCount=1
i = 0
t = []
for l in temp:
示例#5
0
def eval(expression, k, tostr):
    exp = InfixTopostfix.menu(expression)
    #exp = postfix.menu(expression)
    #print exp
    count = 0
    for i in exp:
        #print(i)
        if (InfixTopostfix.isOperator(i)):
            #if(postfix.isOperator(i)):
            count = count + 1
    temp = []  # temporary variable -> contains
    VarCount = 0  # to count the variables, whether it is ready to be operated on (i.e, 2 vars )
    opstack = []  # stack for operator
    varstack = []  # stack for operand
    t = ""
    exp = str(exp)
    post = re.findall('\w+|\+|-|\*|/|=', exp)
    #print post
    l = []
    for x in post:
        if (InfixTopostfix.isOperand(x)):
            l.append(x)

    f = open("symbol_original.txt", "a")
    for i in l:
        if not i.isdigit():
            f.write(i)
            f.write('\n')
    f.close()

    store_i = k
    temp_list = []
    temps = ''
    #print post
    t = []
    for i in post:
        if InfixTopostfix.isOperator(i):
            if i != '=':
                #if postfix.isOperator(i):
                x = varstack.pop()
                y = varstack.pop()
                t1 = y + i + x
                temp.append(t1)
                temps = "t" + str(k)
                #print i+"harekal"+temps
                temp_list.append(temps)
                varstack.append(temps)
                k = k + 1
            elif count == 1:
                x = varstack.pop()
                y = varstack.pop()
                print var, '*' * 80
                tostr.append(var)
                t.append(str(x) + "=" + str(y))
        else:
            varstack.append(i)
    f = open("symbol_temp.txt", "a")
    inv = 0
    for i in temp_list:
        if inv < len(temp_list) - 1:
            f.write(i)
            f.write('\n')
        inv = inv + 1

    f.close()

    i = store_i
    #print temp, i, 'hellohello'
    for l in temp:
        if i < store_i + count - 2:
            t.append("t" + str(i) + "=" + str(l))
        else:
            #print varstack
            var = varstack.pop()
            var = varstack.pop()
            #print var
            print var, '*' * 80
            tostr.append(var)
            t.append(str(var) + "=" + str(l))
        i = i + 1

    f = open("inout.txt", "a")
    for i in t:
        print i
        f.write(i)
        f.write('\n')
    f.close()

    if k == 0:
        return 0
    return k - 1