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”...
Implement a substring search function which searches a string for a given keyword. The function should...
Implement a substring search function which searches a string for a given keyword. The function should return a pointer to the address of the first character of the substring within the original string, if it exists. If the substring is not found within the string, return NULL. If the input is found, print "Key found." otherwise print "Key not found." Your program’s input and output should match what is seen in the example run. • You may only use stdio.h....
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...
Part 1 1. Implement the binary search function. The function should take as formal parameters an...
Part 1 1. Implement the binary search function. The function should take as formal parameters an array of integers, its size and target, and returns (a) in the case of successful search – the position (index) of the target in the array (b) in the case of unsuccessful search – an exception with the message “Unsuccessful search” thrown from the function to the user. 2. The binary search works correctly only when the input is sorted. This requirement should be...
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...
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
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT