Thread: problem with parser code

  1. #1
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732

    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

  2. #2
    Sr. Software Engineer filker0's Avatar
    Join Date
    Sep 2005
    Location
    West Virginia
    Posts
    235
    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.
    Insert obnoxious but pithy remark here

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    you are right filker0 its was something to do with the parameters. it working fine now. thanks very much for you help.


    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with game code.
    By ajdspud in forum C++ Programming
    Replies: 5
    Last Post: 02-14-2006, 06:39 PM
  2. problem with selection code
    By DavidP in forum Game Programming
    Replies: 1
    Last Post: 06-14-2004, 01:05 PM
  3. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM
  4. Help with code for simple Y2K problem
    By Mule in forum C++ Programming
    Replies: 3
    Last Post: 03-06-2003, 12:53 AM