1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
IrinaVladis [17]
3 years ago
7

In most programming languages, the compiler carries a preprocessing step to determine if certain statements will compile. For in

stance it may check to see if parentheses match.Write a Java program that simulates the actions of a preprocessor, to detect if certain Java constructs are syntactically correct. Table 1 shows the types of Java statement formats under consideration, and also example of each statement.
Computers and Technology
1 answer:
MakcuM [25]3 years ago
6 0

Answer:

See explaination for program code

Explanation:

package com.company;

import java.io.IOException;

import java.nio.file.Files;

import java.nio.file.Paths;

import java.util.Stack;

/*

* A program which reads input from given text filename and prints

* whether it passed the pre-processing stage or not

* Sample input

* int A = (a + b);static void display(int x)

* {

* //A sample input file

* }

* */

public class MyPreprocessor {

/*

* Reads input from filename and returns a string

* containing whole text

* */

public String readInput(String filename) {

String content = "";

try {

content = new String(Files.readAllBytes(Paths.get(filename)));

} catch (IOException e) {

e.printStackTrace();

}

return content;

}

/**

* Filters input since we are only concerned with

* a subset of input = '{}[]()/*'

*/

public String filterText(String text) {

String output = "";

for(int i = 0;i<text.length();++i) {

switch (text.charAt(i)) {

case '[':

case ']':

case '{':

case '}':

case '(':

case ')':

case '*':

case '/':

output += text.charAt(i);

}

}

return output;

}

/*

* Uses stack to determine if input is valid or not

* */

public boolean parse(String input) {

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

// '$' is special symbol to represent bottom of stack

St.push('$');

for(int i = 0;i<input.length();++i) {

Character symbol = input.charAt(i);

if(St.peek().equals(getOpenSymbol(symbol)))

St.pop();

else St.push(symbol);

}

return St.peek() == '$';

}

private static Character getOpenSymbol(Character symbol) {

switch (symbol) {

case '}': return '{';

case ']': return '[';

case ')': return '(';

case '*': return '*';

case '/': return '/';

default: return ' ';

}

}

public static void main(String[] args) {

MyPreprocessor preprocessor = new MyPreprocessor();

String inputText = preprocessor.readInput("src/input.txt");

String filteredInput = preprocessor.filterText(inputText);

if(preprocessor.parse(filteredInput))

System.out.println("preprocessing passed.");

else System.out.println("preprocessing failed!");

}

}

You might be interested in
When you open a program, the hard drive___
Papessa [141]

Answer:

When you open a program, the hard drive <u>Registers the program and runs the program accordingly. </u>

I hope this helped!

3 0
2 years ago
Calcule la carga que puede levantar un cabrestante si la manivela mide 40 cm y el El radio del molinete es una tercera parte de
Luda [366]

Answer:

C

Explanation:

because...

5 0
2 years ago
What Security Issu E Commerce ?​
Andre45 [30]

Answer:

e means we don't know so I can't tell the answer

5 0
2 years ago
What is the value of x after this statement?<br><br> double x = 2.0 / 5.0;
Step2247 [10]

Answer:

Exact Form:

x

=

2

_

5

Decimal Form:

x = 0.4

Explanation:

5 0
1 year ago
Read 2 more answers
If Carly wants to add wind belts for the part of the Atlantic just south of the equator, how should she draw the arrows?The arro
AnnyKZ [126]

If Carly wants to add wind belts for the part of the Atlantic just south of the equator, she should draw the arrows upward to the left. <span> At the South of the equator are trade winds which blow wind from the northeast toward the equator. </span>

4 0
2 years ago
Other questions:
  • Marie uses a browser to visit a blog. What is the unique identifier of the blog? A. web page B. website C. web address D. email
    7·2 answers
  • C2.5 - A group of four pirates has a treasure chest and one unique lock and key for each pirate. Using hardware that is probably
    6·1 answer
  • George and Miguel want to know more about their local and online competitors and about the retail industry. What is the best way
    9·1 answer
  • What is the purpose of filters in a data base
    11·1 answer
  • An organization is building a new customer services team, and the manager needs to keep the tream focused on customer issues and
    13·1 answer
  • Check examples of a good work ethic. *
    6·1 answer
  • If you could make another device "smart" and have
    12·1 answer
  • What is the correct order of precedence of the mathematical operators?
    12·1 answer
  • 1) You are working with an organization as a network manager. The organization has
    6·1 answer
  • How do you think electronic spreadsheets have transformed businesses today?​
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!