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

Help with this java code please? !!!!!!!please provide the code running. In most

ID: 3853409 • Letter: H

Question

Help with this java code please? !!!!!!!please provide the code running.

In most programming languages, the compiler carries a preprocessing step to determine of certain statements will compile. For instance it may check to see if parentheses match.

Write a Java program that simulates the actions of a preprocessor to will detected if certain Java constructs are syntactically correct. Table 1 shows the types of Java statement formats under consideration:

Construct

Example

Statement

data_type = expression

int x = 3 + (10 – 4) * ( 10 + 4)

Method

<attrib> rt name(<parameter>)

{

<statement>

}

public void display(int n)

{

int arr[ ] = new int[n];

        System.out.println( x[2]);

}

class

<attrib> class Name

{

      dt fields;

    <attrib> Name(<parameter> )

      {

                 method (<parameter>);

       }

       <attrib> rt method(<parameter>)

{

}

}

public class MyParser

{

      public static void main(String [ ] arg )

      {

                 display (10);

       }

       static void display(int x)

{

         /*

              My pre-processor

         */

}

}

Table 2 shows the delimiters under consideration.

Table 2

Delimiters

Symbol

Left parenthesis

(

Right parenthesis

)

Left curly braces

{

Right curly braces

}

Left square brackets

[

Right square brackets

]

Forward slash

/

Star (multiplication symbol)

*

Note:

In your implementation, design a class called Preprocessor that accepts a string that represents the statement to be analyzed. The class will contain, among other possible methods, a method that determines whether or not the given statement is valid, with respect to the delimiters of Table 2. Do not be concerned with other symbols.

You will need a test class. You may want to name it MyPreprocessor.

You may have to enter all statements on a single line, unless you will be reading the input from a file, in which the file would be read using presumable the class BufferedReader or LineNumberReader.

Your output would echo the input, and say whether or not the input passed the preprocessing stage.

You are to use the concept of stack to determine if the constructs are syntactically correct.

Expert Answer

Was this answer helpful?

0

0

816 answers

Answer:

/*** JAVA PROGRAM THAT SIMULATES THE PREPROCESSOR *****/

import java.util.Stack;

import java.util.*;

import java.io.*;

class Preprocessor

{

     public static boolean balance_Chek(String inStr12)

    {

        Stack<Character> s_st1 = new Stack<Character>();

        for(int k1 = 0; k1 < inStr12.length(); k1++)

        {

                   char let = inStr12.charAt(k1);

                   if(let == '[' || let == '{' || let == '('||let=='/')

                   {

                        switch(let)

                        {

                    // FORSQUARE BRACKET

                                  case '[':

                                      s_st1.push(let);

                                      break;

                    // FOR CURLY BRACE

                                  case '{':

                                      s_st1.push(let);

                                      break;

                    // FOR BRACKETS

                                  case '(':

                                      s_st1.push(let);;

                                      break;

                //FOR COMMANDS

                        case '/':

                             char t=inStr12.charAt(k1+1);

                             if(t=='*')

                             s_st1.push(let);

                  

                             break;

                                  default:

                                  break;

                        }

                   }

                   else if(let == ']' || let == '}' || let == ')'||let=='*')

                   {

                        if(s_st1.empty())

                                  return false;

                        switch(let)

                        {

// FORSQUARE BRACKET

                                  case ']':

                                      if (s_st1.pop() != '[')

                                      return false;

                                      break;

// FOR CURLY BRACE

                                  case '}':

                                      if (s_st1.pop() != '{')

                                           return false;

                                      break;

                    // FOR BRACKETS

                                  case ')':

                                      if (s_st1.pop() != '(')

                                      return false;

                                      break;

                //FOR COMMANDS

                        case '*':

                             if(inStr12.charAt(k1+1)=='/')

                                      if (s_st1.pop() != '/')

                                      return false;

                                      break;

                                  default:

                                      break;

                        }

                   }

        }

        if(s_st1.empty())//CHECK STACK IS EMPTY

                   return true;

        return false;

    }//EMD METHOD

}

//TEST CLASS

public class MyPreprocessor

{

//MAIN METHOD

          public static void main (String [] args)

    {

       

//FileReader AND BuffeRedreader TO READ FROM FILE

          FileReader fk=new FileReader("tet.txt");

          BufferedReader bk=newBufferedReader(fk);

          Preprocessor p;

          String preLine;

//READING FROM FILE

          while((preLine=br.readLine())!=null)

          {

//DISPLAY THE STRING AS WELL AS CHECK ITS BALANCED DELIMITERS

              System.out.println("Line is: "+preLine +" is Balanced?"+balance_Chek(preLine));

          }

    }

}

Construct

Example

Statement

data_type = expression

int x = 3 + (10 – 4) * ( 10 + 4)

Method

<attrib> rt name(<parameter>)

{

<statement>

}

public void display(int n)

{

int arr[ ] = new int[n];

        System.out.println( x[2]);

}

class

<attrib> class Name

{

      dt fields;

    <attrib> Name(<parameter> )

      {

                 method (<parameter>);

       }

       <attrib> rt method(<parameter>)

{

}

}

public class MyParser

{

      public static void main(String [ ] arg )

      {

                 display (10);

       }

       static void display(int x)

{

         /*

              My pre-processor

         */

}

}

Explanation / Answer

//Please try the below code it will work the attached code in question is not working.

//NOTE : To Run the below code create file with name "test" in D folder inside the file you can enter the string you

// want to check like "int x = 3 + (10 – 4) * ( 10 + 4)"

import java.util.*;
import java.io.*;
import java.io.LineNumberReader;
import java.io.FileReader;
import java.io.IOException;

public class Preprocessor{

   public static void main(String[] args) throws IOException {
      
       FileReader fr = null;
        LineNumberReader lnr = null;
        String str;
        int i;
        try {
        
             // create new reader
             fr = new FileReader("D:/test");
             lnr = new LineNumberReader(fr);
     
             // read lines till the end of the stream
             while((str = lnr.readLine())!=null) {
                i = lnr.getLineNumber();
                         
              
                if(balancedParanthesis(str))
                {
                   System.out.println("your Input: "+str +"is PASSED the preprocessing stage");
                }
                else
                {
                   System.out.println("your Input: "+str +"is NOT PASSED the preprocessing stage");
                }
             }
           
          } catch(Exception e) {
           
             // if any error occurs
             e.printStackTrace();
          } finally {
           
             // closes the stream and releases system resources
             if(fr!=null)
                fr.close();
             if(lnr!=null)
                lnr.close();
          }
       }


   public static boolean balancedParanthesis(String s){
  
   Stack<Character> st = new Stack<Character>();
  
   for(int i = 0; i < s.length(); i++) {
       char c = s.charAt(i);
       if(c=='{' || c=='[' || c=='('){
           st.push(c);
       }
       else if(c == ']') {
            if(st.isEmpty()) return false;
            if(st.pop() != '[') return false;
  
        }else if(c == ')') {
            if(st.isEmpty()) return false;
            if(st.pop() != '(') return false;
  
        }else if(c == '}') {
            if(st.isEmpty()) return false;
            if(st.pop() != '{') return false;
        }
      
   }
   return st.isEmpty();
      
   }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote