For this project, design and implement Java code that will check a source code for balanced parenthesis. You are to implement a dynamically allocated stack that will push and pop objects representing parenthesis as they are encountered in a source code. Since this part of the parser only cares about five different pairs of tokens, you can disregard any other tokens that you encounter along the way.

Do not over think this assignment. There is a little bit of business logic, parsing and a stack. Also, do not ask me if you can use a tokenizer for this assignment, since it will not help you so no, since while( is a valid syntax and so is while (. In other words you dont have a character to tokenize on, instead read in one character of a line at a time.

These are the syntax specifications for the programming language that you are to check the parenthesis balanceness for.

( ) - cannot contain 2,4,5 inside but can contain 3. This type of parenthesis is like a function call or any argument list type of semantic of the code.

{ } - These parenthesis delineate a block of code. Any number of nested parenthesis 1-5 can be encountered in this block.

[ ] - cannot contain 2, 4, 5. These parenthesis are used to dereference an array element.

/* */ A comment construct must be a matching parenthesis pair that can contain any number of parenthesis 1-3, 5 which can be mismatched.

// A single line comment, do not have to have right matching but is terminated by a newline character.

Make sure that you implement and check for parenthesis nesting.

If your implementation encounters a mismatched parenthesis, then you should print out the source line of the parent construct that was not matched (whenever possible if this cannot be determined, print the same line as when the error was encountered).

For example (of course your program must parse any source code with the above described specifications, not only the following example):

1 function foo (
2 array1 [],
3 array 2 [(),()],
4 asteroid ()
5 /* this function is for foo processing */
← Error: unmatched parenthesis '(' from line 1

Hints:

a tricky part for the example above would be if the 5 line would be 5 /* this function ) is for foo processing */ you would still get the same error message.

push objects on the stack, where each object contains not only the type of the parenthesis, but also a line number. This way it is easy to detect which line has the matching construct.

The easiest way to parse the source code file might be using a String reader and then walk the buffer you read in with a for loop.

Your program will read input from a command line (standard input). That being said, you are required to build a test suite of sample source code programs AND TURN IT IN to show how you went about testing your code.

Academic Honesty!
It is not our intention to break the school's academic policy. Projects posted are only used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly fill out the form. Please provide a valid email address and we'll get back to you in less than 24 hours. We will be sending an invoice through PayPal upon confirmation. We are a non profit organization however we need an amount to keep this organization running, and to be able to complete our research and development.