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]
4 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]4 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
Why is it important to enforce access controls and to keep logs regarding who has access to data closets
vlada-n [284]

Answer:

To ensure that in the event of a failure or breach, there is a record of when the equipment was accessed.

Explanation:

Hope this helps:)....if it don't then sorry for wasting your time and may God bless you:)

8 0
3 years ago
Read 2 more answers
If you are trying to improve your budget and spending, which option would save you the most money?
Solnce55 [7]

Explanation:

If you want to improve your budget and spending, then the best option that would save you the most money would be buying the securities for a long period of time on fixed basis. This will allow you to hold your savings with the bank, and you won't be able to take them out easily before the term period. In this way you won't think of spending that money. Also you can have an interest amount earned on these securities. In this way you can save the most money.

8 0
3 years ago
Read 2 more answers
Which of the following is not a concern that has come out of the widespread adoption of the Internet?
slamgirl [31]

Answer:

The correct option is;

Selecting the correct browser to use for the sites you want to visit

Explanation:

Censorship of information is the subduing of information based on the consideration of such information being harmful or inconvenient

Increased anonymity

Making an individual anonymous which is one of the capabilities of the internet, is seen as encouraging unethical behavior and promoting discussions which are deemed uncivil in a commonly shared space online.

Unauthorized sharing of copyrighted information

The sharing and distribution of copyrighted material is against the law but it is a common occurrence and in various formats online

Out of the given options, selecting the correct browser to use for the sites you want to visit is the one that is not a concern.

5 0
3 years ago
In a(n) ____________________ device, the movement of electrons performs essentially the same functions as gears and wheels in me
lana66690 [7]

Answer:

Electronic computing.

Explanation:

8 0
2 years ago
PLzzzzzz help me!! I will mark brainiest to the one who answers it right!!
jasenka [17]

Answer: abstract algebra

Explanation: start with the algorithm you are using, and phrase it using words that are easily transcribed into computer instructions.

Indent when you are enclosing instructions within a loop or a conditional clause. ...

Avoid words associated with a certain kind of computer language.

4 0
3 years ago
Other questions:
  • The first time that a particular visitor loads a web site page is called a(n) _____.
    5·1 answer
  • Data about the size and composition of a population are gathered in a survey called _____.
    6·2 answers
  • For example, sumDigits(234) returns 9 (= 2 + 3 + 4). (Hint: Use the % operator to extract digits and the / operator to remove th
    15·1 answer
  • )the number of levels in a tree is called
    6·1 answer
  • An image can be copied from the Internet or another location and pasted into Paint.
    13·1 answer
  • Which part of the Word application window should the user go to for the following activities?
    14·1 answer
  • What can you use on Code.org's Web Lab to find and fix problems when you are writing code for your website?
    13·1 answer
  • It is where your cpu (processor) is installed
    10·2 answers
  • Who wants 100 points? comment buh. i don't rlly care buh.
    15·2 answers
  • Which of these countries has very strict cultural
    8·2 answers
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!