Software Development Problem Set:
Assessed Exercise
1 Introduction
For your assessed work you will need to develop a program in C and then use your
program to help answer a series of questions during your test next week.
Important points:
1. The program should be your own work— this is not a group exercise! You will
be required to submit your code for plagiarism checking.
2. If you need help with your programming, please seek help from the lecturer or
the TAs— not from colleagues or the Internet.
3. You must attend the test. You will only be able to access the questions during
your test.
4. You will need a working program so please be sure to arrive at the test with a
working program. Without a working program you will not be able to answer as
many of the on-line questions.
2 The Program
Syntax checking is critical to many computing applications, such as grammar check-
ers, compilers and so on. Your task is to write a program that reads in text containing
a series of one or more symbols and performs the following analysis on them. Your
program should support as many of features described below as possible.
2.1 Part A— syntax checking
1. Bracket counting. You should write a program that keeps a separate count of
the number of open and close brackets in the input file, and prints these counts
as output. Parentheses (), square brackets [], and braces {} should be tallied
independently.
2. Bracket validation. Extend your program to display whether the number of
open brackets matches the number of close brackets, or whether the
expression is unbalanced.
2.2 Part B— formatted output
Extend your program to print out the expression from the text file, such that:
Each line contains only a single open or close bracket
Each open causes the next line to be indented by 2 spaces
Each close causes the indent to unindent by 2 spaces
Any non-bracket symbol is printed at the appropriate level of indentation on its
own line.
Symbols between brackets may contain line breaks. If the input sequence con-
tains a line break, then remember to indent the next line.
For example, given the input:
([word])
the output would be (exactly):
(
[
word
]
)
Line breaks are honoured, but remember to indent new lines appro-
priately. So, given the input:
([hello
world])
the output would be (exactly):
( [
hello
world
]
)
Spaces a honoured, so, given the input:
([hello world])
the output would be (exactly):
(
[
hello world
]
)
2.3 Part C— syntax validation
Extend your program to validate whether the expression is properly nested, i.e. the
brackets are in matched rather than mismatched pairs. Examples: (), and ([]) are all
nested correctly, whereas {[(}]) is incorrectly nested (the pairs do not match).
2.4 Notes
The source input may contain multiple lines, but excludes blank lines. Lines of text will
not exceed 80 characters in length. You can assume that brackets can not be nested
deeper than 30 levels.
On Moodle we will supply you with a test file and the expected outputs so that you
can check that your program is working. The numerical output does not need to pre-
cisely match the output from our program, however, the formatted output should
match precisely.
3 Next week test
In the test you have to submit your program for plagiarism checking and then
answer a series of on-line question. The questions are designed to assess your
understanding of the program that you have written and of programming in general.
Note that we will not mark your program itself— rather we will mark your answers to
the questions that we have set. Below are examples of questions that you might be
asked:
Q1. Run your program on the data file called syntax1.dat. Based on the output
answer the following questions.
Q2. Consider the program contained in the file reformat.c. Reformat the pro-gram
so that it meets the style. guidelines specified. Paste in the exact format of line 4
(currently printf("hello world.");)
Q3. Download the program contained in the file test1.c. Correct the syntax error
on line 27 and paste in the new version of the line.
During the assessment you will NOT be able to compare notes with your
colleagues.
4 Resources
There are some useful functions which you may want to consider to help address the
first step in this task:
1. int fgetc (FILE stream); — Read a single ASCII character from a file ‘stream’. A
special value ‘EOF’ is returned if the end of the file is reached.
2. char fgets(char str, int size , FILE stream) — Get a line from the file (stream) and
copy it into ‘str’. Take care that you’ve set ‘size’ to be large enough to read the
whole line from the file. A very common error is not to declare an array big enough
(and thus allocate enough space) for the resulting data.
3. You may find the functions in ‘ctype.h’ helpful.
4. Recall that a character symbol e.g. ‘A’ is really stored internally as an integer in
the ASCII code table (65 in this case), see: http://www.asciitable.com/. This
means you can do simple arithmetic on characters, for example if you have a
character variable containing an upper case letter you can convert it to lower case
simply by adding 32 (the distance between ‘a’ and ‘A’ in the ASCII table).