2011-03-01 45 views
1

- > ---> a | b ---> 1 | 1解析器幫助程序不能正常工作

這是假設開始將a或b和1之後。 a或b之後有多少個1並不重要。例如:A1,B1,b111111,A111 一切,我輸入是無效的,但這應該是有效的:A1,B1,b111111,A111

/* 
<S> --> <A><B> 
<A> ---> a | b 
<B> ---> 1 | 1 <B> 

It suppose to be vaild for example: a1, b1, b111111, a111 
*/ 
#include <iostream> 
#include<stdlib.h> // for the exit(1) function 
using namespace std; 

char text[100]; 
char ToBeChecked; 

char lexical(); //identify the characters 
void SProd(); 
void AProd(); 
void BProd(); 


int main() 
{ 
    cout<<"Enter a string(max. 100 characters"<<endl; 
    cin>>text; 

    ToBeChecked = lexical(); //identify the character; find the first letter and give it to ToBeChecked 
    SProd(); 

    if(ToBeChecked = '\0') 
     cout<<"Valid"<<endl; 
     else 
     cout<<"Invalid"<<endl; 
    cin.get(); 
    return 0; 
} 

char lexical() 
{ 
    static int index = -1; //a memory box named index with a value of -1; is static so it won't change. 
          //is -1 because -1 to 1 is 0; everytime move on to next one 
    index++; //update index 
    return text[index]; //return the value of index 
} 

//<S> --> <A> <B> 
void SProd() 
{ 
    AProd(); 
    BProd(); 
} 

//<A> ---> a | b 
void AProd() 
{ 
    if(ToBeChecked == 'a'){ 
     ToBeChecked = lexical(); 
    } 
    else 
    { 
     if(ToBeChecked == 'b'){ 
      ToBeChecked = lexical(); 
     } 
    } 
} 

//<B> ---> 1 | 1 <B> 
void BProd() 
{ 
    if(ToBeChecked != '1') 
    { 
     cout<<"Invalid"<<endl; 
     exit(1); 
    } 
    else 
     while (ToBeChecked == '1') 
      ToBeChecked = lexical(); 
} 
+3

您應該重新格式化您的帖子,以便您實際上提出某種問題。 – 2011-03-01 22:53:44

+0

我輸入的所有內容都是無效的,但這應該是有效的:a1,b1,b111111,a111 – Mugetsu 2011-03-01 22:56:25

+0

這是一個聲明。你有什麼問題? – razlebe 2011-03-01 23:00:34

回答

5

仔細看這條線

if(ToBeChecked = '\0') 

它做了什麼,爲什麼可能不是你想要的?

+0

謝謝!我知道我做的一切都對,所以我很困惑,爲什麼它不工作。我從來沒有想過我有這個錯誤。 if(ToBeChecked =='\ 0') – Mugetsu 2011-03-01 23:08:50