Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with this question: LD A places the operand A in the register ST A p

ID: 671357 • Letter: I

Question

I need help with this question:

LD A places the operand A in the register

ST A places the contents of the register into the variable A

AD A adds the contents of the variable A to the register

SB A subtracts the contents of the variable A from the register

ML A multiples the contents of the register by the variable A

DV A divides the contents of the register by the variable A

Write a program that accepts a postfix expression containing single letter operands and the operators +, -, *, and / and prints a sequence of instructions to evaluate the expression and leaves the result in the register. Use variables of the form TEMPn as temporary variables. For example, using the postfix expression ABC*+DE-/ should print the following:

LD B

ML C

ST TEMP1

LD A

AD TEMP1

ST TEMP2

LD D

SB E

ST TEMP3

LD TEMP2

DV TEMP3

ST TEMP4

Use the following postfix expressions for input

AB+C- ABC+/CBA*+

ABC+- AB-*CBA+-*

AB-C+DEF-+$ ABC-/BA-+/

ABCDE-+$*EF*- ABC+$CBA-+*

ABC+*CBA-+* AB0+/CBA+-/

Keep in mind that you are NOT evaluating the postfix expression, you are generating the machine language instructions that would perform the evaluation if actually executed.

In your analysis, be sure to discuss the implementation you choose and why, why a stack makes sense. Consider a recursive solution (you do not need to implement recursion) and compare it to your iterative solution. Is one better than the other? Why? Tell us what you learned and what you would do differently. You may not use library functions. you must write the stack code. You must read and write from named files.

Explanation / Answer

package Cryptography;

import java.io.*;
import java.util.*;

public class Design_a_Compiler {

        public static String createExpression(String exp)
        {
       
            Stack st = new Stack();
            int start = 1;
            StringBuffer s1 = new StringBuffer();
       
        for (int i = 0; i < exp.length(); i++)
        {
            char ch = exp.charAt(i);
            System.out.println(ch);
            if (ch == '+')
            {
                if (st.empty())
                    {
                        return "Not a valid Expression!!!!";
                    }
                String b = (String)st.pop();
                if (st.empty())
                {
                    return "Not a valid Expression!!!!";
                }
                String a = (String)st.pop();
                s1.append("LD " + a + " ");
                s1.append("AD " + b + " ");
                String temp = "TEMP" + index++;
                s1.append("ST " + temp + " ");
                st.push(temp);
            }
            else if (ch == '-')
            {
                if (st.empty())
                {
                    return "Not a valid Expression!!!!";
                }
                String b = (String)st.pop();
                if (st.empty())
                {
                    return "Not a valid Expression!!!!";
                }
                String a = (String)st.pop();
                s1.append("LD " + a + " ");
                s1.append("SB " + b + " ");
                String temp = "TEMP" + index++;
                s1.append("ST " + temp + " ");
                st.push(temp);
            }
            else if (ch == '*')
            {
                if (st.empty())
                {
                    return "Not a valid Expression!!!!";
                }
                String b = (String)st.pop();
                if (st.empty())
                {
                    return "Not a valid Expression!!!!";
                }
                String a = (String)st.pop();
                s1.append("LD " + a + " ");
                s1.append("ML " + b + " ");
                String temp = "TEMP" + index++;
                s1.append("ST " + temp + " ");
                st.push(temp);
            }
            else if (ch == '/')
            {
                if (st.empty())
                {
                    return "Not a valid Expression!!!!";
                }
                String b = (String)st.pop();
                if (st.empty())
                {
                    return "Not a valid Expression!!!!";
                }
                String a = (String)st.pop();
                s1.append("LD " + a + " ");
                s1.append("DV " + b + " ");
                String temp = "TEMP" + index++;
                s1.append("ST " + temp + " ");
                st.push(temp);
            }
            else
            {
                st.push(String.valueOf(ch));
            }
        }
        return s1.toString();
    }
   

    public static void main(String[] args) throws FileNotFoundException, IOException
    {
       
        Scanner keyboard = new Scanner(System.in);
        System.out.print( "Enter the input filename: " );
        String in_file = keyboard.nextLine();       
        File infile = new File( in_file );            

    if ( infile.exists() )       
    {                                            
        Scanner S1 = new Scanner( infile );
        while ( S1.hasNext() )
        {
            String line = S1.nextLine();
            //System.out.println( " " + line );
        }
        //S1.close();
      
        System.out.print( "Enter the output filename: " );
        String out_file = keyboard.nextLine();       
        File outfile = new File( out_file );
            
           
            if (!outfile.exists())
            {
                outfile.createNewFile();
            }

            FileWriter fw = new FileWriter(outfile.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
           
           
           
            while (S1.hasNextLine())
            {
               
                String expression = S1.nextLine();
                bw.write(expression + ": ");              
                bw.write(createExpression(expression));
                bw.write(" ");
            }
           
           
            bw.close();

            System.out.println("The sequence of instructions have written to file");
           
        }
    }
}