SE307 - Computational Complexity Theory
Department of Computer Science
National University of Ireland, Maynooth
T Naughton, NUIM
Back to SE307 home
Lab 5 - Mechanical reasoning about machines
In this lab you will continue working on the concept of using a machine to analyse another machine. You will build on your solution to the previous lab's problem.
Problem 5.A Construct a machine that takes a function and a variable name as inputs and checks if (after having been declared) that variable ever changes value during execution of the function (i.e. if you ran the function, would the variable ever have a nonzero value?). All input functions will conform to the restricted structure described in Lab 4. The machine outputs a `T' if the variable is changed, and an `F' otherwise. If the variable is not declared in the function, the machine outputs an `F'.
Hand up your solutions to this problem at the beginning of next Tuesday's class test (14 November 2000).
If you chose to use the C++ MachineBase abstract base class given in the previous lab, here is another derived class MachineFiveA that you may find useful. MachineFiveA acts as an interpreter of the input machine passed to it, printing the values of variables as each line of code is simulated. It requires the VarList class to keep a dynamic list of variables. Once again, I include a driver program lab5a.cpp that creates a MachineFiveA object. You can use the sample input machines from last week.
Note: MachineFiveA naively (you could say stupidly!) simulates the input machine (just give it a machine with an infinite loop!). You'll have to modify MachineFiveA in order to solve Problem 5.A.
Your class definition might then look something like this (after stripping out most of the comments):
class MachineFiveA : public MachineBase {
protected:
VarList vars; // list of variables in input machine code
string changedvar; // name of a variable in the input machine
public:
MachineFiveA(string fname, string vname); // constructor
virtual ~MachineFiveA(); // destructor
private:
virtual void ProcessDeclaration(); // process a declaration
virtual void ProcessStatement(int level); // process a statement
};
Your constructor might look something like this:
MachineFiveA::MachineFiveA(string fname, string vname)
:MachineBase(fname) {
/* Constructor. Call base class constructor. Initialise
** derived class data members.
*/
changedvar = vname;
// VarList has its own constructor
}
Your updated driver program might look something like this:
#include<iostream>
#include<string>
#include "machine5a.h"
void main(void) {
string fname("lab4input1.txt"); // input filename
string vname("ab"); // variable name
MachineFiveA mymachine(fname, vname); /* create a machine and pass
** the name of a file
** containing the input machine
** and the name of a variable
** in the input machine.
*/
// analyse the input machine
if (mymachine.Analyse()) {
cout << "\nT" << endl;
} else {
cout << "\nF" << endl;
}
}