CYK Algorithm, Problem in Vector<strings>


Problem in the for loop starting on 68th line of code. Please kindly rectify!!


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
144
#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
What's the problem? Does it fail to compile? Does it attract rabbid squirrels into your house? Does it make a sound like a dog whistle? Does the FBI knock on your door every time you run it? You need to explain what the problem is.
It runs with no syntax errors but fails to compile..
it gives the "Enter the String" output.
but after entering the string the compiler just hangs up saying CYK.exe not working properly. kind of semantic error may be.. kindly please help
If it fails to compile, there has to be a syntax or linker error, thus it cannot be run.

If it gives output, it is running.

The compiler is not what freezes, it is your program.

Line 66 is invalid syntax - you can't make an array on the stack from a variable which is not a compile-time constant.

Scroll to the bottom:
http://ideone.com/MU6Cfs
I have edited the program
and tried to run it but still a run time error.
kindly help!
what should I do to not make an array on the stack from a variable which is not a compile time constant?
I've changed the line 69 as
 
vector <string> TT[100];


but still receiving same error!
and the cout statement of 70 line is not executing!!
Last edited on
I would normally continue to help but because you so rudely private messaged me I will not spare the kindness to continue helping someone as impatient and ungrateful as you.

http://www.cplusplus.com/forum/beginner/121893/
Last edited on
what should I do to not make an array on the stack from a variable which is not a compile time constant?


The size of the array must be a compile time constant. If you wish to use a variable that is not a compile time constant, use a vector instead.

For instance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>

int main()
{
    unsigned num_lines;
    std::cout << "Enter the number of lines.\n> ";
    std::cin >> num_lines;

    const unsigned width = 79;
    std::vector<std::string> lines(num_lines);  // creates a vector of num_lines default constructed strings.

    for (unsigned i = 0; i < lines.size(); ++i)
        lines[i] = std::string((width / 2 ) - (i * 2 + 1)/2, ' ') + std::string(i * 2 + 1, '*');
    
    std::cout << '\n' ;
    for (unsigned i = 0; i < lines.size(); ++i)
        std::cout << lines[i] << '\n';
}


http://ideone.com/RdBslj
Last edited on
Topic archived. No new replies allowed.