hey, I can seem to make this one work so I wrote another one. It runs but it doesn't work. I tested this but it keep saying invalid for everything anyway. You can check the flow chart again; i just changed ThisOne to ToBeChecked.

valid invalid
c cc ccc b a ab acd ada
ad ac acc accc ba cd ca da

here is the link to the flowchart for people that don't want to download it from the attachment:

main function: http://img84.imageshack.us/img84/6103/mainfunction.png

SProd Function:http://img269.imageshack.us/img269/2108/sprodfunction.png

AProd Function: http://img62.imageshack.us/img62/9852/aprodfunction.png

BProd Function: http://img51.imageshack.us/img51/3536/bprodfunction.png

/*
Write a recursive descent parser for the following grammar.
Specifically, your C & C++ program will let user enter any string up to 200 characters. 
Also, the program will tell user whether the input string satisfies the following grammar.

    <S> --->  a <B> | <A> | b
    <A> --->  c <A> |  c
    <B> --->  d | <A>

    Non-terminals are in "< >" brackets, while lower case characters and numbers are terminals.

    The following is a set of sample data for testing the first set of grammar:
     
		valid 							invalid
    c 	cc 	ccc 	b 				a 	ab 	acd 	ada
    ad 	ac 	acc 	accc 			ba 	cd 	ca 	da
*/

#include <iostream>
#include<stdlib.h>  // for the exit(1) function
using namespace std;

char text[200];
char ToBeChecked;

char lexical(); //identify the characters
void SProd();// <S> --->  a <B> | <A> | b
void AProd();// <A> --->  c <A> |  c
void BProd();// <B> --->  d | <A>


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> | <A> | b
void SProd()
{
    if(ToBeChecked == 'b')
	{
		ToBeChecked =lexical();
	}
	else
	{
	if(ToBeChecked == 'a')
	{
			ToBeChecked = lexical();
			BProd(); 
	}
		else
		{
			AProd();
			}
	}

}

// <A> --->  c <A> |  c
void AProd()
{
   if(ToBeChecked != 'c')
   {
	   cout<<"Invalid"<<endl;
		 exit(1);
   }
   else
	   ToBeChecked = lexical();

   while(ToBeChecked == 'c')
	   ToBeChecked = lexical();
}



//<B> --->  d | <A>
void BProd()
{
    
	if(ToBeChecked == 'd')
		ToBeChecked = lexical();
	else
		AProd(); 
	

}

Line 40, the single equal sign should be doubled == for comparison. Compiling with all warning on would have caught that typo (use -Wall as a compiler flag).

Comments
thanks man

Thanks. What do you mean by "use -Wall as a compiler flag" and how do you do it? I use visual c++ 2010 and code::block

In GCC (MinGW under windows), the flag to turn on almost all warnings is the -Wall flag, which means the command-line to compile is:

$ gcc my_source_file.cpp -Wall -o my_program

(in MSVC, the same compilation flag is "/Wall")

However, if you use an IDE, there is usually a "build options" or "project property" menu button. It will usually show a big list of compiler options. You just turn on every one of them that says "enable warnings for ... ", or at least enable the one which says "enable all warnings (-Wall)".

This article has been dead for over six months. Start a new discussion instead.