-
problem with parser code
hello guys,
i have been stuck with some right now on my design on paser for a simple grammer which is given bellow
L -> ( C )
C -> idC'
C' -> , id C' | E
for the expression
( a , b , c )
the problem with the code is when i return from the function C' to the C the token 'sym' which i read in C' is not the same in C. its true that u can say that all the local varables will destroyed when the control comes out of the function. but in my case i have declared sym variable as globle so that it can be acessed by all the function. and here us code
Code:
char *sym;
void L(char *sym)
{
if(strcmp(sym,"(")==0)
{
sym=getsym();
C(sym);
printf("%s",sym); // so if i print the sym here it has to print ')'but it prints the old which was read by this function first that was id
if(strcmp(sym,")")!=0)
{
printf("Error: closing para missing\n");
getchar();
exit(1);
}
}
else
{
printf("Error: opening para missing\n");
getchar();
exit(1);
}
}
void C(char *sym)
{
if(strcmp(sym,"id")==0)
{
sym=getsym();
C1(sym);
}
else
{
printf("Error: Identifier missing\n");
getchar();
exit(1);
}
}
void C1(char *sym)
{
if(strcmp(sym,",")==0)
{
sym=getsym();
if(strcmp(sym,"id")==0)
{
sym=getsym();
C1(sym);
}
else
{
printf("Error: Identifier missing\n");
getchar();
exit(1);
}
}
}
any help would be appriciated
ssharish2005
-
I don't know how getsym() works, but I'm pretty sure that at least part of your problem is that the functions C, C1, and C' don't see your global variable sym, as the formal parameter, also named sym, hides the global one due to the scoping rules of the C language.
-
you are right filker0 its was something to do with the parameters. it working fine now. thanks very much for you help.
ssharish2005