2016-05-05 13 views
0

私が取り組むプログラムは、^ 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

少なくとも、expressionStack.empty()のチェックは、このコードのかなりの部分で役に立つと思われるようです。 – WhozCraig

答えて

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'が含まれている、とどうなるか考えてみましょう。

第2のifの声明は何を試みますか?

+0

申し訳ありませんが、オートマトンは^ nb^n:$ ab、$ aabb、$ aaabbbなどの入力のみを受け付けています。 – BillyB

関連する問題