Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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

rt name()

{

}

public void display(int n)

{

int arr[ ] = new int[n];

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

}

class

class Name

{

dt fields;

Name( )

{

method ();

}

rt method()

{

}

}

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

image text in transcribedBhanuP answered this

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 s_st1 = new Stack();

for(int k1 = 0; 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));

}

}

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Students also viewed these Databases questions

Question

=+Are there shop stewards?

Answered: 1 week ago