Practical One

Program Syntax Checker

due : 4pm 28 February 1997


Write a program to verify the syntax of the source code for a program, by checking that all brackets are found in matching pairs. Use a stack, implemented as an array to assist with this.

The stack must contain the following functions:

Your program must check that corresponding types of brackets match each other. For example, ( must match against ) and { must match against }. In order to accomplish this, all occurrences of left brackets must result in the bracket being "push"ed onto the stack. When you encounter a right bracket, you must check the topmost element of the stack - if it is the corresponding bracket, then you pop it, otherwise report an error. At the end of the program, you must check the stack for any extra elements and report an appropriate error.

The following expressions in a program ought to generate an error :

Input to your program must be in the form of a text file, which you read in one character at a time. Do not check for any other syntax except the matching of brackets - it does not matter if the test program does not compile, as long as the brackets match. Test your program thoroughly, with large files that are known to compile properly as well as with incorrect data.