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

Help with this please? In most programming languages, the compiler carries a pre

ID: 3762391 • Letter: H

Question

Help with this please?

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.

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

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));

          }

    }

}

   

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