0

I need help in writing a python code that takes in the Three Address Code for the Java source code and returns pseudo code. I have successfully generated pseudo code for 1-D array initialization by extracting the array names, their length, and values.

For example: For the below lines of code:

int arr1[] = {1,2,3}

int arr2[] = {11,12,13}

I am getting the output as below: 1. Initialize arr1 with values 1,2,3 2. Initialize arr2 with values 11,12,13

However, I am stuck at writing logic for generating pseudo code in case there is a loop. For example,

        sum = 0
        for(int i=0; i<3; i++){
            sum += arr1[i]*arr2[i]
        }
        System.out.println(sum);  

Here's the Three address code for the above code:

      sum=0
      i=0
      L1:
           if i >= 3 goto L2
           T1 = addr(a)
           T2 = i * 4
           T3 = T1[T2]
           T4 = addr(b)
           T5 = i * 4
           T6 = T4[T5]
           T7 = T3 * T6
           T8 = sum + T7
           sum = T8
           T9 = i + 1
           i = T9
           goto L1
       L2:      
           print sum

        
  • @matszwecja op is writing it in python – Stultuske Apr 12 '23 at 09:58
  • why do you have goto's and L2 in pseudocode? – Stultuske Apr 12 '23 at 09:59
  • goto I have used to go back to L1 until i<3.. Is it not correct? – Jasleen Kaur Apr 12 '23 at 11:11
  • goto is actual code in scripting/programming languages, not really pseudo-code. as for L2: I see no actual reason why you would need that, since that printing is not in a loop (or at least, shouldn't be) – Stultuske Apr 12 '23 at 11:18
  • Oh I see. So how can I mention looping in three address code? – Jasleen Kaur Apr 12 '23 at 11:54
  • In the final, largest, code block, you write "Here's the pseudo code for the code above". Did you mean to say "...the three-address code for..."? In other words, is that output or input? Also, as you say that your goal is to generate pseudo code: could you elaborate a bit on what this pseudo code is supposed to look like? Generally speaking, the concept "pseudo code" is somewhat loosely defined and means whatever one wants it to mean... – Ture Pålsson Apr 12 '23 at 12:20
  • yes it was three address code. sorry for mistake. I have corrected it. Yes I want to generate pseudo code as below: Initialize sum with 0. Loop through array elements. Find the product of the corresponding elements of arr1 and arr2 and add the product to sum. When loop ends, print sum – Jasleen Kaur Apr 12 '23 at 12:38
  • There is no simple way to derive higher level code from three address code. You wrote you are stuck, but didn't explain what you have done so far and where exactly you got stuck. – trincot Apr 12 '23 at 21:18

0 Answers0