CYK ALGORTIHM, RUN TIME ERROR

Program : CYK Algorithm,
I did it using strings and vectors.
Code is compiling without any syntax errors and also asking me to Enter string
but there is something wrong in the 69th line i guess..
It is saying CYK.exe has stopped working!
can anyone please help me out with this!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
 #include<iostream>
#include<stdio.h>
#include<string.h>
#include<vector>
#include<fstream>
#include<cstddef>
using namespace std;

int main()
{
      ifstream fin;
      fin.open("CYK.txt");
      vector<string> pr;
      string s,str="";
      
      while(!fin.eof())
      {
       getline(fin,s);
       cout<<s<<endl;
       pr.push_back(s);                 
      }
      
	  int nor = pr.size();
      for(int i=0; i<nor;i++)
      {
              if(pr[i][0]=='S')
              {
                               str = pr[i];
                               break;
              }
      }
     
       if(str =="") 
       { 
              cout<<" Invalid Grammar";
              return 0;
       }
      
       char rs[nor];
       for(int i=0; i<nor;i++)
       rs[i]= pr[i][0];
   
       string temp;
       vector<string> ls[nor];
       
       size_t f;
      for(int i=0; i<nor;i++)
      {
        f = pr[i].find_first_of('|');
        temp = pr[i].substr(3,f-3);
        while(f!=std::string::npos)
        {
        	int t=f+1;
			f=pr[i].find('|',f+1);
        	temp = pr[i].substr(t,f-t);
        	ls[i].push_back(temp);
        }
     }
		
	 
      string ip;
      cout<<"Enter String = ";
      cin>>ip;
      string tempo="";
      int len = ip.length();
      temp ="";
   
      cout<<"dhjdfal";
      vector <string> TT[len];
       cout<<"dhjdfal";
 
 
 // Initilaizing the Triangle table;                      
    for(int i=0; i<len;i++ )
	{
    	for(int j=0; j<len; j++)
		{
			TT[i].push_back("");
	    }
    }
 
//caluclating the first row oF TT;    
	  for(int i=0; i<len; i++)
      {    
	          
              temp = temp + ip[i];
              for(int k=0; k<nor;k++)
              {
                
                for(int j=0; j<ls[k].size();j++)
                {      
				  if(ls[k][j].compare(temp)==0)
                  TT[0][i]=TT[0][i]+rs[k];
                }
              }
      }
 //caluclating the rest of the rows;
int x,y;

for(int i=1; i<len; i++)
{
	for(int j=0; j<len-i;j++)
	{
		int m = (TT[i-1][j].size())*(TT[i-1][j+1].size());
		vector<string> temp(m);
		int p=0;
        for(int b=0; b<TT[i-1][j].size();b++)	
        {
        	for(int n=0;TT[i-1][j+1].size();n++ )
        	{
        		temp[p]="";
        		temp[p]= temp[p]+ TT[i-1][j][b] + TT[i-1][j+1][n] ;
        		p++;
        	}
        }

       TT[i][j]="";
       for(int g=0; g<m; g++)
       {
       	for(int h=0; h<nor; h++ )
       	{
       		for(int u=0; u<ls[h].size();u++)
       		{
       			if(temp[g].compare(ls[h][u])==0)
       			TT[i][j] = TT[i][j] + rs[h] ;
       		}
        }
	   }

	x=i;
	y=j;
	}
	
}
 
 if(TT[x-1][y-1]=="")
 	cout<<"The given input string is NOT in the grammar";

 else
   cout<<"Congrats! The input string belongs to the grammar" ;   
      
   return 0;  
}         
Last edited on
Line 69 you create an array of len vectors. Each vector will be empty so on line 78 you try to access elements that doesn't exists. Before you access an element in a vector you have to make sure it has that many elements.

You can use push_back to add one element
1
2
// Adds an empty string to the end of vector TT[i]
TT[i].push_back("");


Or you might want to use resize.
1
2
// Sets the size of vector TT[i] to len (All new string elements will be empty)
TT[i].resize(len);
Last edited on
@peter87 Thanks a lot for your help.
I've rectified it.
But my problem is not solved yet.
still getting the same error.
Note that the cout statement in line 70 is NOT executing!!
Kindly help
How do you know that it's not executing? You don't flush cout so it might very well run even though you see no output. Use one of the following lines instead.
1
2
3
cout<<"dhjdfal"; cout.flush();
cout<<"dhjdfal" << flush;
cout<<"dhjdfal" << endl;  // This will also print a newline character. 
I used one of those lines as you said and I tried all three of them and I'm seeing no output of that 70th line.
May be according to you that cout statement is executing but not showing output. okay!
But where is the problem now?
Why is my program not executing??
What is the problem now??
I've done everything as you told :\
HELP!!
When I run your program I don't get past the first loop (line 16-21) because I don't have the file CYK.txt. Does it give any output at all for you? Maybe you should add a few more cout statements (with flushing) or simply use a debugger to find out where things go wrong.
http://www.cplusplus.com/forum/general/112111/


> vector <string> TT[len];
illegal, the size of the array must be constant.


Also, your file reading is incorrect. Loop against the reading operation instead
16
17
      while( getline(fin, s) )
       pr.push_back(s);
just create a text file CYK.txt in the same directory in which you downloaded this code and write the following in the file

S->AB|BC
A->BA|a
B->CC|b
C->AB|a

and save it.
and yes it gives me output of the file contents as i asked and also asks me to "Enter the string" .
and after i enter the string.
it gives the output as dhjdfal and hungs.. help!!
Last edited on
> and after i enter the string.
¿what string?


read the link

If your program hung, you may terminate it. The debugger should show what was executing.
Last edited on
changed the 69 line as
 
vector<string> TT[100];


and

1
2
3
while(getline(fin,s))
pr.push_back(s);


still same problem.
HELP!!!
Topic archived. No new replies allowed.