Compiler Construction compaction code in c++

this code is reading file which will be given randomnly by our teacher which contains in first line line rows in second line columns and then begins transition table for dfa
then after reading it is stored dynamically in 2d array
then function init creates another array of 128 columns and initializes with -1 at start
then passing 2d array and storing it in this new array
then i have to perform compaction on this array which means columns having same values should be made one
after compaction do traversal of this array
and read another file which contains input string which will be given randomnly by our teacher to generate lexemes tokens
in first row there are ascii values of characters on keyboard and at 128 columns last index accepted state is stored with some number like 101 and rejected state with -1

file is not reading correctly plus i don;t know how to do compaction
i have to check row and column number from file and then on its basis dynamically create an array how to read file and do compaction?


#include "iostream"
#include "fstream"
#include "string"
#define c 6

using namespace std;
void init(int *[],int);//Fuction Prototype

int main()
{
int line=0;
string l;
int rows=5;
int cols=6;
int count=0;

int **a=new int*[rows];// Dynamic array creation

for (int k=0;k<rows;k++)
{
a[k]=new int[cols];
}
ifstream myfile ("23.txt"); // Reading file

if(myfile.is_open())
{
while(!myfile.eof() )
{
myfile>>l;
cout << l;


if ((l =="\n")||(l=="\r"))
{
line++;
cout << endl;

}

if (line ==2)
{

for (int i=0;i<rows;i++)//storing transition table in dynamically creaed array

{
for(int j=0;j<cols;j++)
{
a[i][j]=(int)l[j];
cout << l[j] << '\t';
count++;
}
}

}
}


init(a,count);
myfile.close();//close the file
}

else
cout << "Unable to open file";

return 0;
}

void init(int **a1, int rows)//function to save read data into newly created array of 128 columns
{


int **board ;
board = new int*[1];

for (int i = 0; i < 1; ++i) {
board[i] = new int[129];

}

for (int k=0;k<1;k++)
{
for (int l=0;l<129;l++)
{
board[k][l]=-1;
}
}

for (int i=0;i<rows;i++)
{
for (int j=0;j<1;j++)
{
board[i][j]=a1[i][j];

}
}

//compaction(board,rows);
}
/*void compaction(int a2[][c],int r2)
{
for(int i=0;i<r2;i++)
{
for(int j=0;j<c;j++)
{
if(a2[i][j] == a2[i][j+1])
{

}
}
}
}*/
Last edited on
hint... if you read the file in transposed, you can check rows with a memcmp and avoid the double loop and having to track the state of the comparisons in the inner loop. Its a lot simpler that way.


Topic archived. No new replies allowed.