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
What is the name of the function used to open a file in C?
Rudiy27

Answer:

prompt box

Explanation:

the term has changed over the years.

4 0
2 years ago
Can you redact this image
spin [16.1K]

Answer:what???

Explanation:so

What

6 0
2 years ago
Linda is the owner of Souvenirstop, a chain of souvenir shops. One of the shops is located at the City Centre Mall. Though the s
Klio2033 [76]

Answer:

attraction and attention

6 0
3 years ago
Has anyone used freya on Mobile legends bang bang
Vsevolod [243]
Yeaaaaahhh it’s so fun
4 0
3 years ago
Read 2 more answers
Can someone answer this for me will award brainliest lol
Natasha2012 [34]

Answer: Without GUI's being developed, we most likely would still be using terminal systems. Terminals require command/text input rather than mouse or  other input. The invention of GUI's enabled people to use simpler input methods, rather then clunky text input systems. To sum, GUI's simplified the computer experience as we know today.

Hope this helped :)

6 0
2 years ago
Read 2 more answers
Other questions:
  • Aubrey didnt like to use graphics or images on her slides. She preferred to use only a title for her slides and bullet-poinged t
    14·2 answers
  • Which of the following typically have the highest auto insurance premiums?
    14·1 answer
  • Write a program that responds to a positive integer passed on the command line with the number of bits needed to express that nu
    10·1 answer
  • Brainly won't let me create an account even though i tried multiple emails and ages! It keeps saying "We're sorry, but we are no
    8·2 answers
  • print out the last even number from an array of integers, if there is no even number, then print out a sentence indicating so.
    5·1 answer
  • In the Dynamic Partitioning technique of memory management, the placement algorithm that scans memory from the location of the l
    11·1 answer
  • What will the value of x be after the following statements execute? int x = 0; int y = 5; int z = 4; x = y z * 2;
    14·1 answer
  • Find a 95% confidence interval for the mean failure pressure for this type of roof panel.The article "Wind-Uplift Capacity of Re
    7·1 answer
  • What is the role of the memory in a computer ​
    11·1 answer
  • Which member of the Jackson family was the spokesperson for the Psychic Friends Network.​
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!