-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathps9_3.py
150 lines (115 loc) · 2.91 KB
/
ps9_3.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
from prettytable import PrettyTable
exp = input("Enter arithmetic expression with appropriate brackets: ")
print("\nARITHMETIC EXPRESSION: ", exp)
operators=['*','+','-','/']
asg = exp[0]
stack = []
#print("ASG: ", asg)
#print("EXP: ", exp)
int_code = {}
count = 0
for i in range(len(exp)):
if exp[i] == ')':
s = ''
while stack[-1] != '(':
s = stack.pop()+s
stack.pop() # Remove the '('
count += 1
temp_var = 'T' + str(count)
int_code[temp_var] = s
stack.append(temp_var)
else:
stack.append(exp[i])
int_code[asg]='T'+str(count)
print("\n\nFinal Three Address Code:\n")
for key, value in int_code.items():
print(key, '=', value)
op=[]
arg1=[]
arg2=[]
res=[]
print("\n\nQUADRAPLES\n")
for k,v in int_code.items():
if v[0]=='-':
op.append(v[0])
arg1.append(v[1:])
arg2.append('-')
res.append(k)
else:
flag=0
for i in operators:
if i in v:
flag=1
ind=v.index(i)
op.append(v[ind])
arg1.append(v[0:ind])
arg2.append(v[ind+1:])
res.append(k)
break
if flag==0:
op.append('=')
arg1.append(v)
arg2.append('-')
res.append(k)
table=PrettyTable(['OPERATOR','ARG1','ARG2','RESULT'])
for i in range(len(op)):
table.add_row([''.join(op[i]),''.join(arg1[i]),''.join(arg2[i]),''.join(res[i])])
print(table)
#A=((A+(B*C))-D)
#A=((-B)+(C*D))
# Three address code - TRIPLES
from prettytable import PrettyTable
exp = input("Enter arithmetic expression with appropriate brackets: ")
print("\nARITHMETIC EXPRESSION: ", exp)
operators=['*','+','-','/']
asg = exp[0]
stack = []
#print("ASG: ", asg)
#print("EXP: ", exp)
int_code = {}
count = 0
for i in range(len(exp)):
if exp[i] == ')':
s = ''
while stack[-1] != '(':
s = stack.pop()+s
stack.pop() # To Remove '('
count += 1
temp_var ='('+ str(count)+')'
int_code[temp_var] = s
stack.append(temp_var)
else:
stack.append(exp[i])
int_code[asg]='('+ str(count)+')'
print("\n\nFinal Three Address Code:\n")
for key, value in int_code.items():
print(key, '=', value)
op=[]
arg1=[]
arg2=[]
print("\nTRIPLES\n")
for k,v in int_code.items():
if v[0]=='-':
op.append(v[0])
arg1.append(v[1:])
arg2.append('-')
else:
flag=0
for i in operators:
if i in v:
flag=1
ind=v.index(i)
op.append(v[ind])
arg1.append(v[0:ind])
arg2.append(v[ind+1:])
break
if flag==0:
op.append('=')
arg1.append(k)
arg2.append(v)
table=PrettyTable(['OPERATOR','ARG1','ARG2'])
for i in range(len(op)):
table.add_row([''.join(op[i]),''.join(arg1[i]),''.join(arg2[i])])
print(table)
#A=((A+(B*C))-D)
#A=((-B)+(C*D))