->
---> 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();
 }

最佳答案

仔细看这条线

if(ToBeChecked = '\0')

它是做什么的,为什么那不是您想要的?

关于c++ - 解析器帮助程序不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5161389/

10-09 13:24