我正在研究的程序是一个下推自动机,它只接受^ nb ^ n的输入:$ ab,$ aabb,$ aaabbb等。关于它的一切似乎都有效,除了事实是在主while循环的4次迭代之后,它得到分段错误。我认为这与我在每个循环中将堆栈复制到另一个堆栈这一事实有关,但我不明白为什么它不会在每次迭代时都出错。非常感谢任何帮助!
#include <iostream>
#include <stack>
#include <string>
using namespace std;
int main()
{
cout << "Enter an expression: ";
string expression;
getline(cin, expression);
stack<char> expressionStack;
stack<char> unreadStack;
stack<char> expressionStackPOP;
stack<char> unreadStackPOP;
char expressionOutput;
char readInput;
char transitionState = 'p';
int step=0;
int counter;
int trule = 0;
int rrule = 0;
char expressionO;
char unreadO;
//Enter expression into Stack<char> unreadInput
for(unsigned int i = 0; i<expression.length(); i++)
{
unreadStack.push(expression[i]
);
}
counter = expression.length();
cout << "Got passed putting the expression in unreadStack" << endl;
cout <<"Step"<< "\tState" << "\tUnread Input" << "\t\tStack" <<"\t\tΔ Rule used " << "\tR rule used "<< endl;
while(!unreadStack.empty())
{
switch(transitionState)
{
case 'p':
expressionStack.push('S');
transitionState = 'q';
trule = 1;
//cout << "Changed state to P and pushed S into stack" << endl;
break;
case 'q':
if(unreadStack.top() == 'a' && transitionState=='q')
{
readInput = unreadStack.top();
unreadStack.pop();
transitionState = 'a';
trule = 2;
//cout << "Changed state to qa and popped a from unread stack" << endl;
}
if(unreadStack.top() == 'b' && transitionState=='q')
{
readInput = unreadStack.top();
unreadStack.pop();
transitionState = 'b';
trule = 3;
//cout << "Changed state to qb and popped b from unread stack"<<endl;
}
if(unreadStack.top() == '$' && transitionState=='q')
{
readInput = unreadStack.top();
unreadStack.pop();
transitionState = '$';
trule = 4;
//cout << "This is the end" << endl;
}
break;
case 'a':
//cout << "in case a" << endl;
if(readInput=='a' && transitionState=='a')
{
expressionStack.pop();
transitionState='q';
trule = 5;
//cout << "Changed state to q and popped a from stack" << endl;
}
if(readInput=='S' && transitionState=='a')
{
expressionStack.pop();
expressionStack.push('b');
expressionStack.push('S');
expressionStack.push('a');
transitionState='q';
trule = 6;
//cout << "Changed state to q and popped S from stack and pushed aSb into stack" << endl;
}
break;
case 'b':
if(readInput=='b' && transitionState=='b')
{
expressionStack.pop();
transitionState='q';
trule = 7;
//cout << "Changed state to q and popped b from stack" << endl;
}
if(readInput=='S' && transitionState=='b')
{
expressionStack.pop();
transitionState='b';
trule = 8;
//cout << "Changed state to qb and popped S from stack" << endl;
}
break;
case '$':
cout<<"test"<<endl;
break;
trule = 9;
}
//Output RIGHT HERE THIS IS THE SPOT-----------------------------------------------------
expressionStackPOP = expressionStack;
unreadStackPOP = unreadStack;
//--------------------------------------------------------------
if (step<=9)
cout<<" ";
cout <<step<<"\t"<<transitionState<<"\t";
while(!unreadStackPOP.empty())//copy unreadStackPOP stack to string
{
cout<<unreadO;
unreadO = unreadStackPOP.top();
unreadStackPOP.pop();
}
cout<<"\t\t\t";
while(!expressionStackPOP.empty())//copy expressionStackPOP stack to string
{
cout<<expressionO;
expressionO = expressionStackPOP.top();
expressionStackPOP.pop();
}
cout<<"\t\t"<<trule<<"\t\t"<<rrule<<endl;
step++;
}
}
答案 0 :(得分:1)
此代码:
if(unreadStack.top() == 'a' && transitionState=='q')
{
readInput = unreadStack.top();
unreadStack.pop();
transitionState = 'a';
trule = 2;
//cout << "Changed state to qa and popped a from unread stack" << endl;
}
if(unreadStack.top() == 'b' && transitionState=='q')
{
如果在此代码开头,unreadStack仅包含'a'
,请考虑会发生什么。
第二个if
语句将尝试做什么?