0% found this document useful (0 votes)
2 views3 pages

SPCCEXP7

The code takes a number of expressions as input and stores them in a list of dictionaries. It performs three optimization steps: Dead Code Elimination, Common Subexpression Elimination, and Duplicate Removal, ultimately printing the optimized code. The final output retains only necessary expressions and eliminates redundancies.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

SPCCEXP7

The code takes a number of expressions as input and stores them in a list of dictionaries. It performs three optimization steps: Dead Code Elimination, Common Subexpression Elimination, and Duplicate Removal, ultimately printing the optimized code. The final output retains only necessary expressions and eliminates redundancies.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

CODE:

n = int(input("Enter the number of expressions: "))

# Store intermediate code as a list of dictionaries


expressions = []
for i in range(n):
left = input("Left: ").strip()
right = input("Right: ").strip()
expressions.append({"left": left, "right": right})

# Print Intermediate Code


print("\nIntermediate Code:")
for expr in expressions:
print(f"{expr['left']} = {expr['right']}")

# **Step 1: Dead Code Elimination**


optimized_exprs = []
used_vars = set()

# Identify variables that are used in right-hand expressions


for expr in expressions:
for other_expr in expressions:
if expr["left"] in other_expr["right"]:
used_vars.add(expr["left"])

# Keep only necessary expressions (last expression is always kept)


for expr in expressions:
if expr["left"] in used_vars or expr == expressions[-1]:
optimized_exprs.append(expr)

# Print after Dead Code Elimination


print("\nAfter Dead Code Elimination:")
for expr in optimized_exprs:
print(f"{expr['left']} = {expr['right']}")

# **Step 2: Common Subexpression Elimination**


i=0
while i < len(optimized_exprs):
j=i+1
while j < len(optimized_exprs):
if optimized_exprs[j]["right"] in optimized_exprs[i]["right"]:
old_var = optimized_exprs[j]["left"]
optimized_exprs[j]["left"] = optimized_exprs[i]["left"]
k=0
while k < len(optimized_exprs):
optimized_exprs[k]["right"] = optimized_exprs[k]["right"].replace(old_var,
optimized_exprs[i]["left"])
k += 1
j += 1
i += 1

# Print after Eliminating Common Expressions


print("\nAfter Eliminating Common Expressions:")
for expr in optimized_exprs:
print(f"{expr['left']} = {expr['right']}")

# **Step 3: Final Optimization (Duplicate Removal)**


final_exprs = []
seen = set()
i=0

while i < len(optimized_exprs):


if (optimized_exprs[i]["left"], optimized_exprs[i]["right"]) not in seen:
seen.add((optimized_exprs[i]["left"], optimized_exprs[i]["right"]))
final_exprs.append(optimized_exprs[i])
i += 1

# Print Optimized Code


print("\nOptimized Code:")
i=0
while i < len(final_exprs):
print(f"{final_exprs[i]['left']} = {final_exprs[i]['right']}")
i += 1

OUTPUT:

C:\Users\Asus\PycharmProjects\TimeTabler\.venv\Scripts\python.exe C:\Users\Asus\
PycharmProjects\TimeTabler\main.py

Enter the number of expressions: 3

Left: t1

Right: a+b

Left: t2

Right: a+b

Left: t3

Right: t2 * 4

Intermediate Code:

t1 = a+b

t2 = a+b
t3 = t2 * 4

After Dead Code Elimination:

t2 = a+b

t3 = t2 * 4

After Eliminating Common Expressions:

t2 = a+b

t3 = t2 * 4

Optimized Code:

t2 = a+b

t3 = t2 * 4

Process finished with exit code 0

You might also like