CSCI 3342 Submission 08
Checking for Balanced Symbols
Supplied file(s) |
$sup08/demo_bs_checker (the demo executable)$sup08/bs_checker.txt (the TextItems file)$sup08/sample_files.zip (a zip file of sample input files)
$sup08/demo_bs_checker_with_trace $sup08/bs_checker_with_trace.txt (a version of the executable which includes a tracing feature that you do not have to implement, but which may be a helpful reference during the development of your solution) |
---|---|
Files to submit |
bs_checker.cpp (your source code)bs_checker (your executable)my_tests.sh (your testing script)
|
Where to put them |
Copy them to the submissions/s08 folder in your
uxx account.
|
When they're due | Sunday, March 23, 2025 @11:59pm |
This week you will develop a simplified "balanced symbol checker". That is, you will develop a simplified "utility" program that analyzes a file of text (a C++ or Java program, for example, but not necessarily) to see if each left parenthesis has a corresponding right parenthesis (and similarly for square and curly brackets) and to ensure that all symbols are properly nested if there is any nesting.
This is only a simplified checker because we assume that comments and quoted strings do not contain any single unmatched such symbols. This would not cause a C++ compiler any problem, as you know, but it would cause our program to report a problem where there isn't any when analyzing C++ or Java source code.
Also, for simplicity, we do not include the symbols <
and
>
in our balance checking, since that would require
additional programming to deal with things like the extraction and insertion
operators (>>
and <<
) and the
inequality symbols (<
, >
,
<=
, >=
), in which those symbols do not
appear in "balanced pairs".
sample_files.zip
containing both "good"
and "bad" sample input files (that is files containing balanced symbols
and files containing unbalanced symbols). However, it will be much better
if you now create a few test input files of your own, perhaps even before
looking at the provided sample input files. Experiment with your own
sample input files to make the symbols we are checking both "unbalanced"
and "balanced". Run the sample executable with your revised input file in
each case.
bs_checker.txt
) which your program must
access and display in the usual way.
uxx
account.
Could not open file filename.ext. Program now terminating. The file has matching symbols. ? at index ? on line ? does not match ? at index ? on line ?. ? at index ? on line ? is missing the corresponding open symbol. ? at index ? on line ? has no matching close symbol. The file has unmatched symbols.
struct
that contains three fields: a
char
field to hold a symbol of the kind you're looking for,
and two int
fields for the line number and position on the
line where that symbol was found. Then have a stack
of these
struct
s, onto which you push each "left symbol" (and
its info) that you see, and when you see the matching "right symbol" you
pop off of the stack
the struct
with the
corresponding "left symbol" . If you have all matching symbol pairs, this
will give you an empty stack
at the end; otherwise something
is wrong, either along the way or at the end, and your program must
recognize and report whatever has gone wrong.