-
Notifications
You must be signed in to change notification settings - Fork 6
/
intermediatecodegenerator.py
158 lines (157 loc) · 6.64 KB
/
intermediatecodegenerator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
print("This is program of THREE ADDRESS CODE GENERATOR using Pyhton.\n\f MADE BY:- ABHISHEK MISHRA")
import pandas as pd
import copy
try:
a=pd.read_csv("input.csv")
print("\a One thing in this program is that it takes an input of csv file.\n\aThe formate of the csv file is in following manner: ")
print(a)
print("\a The output of this program is based on above csv file.\n\aYou can take the different input csv file for diffrent required output.")
print("\a One thing is to be noticed that in the csv file, there are two columns one is left and other is right for left and right values respectively.")
print("\a There is only one left side variable for each equation and it may be possible that more than one varible in right side.")
print("\a The operators and operands which are used in right side must be space separated from each other.")
print("\a This program is case sensitive, this means that 'd*10' and '10*d' are treated as different equation.\nIf you want to solve this problem then you can use the 'CODE OPTIMIZATION TECHNIQUE'.")
print('\t')
c=a.shape# It will gives an tuple of numbers of rows and columns
#print(c)
l=[]
o=list("+-*/")#If you want to add more operator youn can use that as well
o1=[]
r=[]
for i in range(c[0]):# Here c[0] is 0th element of tuple c, which is a.shape (c=a.shape)
l=l+[a['left'][i]]
d=a['right'][i]
x=d.split()
l=l+x
#print(l)
sizel=len(l)
for z in range(sizel):
#print(sizel)
if(l[z] in o):
o1=o1+[l[z]]
o1=list(set(o1))
#print(o1)
li=copy.deepcopy(l)# if you use li=l then it may occures some un usual error further in program.
for x in o1:
if(x in li):
li.remove(x)
li=list(set(li))
#print(li)
for b in range(len(li)):
r=r+["R"+str(b)]
#print(r)
i=1
ak=0
z=0
ACounter=0
akm=[]
while(i):
if(ak==len(l)):
i=0
elif(l[ak].isalpha() and l[ak]==a['left'][z]):
print("MOV "+str(l[ak])+' , '+str(r[li.index(l[ak])]))
akm=akm+[r[li.index(l[ak])]]
ak+=1
elif(((l[ak].isalpha()) and (l[ak] in a['right'][z]))and (l[ak] not in o1)):
print("MOV "+str(l[ak])+' , '+str(r[li.index(l[ak])]))
akm=akm+[r[li.index(l[ak])]]
ak+=1
ACounter+=1
if((len(a['right'][z])==1)and (len(akm)==2)):
print("STOR "+str(akm[len(akm)-1])+' , '+str(akm[0]))
#print(akm)
akm.clear()
z+=1
print("\t")
elif((l[ak] in a['right'][z]) and ((l[ak]in o1)and l[ak]=="+")):
print("MOV "+str(l[ak+1])+' , '+str(r[li.index(l[ak+1])]))
akm=akm+[r[li.index(l[ak+1])]]
print("ADD "+str(akm[len(akm)-2])+' , '+str(akm[len(akm)-1]))
akm.pop(len(akm)-2)
#print(akm)
print("STOR "+str(akm[len(akm)-1])+' , '+str(akm[0]))
#print(ak)
#print(ACounter)
ak+=2
ACounter+=2
#print(ACounter)
if(len(a['right'][z].split(" "))==ACounter):
#print(akm)
akm.clear()
z+=1
ACounter=0
#print(z)
print("\t")
elif((l[ak] in a['right'][z]) and ((l[ak]in o1)and l[ak]=="-")):
print("MOV "+str(l[ak+1])+' , '+str(r[li.index(l[ak+1])]))
akm=akm+[r[li.index(l[ak+1])]]
print("SUB "+str(akm[len(akm)-2])+' , '+str(akm[len(akm)-1]))
akm.pop(len(akm)-2)
print("STOR "+str(akm[len(akm)-1])+' , '+str(akm[0]))
ak+=2
ACounter+=2
#print(ACounter)
if(len(a['right'][z].split(" "))==ACounter):
#print(akm)
akm.clear()
z+=1
ACounter=0
#print(z)
print("\t")
elif((l[ak] in a['right'][z]) and ((l[ak]in o1)and l[ak]=="*")):
print("MOV "+str(l[ak+1])+' , '+str(r[li.index(l[ak+1])]))
akm=akm+[r[li.index(l[ak+1])]]
print("MUL "+str(akm[len(akm)-2])+' , '+str(akm[len(akm)-1]))
akm.pop(len(akm)-2)
print("STOR "+str(akm[len(akm)-1])+' , '+str(akm[0]))
ak+=2
ACounter+=2
#print(ACounter)
if(len(a['right'][z].split(" "))==ACounter):
#print(akm)
akm.clear()
z+=1
ACounter=0
#print(z)
print("\t")
elif((l[ak] in a['right'][z]) and ((l[ak]in o1)and l[ak]=="/")):
print("MOV "+str(l[ak+1])+' , '+str(r[li.index(l[ak+1])]))
akm=akm+[r[li.index(l[ak+1])]]
print("DIV "+str(akm[len(akm)-2])+' , '+str(akm[len(akm)-1]))
akm.pop(len(akm)-2)
print("STOR "+str(akm[len(akm)-1])+' , '+str(akm[0]))
akm.clear()
ak+=2
ACounter+=2
if(len(a['right'][z].split(" "))==ACounter):
#print(akm)
akm.clear()
z+=1
ACounter=0
#print(z)
print("\t")
elif((l[ak].isnumeric())and(l[ak] in a['right'][z])):
print("MOV "+str(l[ak])+' , '+str(r[li.index(l[ak])]))
akm=akm+[r[li.index(l[ak])]]
ak+=1
ACounter+=1
if((len(akm)==2)and (a['right'][z]==l[ak-1])):
print("STOR "+str(akm[len(akm)-1])+' , '+str(akm[0]))
akm.clear()
z+=1
ACounter=0
#print(z)
print("\t")
elif((l[ak] not in o1)or (l[ak] not in string.ascii_lowercase)):
print("\f Error!\n\f Please enter valid syntax for three address code.\n\f Check your csv file...")
print(f"\f Error description...\nError in line number {z} and place number {ak}.")
print(f"\f Error element is {a['right'][z]}.")
break
except (FileNotFoundError):
print("Please check you input file. It may possible that file doesn't exist.")
print("Also check the file name that is given in input section at the starting place.")
except(ArithmeticError):
print("An arithmetic error is caused due to which program is not proceed futher.Please check for the solution.")
except(IndexError):
print("List index out of range.")
except:
print("An exceptions occurred.")