infinite loop with <stack> header

hi , this is my first post here . I've wrote a program that uses <stack> header and I need to use a while operation in my code . the problem is when I use stack header operations like stack.pop() in the while it gives me errors when I run the program(it compiles without errors) . for example the code below gives me error :
"Debug assertion failed!
Expression: deque empty before pop"
the code that gives me this error is:
while(true){
stack.pop();
break;
}
anyone knows what's the problem .
thanks
here's the complete code:

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <string>
#include <stack>
using namespace std;
#include <cctype>
#include <ctype.h>


//--------------------------------------------------------
//--------------------------------------------------------
char Action[6][6];
char Action2[6][6];
//----Grammer Rules
char arr1[]={'S','E','E'};
char arr2[]={'E','(','q'};
int Numfunc(char a){
int c;
switch(a){
case '0':
c=0;break;
case '1':
c=1;break;
case '2':
c=2;break;
case '3':
c=3;break;
case '4':
c=4;break;
case '5':
c=5;break;
}
return c;
}
char actionfunc(char a,char b){
int c;
int d;
switch(b){
case 'q':
c=0;break;
case '(':
c=1;break;
case ')':
c=2;break;
case '$':
c=3;break;
case 'S':
c=4;break;
case 'E':
c=5;break;
}
switch(a){
case '0':
d=0;break;
case '1':
d=1;break;
case '2':
d=2;break;
case '3':
d=3;break;
case '4':
d=4;break;
case '5':
d=5;break;
}

return Action[d][c];
}
char actionfunc2(char a,char b){
int d;
int c;
switch(b){
case 'q':
c=0;break;
case '(':
c=1;break;
case ')':
c=2;break;
case '$':
c=3;break;
case 'S':
c=4;break;
case 'E':
c=5;break;
}
switch(a){
case '0':
d=0;break;
case '1':
d=1;break;
case '2':
d=2;break;
case '3':
d=3;break;
case '4':
d=4;break;
case '5':
d=5;break;
}
return Action2[d][c];
}

int main ()
{
int k=0;
stack<char> mystack;
stack<char> stack;
//-----<initializing parse table Action>---
Action[0][0]='S';
Action[0][1]='S';
Action[0][2]='E';
Action[0][3]='E';
Action[0][4]='E';
Action[0][5]='S';
Action[1][0]='E';
Action[1][1]='E';
Action[1][2]='E';
Action[1][3]='A';
Action[1][4]='E';
Action[1][5]='E';
Action[2][0]='S';
Action[2][1]='S';
Action[2][2]='E';
Action[2][3]='E';
Action[2][4]='E';
Action[2][5]='S';
Action[3][0]='E';
Action[3][1]='E';
Action[3][2]='R';
Action[3][3]='R';
Action[3][4]='E';
Action[3][5]='E';
Action[4][0]='E';
Action[4][1]='E';
Action[4][2]='S';
Action[4][3]='E';
Action[4][4]='E';
Action[4][5]='E';
Action[5][0]='E';
Action[5][1]='E';
Action[5][2]='R';
Action[5][3]='R';
Action[5][4]='E';
Action[5][5]='E';
//-----</initializing parse table Action>--
//-----<initializing parse table Action2>--
for(int i=0;i<6;i++){
for(int j=0;j<6;j++){
Action2[i][j]='0';
}
}
Action2[0][0]='3';
Action2[0][1]='2';
Action2[0][5]='1';
Action2[2][0]='3';
Action2[2][1]='2';
Action2[2][5]='4';
Action2[3][2]='2';
Action2[3][3]='2';
Action2[4][2]='5';
Action2[5][2]='1';
Action2[5][3]='1';
//-----</initializeing parse table Action2>
char str[500];
mystack.push('$');
cout<<"inter your lexeme: ";
cin>>str;
while(str[k]!=';'){
mystack.push(str[k]);
k++;
}
cout<<"Popping out elements...";
while(!mystack.empty()){
cout<<" "<<mystack.top();
mystack.pop();
}
cout<<endl;
//-------------------
//-------------------
stack.push('$');
stack.push('0');
while(true){
stack.push(mystack.top());
mystack.pop();
break;
char var1=stack.top();//)
stack.pop();
char var2=stack.top();//0
stack.push(var1);
//-----Shift condition------
if(actionfunc(var2,var1)=='S'){
cout<<"Shift Condition"<<endl;
char Num=actionfunc2(var2,var1);//2
stack.push(Num);
break;
}
//----Reduce condition------
else if(actionfunc(var2,var1)=='R'){
mystack.push(var1);
char Num2=actionfunc2(var2,var1);
while(stack.top()!=arr2[Numfunc(Num2)]){
stack.pop();
}
stack.pop();
stack.push(arr1[Numfunc(Num2)]);
//------
var1=stack.top();
stack.pop();
var2=stack.top();
stack.push(var1);
Num2=actionfunc2(var2,var1);
stack.push(Num2);
}
//----Error condition-------
else if(actionfunc(var2,var1)=='E'){
cout<<"Error";
break;
}
//----Accept condition------
else if(actionfunc(var2,var1)=='A'){
cout<<"lexeme is correct";
break;
}
//---Else-------------------
}

return 0;
}
//-----------------------
//-----------------------
and when I run the complete code it gives me no error in the compilation but gives me another error in the run :
"Debug Assertion Failed!
Program:f:\C++\test5\test4\Debug\test4.exe
File:j\program files\microsoft visual studio 9.0\vc\include\deque
Line: 202
Expression: ("_Myoff + _Off <= ((_Mydeque*)(this->Getmycont()))->_Myoff+((_Mydeque*)(this->_Getmycont()))->_Mysize&&_Myoff+ _Off >= ((_Mydeque*)(this->_Getmycont()))-> _Myoff",0)
Here it is formatted. Please use code tags in the future and ensure that what you copy and paste is properly formatted.

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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
#include <string>
#include <stack>
using namespace std;
#include <cctype>
#include <ctype.h>


//--------------------------------------------------------
//--------------------------------------------------------
char Action[6][6];
char Action2[6][6];
//----Grammer Rules
char arr1[]={'S','E','E'};
char arr2[]={'E','(','q'};
int Numfunc(char a){
	int c;
	switch(a){
case '0':
	c=0;break;
case '1':
	c=1;break;
case '2':
	c=2;break;
case '3':
	c=3;break;
case '4':
	c=4;break;
case '5':
	c=5;break;
	}
	return c;
}
char actionfunc(char a,char b){
	int c;
	int d;
	switch(b){
case 'q':
	c=0;break;
case '(':
	c=1;break;
case ')':
	c=2;break;
case '$':
	c=3;break;
case 'S':
	c=4;break;
case 'E':
	c=5;break;
	}
	switch(a){
case '0':
	d=0;break;
case '1':
	d=1;break;
case '2':
	d=2;break;
case '3':
	d=3;break;
case '4':
	d=4;break;
case '5':
	d=5;break;
	}

	return Action[d][c];
}
char actionfunc2(char a,char b){
	int d;
	int c;
	switch(b){
case 'q':
	c=0;break;
case '(':
	c=1;break;
case ')':
	c=2;break;
case '$':
	c=3;break;
case 'S':
	c=4;break;
case 'E':
	c=5;break;
	}
	switch(a){
case '0':
	d=0;break;
case '1':
	d=1;break;
case '2':
	d=2;break;
case '3':
	d=3;break;
case '4':
	d=4;break;
case '5':
	d=5;break;
	}
	return Action2[d][c];
}

int main ()
{
	int k=0;
	stack<char> mystack;
	stack<char> stack;
	//-----<initializing parse table Action>---
	Action[0][0]='S';
	Action[0][1]='S';
	Action[0][2]='E';
	Action[0][3]='E';
	Action[0][4]='E';
	Action[0][5]='S';
	Action[1][0]='E';
	Action[1][1]='E';
	Action[1][2]='E';
	Action[1][3]='A';
	Action[1][4]='E';
	Action[1][5]='E';
	Action[2][0]='S';
	Action[2][1]='S';
	Action[2][2]='E';
	Action[2][3]='E';
	Action[2][4]='E';
	Action[2][5]='S';
	Action[3][0]='E';
	Action[3][1]='E';
	Action[3][2]='R';
	Action[3][3]='R';
	Action[3][4]='E';
	Action[3][5]='E';
	Action[4][0]='E';
	Action[4][1]='E';
	Action[4][2]='S';
	Action[4][3]='E';
	Action[4][4]='E';
	Action[4][5]='E';
	Action[5][0]='E';
	Action[5][1]='E';
	Action[5][2]='R';
	Action[5][3]='R';
	Action[5][4]='E';
	Action[5][5]='E';
	//-----</initializing parse table Action>--
	//-----<initializing parse table Action2>--
	for(int i=0;i<6;i++){
		for(int j=0;j<6;j++){
			Action2[i][j]='0';
		}
	}
	Action2[0][0]='3';
	Action2[0][1]='2';
	Action2[0][5]='1';
	Action2[2][0]='3';
	Action2[2][1]='2';
	Action2[2][5]='4';
	Action2[3][2]='2';
	Action2[3][3]='2';
	Action2[4][2]='5';
	Action2[5][2]='1';
	Action2[5][3]='1';
	//-----</initializeing parse table Action2>
	char str[500];
	mystack.push('$');
	cout<<"inter your lexeme: ";
	cin>>str;
	while(str[k]!=';'){
		mystack.push(str[k]);
		k++;
	}
	cout<<"Popping out elements...";
	while(!mystack.empty()){
		cout<<" "<<mystack.top();
		mystack.pop();
	}
	cout<<endl;
	//-------------------
	//-------------------
	stack.push('$');
	stack.push('0');
	while(true){
		stack.push(mystack.top());
		mystack.pop();
		break;
		char var1=stack.top();//)
		stack.pop();
		char var2=stack.top();//0
		stack.push(var1);
		//-----Shift condition------
		if(actionfunc(var2,var1)=='S'){
			cout<<"Shift Condition"<<endl;
			char Num=actionfunc2(var2,var1);//2
			stack.push(Num);
			break;
		}
		//----Reduce condition------
		else if(actionfunc(var2,var1)=='R'){
			mystack.push(var1);
			char Num2=actionfunc2(var2,var1);
			while(stack.top()!=arr2[Numfunc(Num2)]){
				stack.pop();
			}
			stack.pop();
			stack.push(arr1[Numfunc(Num2)]);
			//------
			var1=stack.top();
			stack.pop();
			var2=stack.top();
			stack.push(var1);
			Num2=actionfunc2(var2,var1);
			stack.push(Num2);
		}
		//----Error condition-------
		else if(actionfunc(var2,var1)=='E'){
			cout<<"Error";
			break;
		}
		//----Accept condition------
		else if(actionfunc(var2,var1)=='A'){
			cout<<"lexeme is correct";
			break;
		}
		//---Else-------------------
	}

	return 0;
}
Here is the first question that the program asked.
inter your lexeme: hello

I entered hello. I have no idea what a lexeme is so I don't have the slightest clue what to enter. So I just entered hello. The following while loop crashes because you are searching for a ; that obviously does not exist within the string. What are you trying to do here and how do you know that the user will enter a ';'?
1
2
3
4
5
6
7
8
char str[500];
	mystack.push('$');
	cout<<"inter your lexeme: ";
	cin>>str;
	while(str[k]!=';'){
		mystack.push(str[k]);
		k++;
	}
Look at line 185. Now why would you call the top member function on a stack (mystack) that you just finished emptying? What do you think will happen when you call top on an empty stack object?
Line 185: stack.push(mystack.top());

There is nothing on mystack because you just emptied it. Then you try to put its top element onto another stack.
yeah , thank all u guys . all the errors was because I wanted to locate the top of a stack that was already was empty !
Topic archived. No new replies allowed.