You are on page 1of 2

COSC2320: Data Structures and Algorithms

HW3: Check Balanced Symbols in C++ Programs Using Stacks

1 Introduction
You will create a C++ program to check an input C++ program. The program must exploit a stack to check
correctly balanced symbols.
In order to simplify parsing we will focus on 1-character tokens. Specifically, we will consider:
()
[]
{}
" "

2 Input
The input will be one C++ program, which may or may not be indented. You cannot assume that the program
is correctly indented. You can assume there will be only one arithmetic expression per line.

3 Examples
INPUTS
#include <stdio.h>
#include <string>
int main() {
int x;
int a[10];
string s="xyz",s1="(a string),a bracket array A[10]";
x=10*2+(3-4)*2;
a[0]=x;
}

4 Program and output specification


The main program should be called checkbalanced. Syntax:

checkbalanced "input=file.cpp"
Write output to the console as follows.
Correct input file:
Write Correct
For an incorrect input file with at least one error, write the line number, the symbol at the top of the
stack and the current incorrect symbol:
Error at line #: top()=<symbol>;current=<symbol>.

5 Requirements
Your program must use stacks. Every time you find an opening symbol you push() it. Everytime you
find a closing symbol you must try to pop() the opening from the stack. Notice pop() should work
only when the closing symbol matches the opening symbol. That is, incorrect closing symbols should
be skipped until success or end of file.
You are allowed to build your stacks in any way you prefer, as long as the fundamental stack operations
push(), pop() and top() exist. Your program must not crash due to stack overflows or underflows.
Consider extreme cases: empty files, files with only opening symbols, files with only closing symbols.
It is acceptable to treat end-of-file as a closing symbol to make your program shorter. Errors detected
with end of file can be reported on line n + 1 with symbol $.
You are encouraged to solve this homework in one pass, reading the input file once. But it is acceptable
to use any dynamic data structures to store the file lines.
It is acceptable to use an array for the stack. You can assume the input file will be at most 1000
characters in such case. But you are encouraged to use linked lists to have a dynamic size stack.
Notice the stack allows you to detect errors,
The program should not halt when encountering errors. The only error that is unrecoverable is a
missing input file.
Test cases: Your program will be tested with 10 test cases, going from easy to difficult. You can
assume 80% of test cases will be clean, valid input files. If your program fails an easy test 10-20
points will be deducted. A medium difficulty test case is 10 points off. Difficult cases with specific
input issues or complex algorithmic aspects are worth 5 points.
Extra credit: +10 for linked lists, +20 for detecting errors in arithmetic expressions (unbalanced parenthjeses, missing operators, more operators than needed, list of numbers without operators).
A program not submitted by the deadline is zero points. A non-working program is worth 10 points.
A program that does some computations correctly, but fails several test cases (especially the easy
ones) is worth 50 points. Only programs that work correctly with most input files that produce correct
results will get a score of 80 or higher. In general, correctness is more important than speed.

You might also like