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