fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. // Function to generate 3-address code from postfix expression
  5. void generateTAC(string expr) {
  6. stack<string> st;
  7. int temp = 1;
  8.  
  9. for (char c : expr) {
  10. if (isalnum(c)) {
  11. st.push(string(1, c)); // Operand
  12. } else { // Operator
  13. string op2 = st.top(); st.pop();
  14. string op1 = st.top(); st.pop();
  15. string t = "t" + to_string(temp++);
  16. cout << t << " = " << op1 << " " << c << " " << op2 << endl;
  17. st.push(t);
  18. }
  19. }
  20.  
  21. // Final result
  22. if (!st.empty()) {
  23. cout << "Result = " << st.top() << endl;
  24. }
  25. }
  26.  
  27. int main() {
  28. string expr;
  29. cout << "Enter postfix expression: ";
  30. cin >> expr;
  31.  
  32. cout << "\nThree-Address Code:\n";
  33. generateTAC(expr);
  34.  
  35. return 0;
  36. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Enter postfix expression: 
Three-Address Code: