Question

In: Computer Science

You should implement a syntax checking program that is similar to a function of compiler. Compliers...

You should implement a syntax checking program that is similar to a function of compiler. Compliers check programs for syntax errors, but frequently a lack of one symbol (such as a missing brace or com-ment starter) will cause the complier to spill out a hundred lines of diagnostics without identifying the real error. A useful tool in this situation is a program that checks whether everything is balanced. Thus, every right brace, bracket, and parenthesis must correspond to its left counterpart. The sequence [()] is legal, but [()]) is wrong.

I get a runtime error, how can I solve this?

Solutions

Expert Solution

This should work =]

import java.util.*;
import java.io.*;
public class Test
{
private static char[] stack=new char[100000];
private static int stackPoint=-1;
public static void main(String[] args) throws Exception
{
System.out.println("Enter file name: ");
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String file=in.readLine();
Scanner scan=new Scanner(new FileReader(file));
out:while (scan.hasNext())
{
String stg=scan.nextLine();
for(int k=0; k<stg.length(); k++)
{
if(stg.charAt(k)=='{' || stg.charAt(k)=='(' || stg.charAt(k)=='[')
{
push(stg.charAt(k));
}
else if(k!=stg.length()-1 && stg.charAt(k)=='/' && stg.charAt(k+1)=='*')
{
push('/');
k++;
}
else if(stg.charAt(k)=='}' || stg.charAt(k)==')' || stg.charAt(k)==']')
{
char c=pop();
if(c=='-')
break out;
if(stg.charAt(k)=='}' && c!='{' || stg.charAt(k)==')' && c!='(' || stg.charAt(k)==']' && c!='[')
{
push(c);
break out;
}
}
else if(k!=stg.length()-1 && stg.charAt(k)=='*' && stg.charAt(k+1)=='/')
{
char c=pop();
if(c=='-')
break out;
if(c!='/')
{
push(c);
break out;
}
}
}

}
if(stackPoint==-1)
{
System.out.println("YES");
}
else
System.out.println("NO");

}

static void push(char c)
{
stackPoint++;
stack[stackPoint]=c;
}
static char pop()
{
stackPoint--;
if(stackPoint<=-2)
return '-';
return stack[stackPoint+1];
}
}


Related Solutions

You are to implement a program to play “Guess an Animal.”  Your program should construct a tree...
You are to implement a program to play “Guess an Animal.”  Your program should construct a tree with a string at each node.  This tree stores the knowledge that your program currently knows.  At leaf nodes, the strings should contain the names of animals, while interior nodes should contain yes/no questions.  The program should start at the root and ask the question stored at the root.  If the user answers the question no, the program traverses the left branch of the tree, while if the...
Write a function for checking the speed of drivers. This function should have one parameter: speed....
Write a function for checking the speed of drivers. This function should have one parameter: speed. 1. If speed is less than 70, it should print “Ok”. 2. Otherwise, for every 5km above the speed limit (70), it should give the driver one demerit point and print the total number of demerit points. For example, if the speed is 80, it should print: "Points: 2". 3. If the driver gets more than 12 points, the function should print: “License suspended”...
Write a program to implement and analyzing the Bubble Sort. a. Write a C++ function for...
Write a program to implement and analyzing the Bubble Sort. a. Write a C++ function for Bubble Sort b. Use a dynamic array of integers in a variable size of n. c. Display the following information: 1) Total counts of comparisons 2) Total counts of shifts / moves / swaps, whichever applies d. Write a main() function to test a best, and an average cases in terms of time efficiency i. Fill out the array with random numbers for an...
Write a program in C++ to implement Lamport’s logical clocks. Your program should take as input...
Write a program in C++ to implement Lamport’s logical clocks. Your program should take as input a description of several process schedules (i.e., lists of send, receive or print operations). The output of your program will be a linearization of these events in the order actually performed, annotated with Lamport clock values. The input of the program will be a collection of processes, each with a list of operations to perform. The processes are named p1...pn for some n (you...
data structure class: a. syntax for generics b. comparable interface c.how do you implement interface answer...
data structure class: a. syntax for generics b. comparable interface c.how do you implement interface answer for all of them please. answer for all of them please
/*Use recursion in the function: void getDigit( int num) /* Try this program and implement the...
/*Use recursion in the function: void getDigit( int num) /* Try this program and implement the function void getDigit( intnum) so that it displays the digits in a given number. For example, the number, 1234 consist of 1, 2, 3 and 4. This is an exercise to demonstate the use of a recursive function and the use of recusrion in C++ */ #include #include using namespace std; int main() {      int num;      cout <<"Enter an integer: ";     ...
1. Checking accts & MMDA's are very similar. Where would you put your $ & why?...
1. Checking accts & MMDA's are very similar. Where would you put your $ & why? 2.What reserve requirement would you put on banks? Why? Does the type of loans the bank make effect your decision? 3. If I rates are increasing is that a + or - for bank earnings? Answer all 3 please thank you!
dd a new function that is similar to the counting function that you already have -...
dd a new function that is similar to the counting function that you already have - but which uses a for loop to do the exact same thing (count from n//2 to n). Your solution must use a for loop with a range function. Instrument your code by adding print functions so that we can tell which function is doing the counting, as shown in the sample output below. Modify your program so that it alternates between using your for...
The application of syntax analysis techniques in query processing system such as SQL You should cover:...
The application of syntax analysis techniques in query processing system such as SQL You should cover: 1) What is the problem? 2) What is the compiler construction techniques used to solve the problem 3) How to solve the problem using the compiling techniques.
In this question, you should complete the C program, perfect.c, by implementing the function isPerfect. Open...
In this question, you should complete the C program, perfect.c, by implementing the function isPerfect. Open the file perfect.c, complete, compile and run it. When you are sure it is correct, include the c file in your final submission. * Note: You should first carefuly read the COMMENTS provided for the function isPerfect, and then start completing the function. * Note: You MUST NOT alter the main function and the prototype/header of the isPerfect function. ONLY develop the body of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT