I am creating a program that will find the canonical LR0 sets for a given grammar.
To start I have 2 char arrays, production, and production2. production is simply the grammar imported from a text file. production2 is the augmented grammar with a "." shifted throughout each production: see expected output.
My problem occurs when iterating through production to create the augmented production 2 starting at line 66 to line 117.
I believe I screwed up my logic somewhere as the loop does not process the correct output.
I will try to solve the problem on my own, but hopefully it is a simple solution. Any assistance would be greatly appreciated.
My code below is what I currently have for augmenting my grammar:
See line 66 for the loop in question.
int num = j;
for(int b = 1; b <= 3; b++) //¿why range [1:3]?
{
if(production2[b-1][num] == '\0')
{
break;
}
else
{
for(int a = 0; a < 10; a++) //hiding `a' that gived you production2.size
{
if(a == num-1)
{
a++;
}
if(a==num+1)
{
v.push_back('.');
}
char x =production2[b-1][a];
if(a>=0) //tautology
{
v.push_back(x);
}
}
for (int c = 0; c < v.size(); c++)
{
production2[b][c] = v[c];
}
num++;
a++;
v.clear();
}
}
you are only operating on production2[0], production2[1] and production2[2] over and over again.
the logic is unnecessarily complicated for just moving a dot.
say that s is a std::string that has a dot
1 2 3 4 5 6 7 8 9 10
int position = s.find('.');
ss = s;
augmented_grammar.push_back(ss);
while(position < s.size()-1) {
next = ss;
swap(next[position], next[position+1]); //move the '.' to the right
++position;
ss = next;
augmented_grammar.push_back(ss);
}
73 74 75
for (int b = 0; b < 10; b++) {
production2[a][b] = production[i][b];
}
`a' reaches 12, out of bounds.
production2 is not big enough to store the augmented grammar
Thenk you for pointing out the production 2 error. After talking to some other people i was told to use a vector of strings and just use the swap. Thank you again.