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 US agency originally started ARPANET?
RUDIKE [14]
<span>c. Department of Advanced Research Projects Agency</span>
8 0
3 years ago
In the circuit shown in the figure above, what will happen when switches S1 and S3 are both closed? 
sp2606 [1]

Well, ya see, we need to have the picture of the circuit.

8 0
2 years ago
Given the list my_list containing integers, create a list consisting of all the even elements of my_list. Associate the new list
Natasha_Volkova [10]

In python:

new_list = [x for x in my_list if x % 2 == 0]

4 0
3 years ago
Suppose x is 1. What is x after x = 1?<br> A. -1<br> B. 0<br> C. 1<br> D. 2<br> E. -2
Dimas [21]

Answer:

its number 10204921094993293959592828358

Explanation:

also 339399393993939399393

8 0
3 years ago
Guidelines for organizing files and folders
Rom4ik [11]

Answer:

Don't mix business and personal files, group it by category or date. Create folder templates.

4 0
3 years ago
Other questions:
  • A peripheral can be used to tell a computer to complete a specific task. True False
    15·1 answer
  • Hit and Slopes Program: A Write a program that can be used by a ski resort to keep track if local snow conditions for one week.
    15·1 answer
  • Hanging out with friends, watching your favorite TV show, and buying a pair of new shoes are all examples of _____ for doing wel
    5·2 answers
  • On computer X, a nonpipelined instruction execution would require 12 ns. A pipelined implementation uses 6 equal-length stages o
    9·1 answer
  • To reduce inflation, the Federal Reserve _____ the money supply. This action also causes the economy to shrink.
    13·2 answers
  • Do rats smell good? what do you think
    8·2 answers
  • What is the mode of 18, 18, 15, 18, 18, 24, 21, 21, 24, and 14?<br> 18<br> 18.5<br> 21<br> 22.3
    13·1 answer
  • What is the first step a user should take toward generating an index?
    7·1 answer
  • What is net pay?<br> What is net pay?
    11·2 answers
  • Write a program to prompt the user for hours worked to compute a gross pay for an employee, and he is paid 100/hour. If he worke
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!