CS301
Foundations of Computer Science

Spring 2002
Department of Computer Science
Link to Colorado State University Home
 Page


Homework Assignment 10: Simulating a Pushdown Automata

For this assignment you are to write code to simulate a pushdown automata based on a single stack as described in Chapter 8 of our text book. Use code examples you can find on the net, such as the Java code provided by Susan Rodger. Your program's input and output must be character-based; graphical displays like those in Rodger's code are not required. You may use any programming language you choose, but I strongly suggest you use Java, because Susan Rodger's collection of Java code includes PDA simulation code! Be sure you acknowledge the source of any code you use in the comments in your code.

Your code should read in the specification of a PDA using a format demonstrated by the following, which represents the PDA shown on page 230 of the text. Lines that start with // are to be interpreted as comments by your code.

//accepting states
q0 q1
//delta function.  First three tokens are the input state, input, and stack symbol,
// and the next two are the resulting state and stack symbol to be pushed on the stack.
q0 a null ->  q0 A
q0 b A    ->  q1 null
q1 b A    ->  q1 null
Here is another example from page 231:
//accepting states
q1
//delta function
q0 a null -> q0 A
q0 b null -> q0 B
q0 c null -> q1 null
q1 a A    -> q1 null
q1 b B    -> q1 null
The symbol -> is required. Your input method must simply skip this token.

Your code should read the specification of one PDA from a file whose name is given as a command line argument. Once the PDA is read in, your code must read from standard input a string of symbols from the alphabet of the PDA. Your code must process the input string by simulating the operation of the PDA. Print out the configurations resulting from each transition. The result will look like the following for processing the string aabb using the first PDA shown above. This output is just what is shown on the right side of the figure on page 230.

   [q0, aabb, null]
-> [q0, abb, A]
-> [q0, bb, AA]
-> [q1, b, A]
-> [q1, null null]
ACCEPT
After processing one string, wait for another string to be entered to standard input. If no more strings are entered, terminate. So, the interaction with your program would look like this, if you used Java and your main class is named PDASim and example1.pda is the name of the PDA file:
% cat example1.pda
//accepting states
q0 q1
//delta function.  First three tokens are the input state, input, and stack symbol,
// and the next two are the resulting state and stack symbol to be pushed on the stack.
q0 a null ->  q0 A
q0 b A    ->  q1 null
q1 b A    ->  q1 null

% java PDASim example1.pda
Enter string to be processed:  aabb
   [q0, aabb, null]
-> [q0, abb, A]
-> [q0, bb, AA]
-> [q1, b, A]
-> [q1, null null]  
ACCEPT
p
Enter string to be processed:  abb
   [q0, abb, null]
-> [q0, bb, A]
-> [q1, b, null]    
REJECT

Use your program to produce answers for 1c, 1d, 3c, 3h, 12, 14c, and 15a. If you choose to earn extra credit on this assignment, you may write code to simulate a nondeterministic PDA and solve exercises 2b, 2c, 2d. In the exercises, "tracing the computation" means producing the configuration output at each transition as shown above. For exercises 3 and 12, show the configuration output for at least two strings in the language and two strings not in the language.

Required Files

Once you complete this assignment, you will have files for the source code, multiple PDA specification files, a makefile for compiling your code and for running your code for all of the required exercises. Here is an example of a makefile and an almost empty pdasim.C. Download both, then do make pdasim, followed by make answers. (Note that copying and pasting the makefile text will not preserve the required tabs. Instead, use your browser to "Save Link As..." to download it.) Also include a README file describing how to compile and run your code. Your README file must also describe the source of your code, such as where you found it on the net and what parts that you wrote or modified.

Checking in Your Solution

To check in your code, create one tar file with all of your files and check this file in. For example, if your current directory is where you have all code and files for this assignment, do
tar cvf assignment10.tar *.java makefile *.pda README 
~cs301/bin/checkin assignment10.tar
After you check in your code to us, we should be able to do
tar xvf assignment10.tar
make PDASim
make answers
and see your program's output for all of the exercises listed above. You may test this by copying your tar file into a temporary directory that only contains that tar file. Then run the above commands.

Extra Credit

To earn extra credit, implement a nondeterministic PDA and use it to answer exercises 2b, 2c, and 2d.