-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathps10_3.py
148 lines (113 loc) · 3.51 KB
/
ps10_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
//program to associate three address code of Boolean expressions (involving relational
//operators, “and”, ”or” and not) with each node in the parse tree.
from collections import defaultdict
binary_operator=["+","-","/","*","and","or",">","<","==","<=",">=","!="]
unary_operator=["not"]
precen={"or":1,"and":2,"==":3,"!=":3,">":4,"<":4,">=":4,"<=":4,"+":5,"-":5,"/":6,"*":6,"not":7}
class Node:
def __init__(self,value=None,right=None,left=None,codeid=None,code=None,codelabel=None,next=None):
self.value=value
self.right=right
self.left=left
self.codeid=codeid
self.code=code
self.codelabel=codelabel
self.next=next
class Stack:
def __init__(self):
self.top=None
def push(self,node):
if not self.top:
self.top=node
else:
node.next=self.top
self.top=node
def pop(self):
pop_node=self.top
self.top=self.top.next
return pop_node
class Syntax_tree:
def inorder(self,root):
if not root:
return
self.inorder(root.left)
if root.value in binary_operator or root.value in unary_operator:
print(root.value,end="\t")
print(root.code,"\n")
else:
print(root.value,"\n")
self.inorder(root.right)
def postorder(self,root):
if not root:
return
self.postorder(root.left)
self.postorder(root.right)
if root.value in binary_operator or root.value in unary_operator:
print(root.value,end="\t")
print(root.code,"\n")
else:
print(root.value,"\n")
def to_postfix(expression):
stack=[]
postfix=[]
for i in expression:
if i=="(":
stack.append(i)
elif i in binary_operator or i in unary_operator:
while len(stack) and stack[-1] != "(" and stack[-1] != ")" and precen[stack[-1]]>=precen[i]:
postfix.append(stack.pop())
stack.append(i)
elif i.isalpha():
postfix.append(i)
elif i==")":
while len(stack):
if stack[-1]=="(":
stack.pop()
break
postfix.append(stack.pop())
return postfix
def calculate_syntax(expression):
syntax_tree=Syntax_tree()
stack_list=Stack()
id=-1
for i in expression:
if i in binary_operator:
new_node=Node()
op2=stack_list.pop()
op1=stack_list.pop()
new_node.value=i
new_node.left=op1
new_node.right=op2
new_node.codeid=id+1
new_node.codelabel="T"+str(new_node.codeid)
new_node.code=new_node.codelabel+" = "+op1.codelabel+" "+i+" "+op2.codelabel
stack_list.push(new_node)
id+=1
elif i in unary_operator:
new_node=Node()
op=stack_list.pop()
new_node.value=i
new_node.left=op
new_node.codeid=id+1
new_node.codelabel="T"+str(new_node.codeid)
new_node.code=new_node.codelabel+" = "+i+" "+op.codelabel
stack_list.push(new_node)
id+=1
else:
new_node=Node()
new_node.value=i
new_node.codeid=-1
new_node.codelabel=i
new_node.code=""
stack_list.push(new_node)
root = stack_list.pop()
print("postorder traversal")
syntax_tree.postorder(root)
input_string="( x < y and y >= z or x == z and not w )"
input_list=input_string.split()
print("Given string")
print(input_string)
postfix=to_postfix(input_list)
print("Postfix")
print(postfix)
calculate_syntax(postfix)