Char Array Augmented Grammar For LR0 Parser Question

Greetings,

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.

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
145
146
147
148
149
150
151
152
153
int main() 
{ 
    char terminals[10][10] = {}; 
    char production[10][10] = {};
    char production2[10][10] = {};
    vector <char> v;

    bool product = false;
    ifstream inFile;
    string strFilename;
    string strword;
    cout << "enter file name" << endl;
    cin >> strFilename;
    
    while (inFile.fail()) //While the file does not open display error message and asks for another file name and location
	{
		cout << "Input file error!\n";
		cout << "Please enter the data file name (with location): ";
		cin >> strFilename;
	}

	inFile.open(strFilename.c_str()); //Opens file
	int i = 0;
	int j = 0;
	while (inFile >> strword) //While the file is copying into the word
	{
	    if (strword == "$")//if a dollar sign is present then we start saving the cahracters in our production array
	    {
	        product = true;
	    }
	    if (product == false)//else we save the poroductions in the terminal array
	    {
	        strcpy(terminals[i], strword.c_str());
	        i++;
	    }
	    else if (product == true && strword != "$")
	    {
	        
	        strcpy(production[j], strword.c_str());
	        j++;
	    }
    
	}
	inFile.close(); //Close the input file
	
    for(int i = 0; i < 10; i++)//loop to insert . at the begining of each rpoduction: E->.TR
	{
	    //int j = 0;
	    if (production[i][0] == '\0')
        {}
	    for(int j = 0; j<10; j++)
	    {
	        if (production[i][0] == '\0')
            {}
	        else if(production[i][j] == '>')
	        {
	            for(int a=9; a>j; a--)
	            {
		            production[i][a]=production[i][a-1];
	            }
	            production[i][j+1]='.';
	        }
	    }
	}
		
	int a = 0;
	for(int i = 0; i < 9; i++)//begin creation of augmented grammar
	{
		for(int j = 3; j <10; j++)
		{
			if(production[i][j-1] == '.')
			{
				for(int b=0; b < 10; b++)
	        	{
		    		production2[a][b]=production[i][b];
	        	}
	    		a++;

				int num = j;
				for(int b = 1; b <= 3; b++)
				{
					if(production2[b-1][num] == '\0')
					{
						break;
					}
					else
					{
						for(int a = 0; a < 10; a++)
						{
							if(a == num-1)
							{
								a++;
							}
							if(a==num+1)
							{
								v.push_back('.');
							}
							char x =production2[b-1][a];
							if(a>=0)
							{
								v.push_back(x);
							}
						}
						for (int c = 0; c < v.size(); c++)
						{
							production2[b][c] = v[c];
						}
						num++;
						a++;
						v.clear();
					}

				}
			}
		}
	}
	
	
	for (int i = 0; i < 10; i++) 
    {
        if (production[i][0] == '\0')
        {}
        else
        {
            for (int j = 0; j < 10; j++)
            {
                cout << production[i][j];//print all of the productions
            }
            cout << endl;
        }

    }
	
	cout << endl;

	
	for (int i = 0; i < 10; i++) 
    {
        if (production2[i][0] == '\0')
        {}
        else
        {
            for (int j = 0; j < 10; j++)
            {
                cout << production2[i][j];//print all of the augmented productions
            }
            cout << endl;
        }

    }
    cout << "end";
    return 0; 
}


output:
E->.TR
R->.+TR
T->.FY
Y->.*FY
F->.(E)

E->.TR
E->T.R
E->TR.
R->.+TR
T->.FY
Y->.*FY
F->.(E)


expected output:
E->.TR
R->.+TR
T->.FY
Y->.*FY
F->.(E)

E->.TR
E->T.R
E->TR.
R->.+TR
R->+.TR
R->+T.R
R->+TR.
T->.FY
T->F.Y
T->FY.
Y->.*FY
Y->*.FY
Y->*F.Y
Y->*FY.
F->.(E)
F->(.E)
F->(E.)
F->(E).

Last edited on
1
2
3
4
char production[10][10];
for (int i = 0; i < 15; i++) 
   for (int j = 0; j < 10; j++)
      production[i][j+1];
out of bounds.
Last edited on
Corrected the out of bound errors. Thank you for pointing them out. Original problem still persists.
Last edited on
provide your input file
apologies. input is a text file

input.txt
1
2
3
4
5
6
7
8
9
10
11
12
+
*
(
)
$
E->TR
R->+TR
T->FY
Y->*FY
F->(E)
F->i
$
Last edited on
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
				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.
Topic archived. No new replies allowed.