This is a code that I actually wrote in C and then changed to C++. It takes a NFA as input and translates it to productions of regular grammar.
Code:
#include <cstdio>
#include <cstdlib>
#include <iostream>
using namespace std;
int main(void)
{
char *state = 0, *sym = 0, c, ns[10][5][5];
state = new char [3];
sym = new char [3];
int statenos, symnos, counter[10][5], i, j, k=0, finstateflag[10];
cout<<"Enter number of states required: ";
cin>>statenos;
if ( statenos > 11 )
{
cout<<"Overflow!";
exit(0);
}
cout<<"Enter name of initial state: ";
cin>>state[0];
for (i=0; i < statenos; i++)
{
finstateflag[i] = 0;
}
for ( i = 1; i < statenos; i ++ )
{
cout<<"\nEnter name of state "<<i+1<<" : ";
cin>>state[i];
cout<<"If it is a final state press Y, else press N: ";
cin>>c;
switch(c)
{
case 'Y':
finstateflag[i] = 1;
break;
case 'y':
finstateflag[i] = 1;
break;
case 'N':
finstateflag[i] = 0;
break;
case 'n':
finstateflag[i] = 0;
break;
default:
cout<<"Wrong input! Aborted!";
exit(0);
}
}
cout<<endl<<"Enter number of symbols: ";
cin>>symnos;
if ( symnos > 10 )
{
cout<<"Overflow!!";
exit(0);
}
for ( i = 0; i < symnos; i++ )
{
cout<<"Enter symbol "<<i+1<<" : ";
cin>>sym[i];
}
for (i = 0; i < 10; i++)
{
for (j = 0; j < 5; j++)
{
counter[i][j] = 0;
for (k = 0; k < 5; k++)
{
ns[i][j][k] = '0';
}
}
}
for ( i = 0; i < statenos; i++ )
{
if (finstateflag[i] == 1)
continue;
else
{
for ( j = 0; j < symnos; j++ )
{
k = 0;
do
{
cout<<"Enter NS for "<<state[i]<<" when "<<sym[j]<<" is entered: ";
cin>>c;
if ( c != '0')
{
ns[i][j][k] = c;
k++;
counter[i][j] = k;
}
else
break;
} while ( c != '0');
}
}
}
for ( i = 0; i < statenos; i++ )
{
if (finstateflag[i] == 1)
{
cout<<endl<<state[i]<<" -> lamda";
continue;
}
else
{
for ( j = 0; j < symnos; j++ )
{
k = counter[i][j] - 1;
while ( k >= 0 )
{
cout<<endl<<state[i]<<" -> "<<sym[j]<<ns[i][j][k];
k--;
}
}
}
}
getchar();
}
I want to create a Class for States with a two-dimensional vector in it for NS. However, the size of NS is an unknown and will be entered by the user. So, how am I supposed to initialize the vector? Also, the number of objects of class States depends on the user's input. Am I supposed to create a pointer of objects? Can anybody help me on its implementation? Just a few tips would do.