A3 47 Practical5
A3 47 Practical5
PRACTICAL No. 5
Topic: Three Address Code Generation
Aim: Write a program to generate three address code for the given language construct
using SDTS.
Input
Code
class ThreeAddressCodeGenerator:
def __init__(self):
self.line_number = 1
self.temp_var_count = 1
self.code = []
def get_next_temp(self):
temp = f"T{self.temp_var_count}"
self.temp_var_count += 1
return temp
# If we reach here, the left condition was true, now evaluate the right condition
self.parse_condition(right, true_label, false_label)
return
# If we reach here, the left condition was false, now evaluate the right condition
self.parse_condition(right, true_label, false_label)
return
if '<' in condition:
left, right = condition.split('<', 1)
self.emit(f"if {left.strip()} < {right.strip()} goto {true_label}")
self.emit(f"goto {false_label}")
elif '>' in condition:
left, right = condition.split('>', 1)
self.emit(f"if {left.strip()} > {right.strip()} goto {true_label}")
self.emit(f"goto {false_label}")
elif '<=' in condition:
left, right = condition.split('<=', 1)
self.emit(f"if {left.strip()} <= {right.strip()} goto {true_label}")
self.emit(f"goto {false_label}")
elif '>=' in condition:
left, right = condition.split('>=', 1)
self.emit(f"if {left.strip()} >= {right.strip()} goto {true_label}")
self.emit(f"goto {false_label}")
elif '==' in condition:
left, right = condition.split('==', 1)
self.emit(f"if {left.strip()} == {right.strip()} goto {true_label}")
self.emit(f"goto {false_label}")
elif '!=' in condition:
left, right = condition.split('!=', 1)
self.emit(f"if {left.strip()} != {right.strip()} goto {true_label}")
self.emit(f"goto {false_label}")
else:
# For conditions like "if x" (boolean variable)
self.emit(f"if {condition} goto {true_label}")
self.emit(f"goto {false_label}")
count = 0
for i in range(len(expr)):
if expr[i] == '(':
count += 1
elif expr[i] == ')':
count -= 1
if count == 0 and i < len(expr) - 1:
return False # Found closing parenthesis before the end
return count == 0
return {
'condition_start': self.line_number,
'body_start': body_start,
'body_end': body_end,
'after_loop': after_loop
}
labels = self.calculate_actual_labels(body_statements)
self.emit(f"goto {condition_label}")
# Example usage
def generate_three_address_code(while_loop):
# Extract condition and body from the while loop
start_cond = while_loop.find('(') + 1
end_cond = while_loop.find(')')
condition = while_loop[start_cond:end_cond]
start_body = while_loop.find('{') + 1
end_body = while_loop.rfind('}')
body = while_loop[start_body:end_body].strip()
Output:
Example 1: Simple condition
Input:
while (i < 10) {
sum = sum + i;
i=i+1
}
Output:
1) if i < 10 goto 3
2) goto 8
3) T1=sum+i
4) sum=T1
5) T2=i+1
6) i=T2
7) goto 1
8) END
Output:
1) if i < 10 goto 3
2) goto 8
3) if sum < 100 goto 3
4) goto 8
5) T1=sum+i
6) sum=T1
7) T2=i+1
8) i=T2
9) goto 1
10) END
Example 3: OR condition
Input:
Output:
1) if i < 10 goto 3
2) goto 3
3) if sum < 50 goto 3
4) goto 8
5) T1=sum+i
6) sum=T1
7) T2=i+1
8) i=T2
9) goto 1
10) END