On Sun, 13 Apr 2008 08:00:38 -0700 (PDT), smartbeginner
<rr******@gmail.comwrote:
>The Problem is CFG grammar generation
Eg:
Input S->ABC
A->bcd
B->efg
C->hij
It should produce the o/p bcdefghij substituting for every Capitals
found on the way recursively
The C pgm I wrote was:
#include<stdio.h>
#include<conio.h>
To maximize the chances of getting help here, it is best to restrict
your code to standard C. Not all of us have this non-standard header
and for those that do it may not contain the same code as yours.
>int main()
{
int recurPrint(char[15],char[][50],int,int);
int findVar(char,char[]);
Because these declarations are inside main, they go out of scope at
the end of main.
char stack[50];
char grmr[15][50];
char lhs[15],ch;
int i=0;
clrscr();
This serves no purpose. Many here have voiced the opinion that your
code should not decide whether previous information on my screen is
still useful.
while(1)
{
printf("\n Enter variable:");
Due to the lack of a terminating \n, on some systems this prompt may
not appear until the buffer is flushed. You are left with the system
expecting input and the user confused.
scanf("%c",&lhs[i]);
flushall();
A non-standard function.
printf("\n Enter production:");
gets(grmr[i]);
How do you propose to prevent buffer overflow?
>
printf("\n Do you want to continue[y/n]:");
ch=getche();
A non-standard function
if(ch=='n'||ch=='N')
break;
++i;
}
How do you prevent the user from inputting more than 15 strings?
>
lhs[i+1]='\0';
If the user stops after the 15th string, this will invoke undefined
behavior by attempting to set the non-existent object lhs[15].
>
recurPrint(lhs,grmr,0,0);
getche();
return 0;
}
int findVar(char find,char lhs[])
The only reason recurPrint can recognize findVar as a declared
function is because the definition happens to precede the call. You
should move the declarations out of main and place them at file scope.
{
int i=0;
while(lhs[i]!='\0')
{
if(lhs[i]==find)
return i;
++i;
}
If you reach this point in the code, the function will return without
returning an int. This invokes undefined behavior.
>
}
int recurPrint(char lhs[],char grmr[][50],int lims,int ji)
{
static int index;
static int j;
index=lims;
j=ji;
printf("\n Start of function Rep");
printf("\n Index is:%d",index);
printf("\n With j is:%d",j);
getche();
if(grmr[index][j]=='\0')
return 0;
else if(grmr[index][j]>='A' && grmr[index][j]<='Z')
Not all character systems make the letters consecutive. Look at the
isupper function to perform this test.
> {
printf("\n Capital arrived");
getche();
Is it really your intention to have the user press a key for every
letter being printed?
> index=findVar(grmr[index][j],lhs);
You never check if findVar succeeded.
> recurPrint(lhs,grmr,index,0);
When this incarnation finally returns (after printing bcd), index is 0
and j is 0. j never gets incremented and you fall out of the
function. You never process the B. Either this block must include
some kind of loop or the calling statement in main needs to iterate
through the desired values of j.
> }
else if(grmr[index][j]>='a' && grmr[index][j]<='z')
islower. But then, that prevents the expansion from including any
non-alpha characters, such as numbers. Why not just else?
> {
printf("\n Small arrived with j as:%d",j);
getche();
printf("%c",grmr[index][j]);
j++;
recurPrint(lhs,grmr,index,j);
}
Your code definitely reaches this point yet you fail to return the
promised int. This invokes undefined behavior.
}
The O/P I am getting for the input mentioned above is :
Start of function Rep
Index is:0
With j is:0
Capital arrived
Start of function Rep
Index is:1
With j is:0
Small arrived with j as:0 b
Start of function Rep
Index is:1
With j is:1
Small arrived with j as:1 c
Start of function Rep
Index is:1
With j is:2
Small arrived with j as:2 d
Start of function Rep
Index is:1
With j is:3
that is just bcd.I need bcdefghij .Please help me
See the logic error identified in the capitals block.
Remove del for email