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
What i have to care about transmitting data packet from one controller to other controller?
Nata [24]
You, nothing! The <span>Network is responsible of </span>transmission<span> of </span>data<span> from </span>one<span> device to </span>another<span> device</span><span>.</span>
3 0
4 years ago
When an application contains just one version of a method, you can call the method using a(n) ____ of the correct data type.
faltersainse [42]

Answer:

Parameter

Explanation:

q: When an application contains just one version of a method, you can call the method using a(n) ____ of the correct data type.

a: Parameter

3 0
3 years ago
Most 4 year colleges want an applicant's scores for all the following except
Nataly_w [17]

Answer:

IQ

Explanation:

5 0
3 years ago
What is a font? A)
BlackZzzverrR [31]

Answer:

A is most likely the answer

Explanation:

5 0
3 years ago
The parts of a memo are _____.
Alex
The correct answer would be D
4 0
4 years ago
Read 2 more answers
Other questions:
  • Which of the following corresponds to the computer interface technology that uses icon, etc?
    6·1 answer
  • Select the correct answer.
    9·2 answers
  • Universal Containers has a custom picklist filed with three values on their products. The Admin would like to create a Dynamic B
    10·1 answer
  • Over the past few years a very definite need has arisen in the electrical trades for:
    15·1 answer
  • What is the most efficient way to prevent the spelling checker from repeatedly flagging a correctly spelled name in all of your
    5·1 answer
  • Customizing ads to people who had earlier visited the site is
    14·1 answer
  • Explain why it is wise to remember your social security number
    15·2 answers
  • Outline 3 computer system problem that could harm people and propose the way avoid the problem​
    12·1 answer
  • What is Mark Dean’s main contribution to the development of the computer?
    9·1 answer
  • When you type information into a document property field on the cover page, Word does not automatically add this information to
    11·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!