Hints for Programming Assignment 2

 

The programming assignment requires that you develop a PDA that recognizes a language of strings representing certain valid arithmetic expressions sandwiched between delimiters, and then implement the PDA as a program in C, C++, Java, or python.

 

You should probably start by designing a PDA assuming that the operands are single-digit integers and without the delimiters.  Once you figure this out, then modify it to obtain a PDA to handle other types of operands and the delimiters as specified in the assignment.

 

To help you come up with a PDA when operands are single-digit integers, first define a state corresponding to each type of symbol, and as long as what you have read so far has been valid, then the PDA will be in the state corresponding to the previous symbol read.  Thus, there will be one state for when the previous symbol read is an operator (+. -, etc.), there will be one state for when the previous symbol is a digit, and so on.

 

Then think about reading in a string one symbol at a time and, based on the previous symbol read in, what are the allowable choices for the next symbol.  For example, if the previous symbol was an operator, then the only valid choices for the next symbol are an operand or a left parenthesis.  In addition, you need to make sure the parentheses match up properly, which will require using a stack (see problem 1h of HW 6 for a PDA that matches up brackets). Thus, each state in your PDA indicates the previously read-in symbol, and you should make transitions based on the next symbol you read and what you pop from the stack.

 

You must turn in a drawing of your PDA, your program, and output on the test cases. Your drawing and program must only handle input symbols that are in the input alphabet. If your program does not implement the PDA in your drawing, you will not receive any credit for the assignment.

 

You should expect to spend at least 5-10 hours on the assignment, so don't wait until the last minute to try to complete it. Once you have figured out the PDA, implementing it as a program should not be very difficult. Your program should be at most a few hundred lines of code, where most of the lines are if or case statements.

 

You can use the stack that is built into C++, Java, or python for your PDA.  Alternatively, you can construct your own stack by using an array with a pointer indicating where the top of the stack is.  Since all of the test strings are relatively short, you can just use a fixed-size array of size, say, 100. (Better yet would be to dynamically allocate memory as needed.) If you push something onto the stack, that corresponds to incrementing the pointer by 1 and then inserting the item into the array location that the pointer is now pointing to. To pop the stack, you read the element in the array location the pointer is pointing to and then decrease the pointer by 1.