Help Badly needed- replacing rules & strings

W

Willing 2 Learn

I'm working on this program below but im stuck. after it finds a match
b/ween sentence & non_terminal it will output where these are found in
both strings. I need it to spit out the rules with a matching LHS
related to that non_terminal; i want to allow the user to select a rule
(from # key)they want to use. When the user selects a specified rule;
i need to replace the rule in that present address by expanding the
non_terminal. I was trying to do this by finding the length of string &
length of senetnce then from left to right move over to make space for
length of string that is being inserted. After doing this couple
times, it will ask the user to enter a dummy value; this will tell it
to go to the next address & do same by outputting rules related to next
address; then repating steps from user. It should do this till entire
string is replaced then output the new string.

#include <iostream.h>
#include <string.h>

int main()
{
char sentence[81]= "E+ND+T";
char terminal[] = "0123456789+-()";
char non_terminal[]="ETND";
char* sptr;
char* nptr;
char s, n;
int rule,length;

//Output the sentence
cout<<(int)&sentence<<endl; //convert string address to int

sptr = &sentence[0];


while(*sptr !='\0')
{

s = *sptr;
cout<<"s="<<s<<endl;
sptr++;

nptr = &non_terminal[0];

while(*nptr !='\0')
{
n = *nptr;
cout<<"n="<< n;


//Output the non-terminals and their position n the sentence and in
the non-terminals

if (s == n)
{
cout<<"Found "<<s<<" at "<<(int)sptr<<" in "<<sentence<<endl;
cout<<"Found "<<n<<" at "<<(int)nptr<<" in "<<non_terminal<<endl;


//output rule relating to non-terminal
if(n =='E')
{
cout<<"Rule: E= E+T (Enter 1)"<<endl;
cout<<"Rule: E= E-T (Enter 2)"<<endl;
cout<<"Rule: E= T (Enter 3)"<<endl;
}

if(n =='T')
{
cout<<"Rule: T = N (Enter 4)"<<endl;
cout<<"Rule: T = (E) (Enter 5)"<<endl;

}
if(n =='N')
{
cout<<"Rule: N= ND (Enter 6)"<<endl;
cout<<"Rule: N = D (Enter 7)"<<endl;

}
if(n =='D')
{
cout<<"Rule: D = 0 (Enter 8)"<<endl;
cout<<"Rule: D = 1 (Enter 9)"<<endl;
cout<<"Rule: D = 2 (Enter 10)"<<endl;
cout<<"Rule: D = 3 (Enter 11)"<<endl;
cout<<"Rule: D = 4 (Enter 12)"<<endl;
cout<<"Rule: D = 5 (Enter 13)"<<endl;
cout<<"Rule: D = 6 (Enter 14)"<<endl;
cout<<"Rule: D = 7 (Enter 15)"<<endl;
cout<<"Rule: D = 8 (Enter 16)"<<endl;
cout<<"Rule: D = 9 (Enter 17)"<<endl;


}



break;

}
nptr++;
}
}

//Ask user to enter # for rule
cout<<"Please enter # for rule selected ";
cin>>rule;

//for(int i=0; i>1; i++)
//{
//length= n.length();
//cout<<length<<endl;
//}




/*switch(rule)
{
case '1': E + T;
case '2': E - T;
case '3': E = T
case '4': T = N;
case '5': T = (E);
case '6': N = ND;
case '7': N = D;
case '8': D = 0;
case '9': D = 1;
case '10': D = 2;
case '11': D = 3;
case '12': D = 4;
case '13': D = 5;
case '14': D = 6;
case '15': D = 7;
case '16': D = 8;
case '17': D = 9;
}*/


return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top