Thread: gettoken() function

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    69

    gettoken() function

    hello,

    I have been trying to understand this function, there is still a few things confusing me. First, what are those weird statements like return token type = BRACKETS for? I have never seen them before.

    Code:
    int gettoken(void)   /* return next token */
    
    
    {
    	
    	int c, getch(void);
    	void ungetch(int);
    	char *p = token;  /* character string pointed by p */
    	
    	while ((c = getch()) == ' '  || c == '\t') /* skip blanks or tabs */
    		;
    	if (c == '(') {
    		if ((c = getch()) == ')') { 
    			strcpy(token, "()");         
    			return tokentype = PARENS;  
    		}  else {
    			ungetch(c);   /* put character back on input before prosessing new input */           
    			return tokentype = '(';
    			
    		 }
    	 } else if (c == '[')  {
    		 for (*p++ = c; (*p++ = getch()) != ']'; )  /* do nothing as long as character is not terminating bracket */ 
    			 ;
    		 *p = '\0';
    		 return tokentype = BRACKETS;  /* if it is, we found it, so tokentype is brackets */
    	 }   else if (isalpha(c))  {      
    			for (*p++ = c; isalnum(c == getch()); ) /* read characters as long as is alphanumeric */
    				*p++ = c;     
    			*p = '\0';
    			ungetch(c);          /* if not, push back into the buffer */
    			return tokentype = NAME   /* how about these odd return statements?? */
    		} else
    			return tokentype = c;
      }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by coder222
    First, what are those weird statements like return token type = BRACKETS for? I have never seen them before.
    This:
    Code:
    return tokentype = BRACKETS;
    is equivalent to:
    Code:
    tokentype = BRACKETS;
    return tokentype;
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gettoken() function from K&R
    By coder222 in forum C Programming
    Replies: 0
    Last Post: 12-09-2015, 06:38 AM
  2. Function Prototype, Function Call, and Function definition
    By dmcarpenter in forum C Programming
    Replies: 9
    Last Post: 04-09-2013, 03:29 AM
  3. Replies: 13
    Last Post: 03-20-2012, 08:29 AM
  4. Print function: sending a function.. through a function?
    By scarlet00014 in forum C Programming
    Replies: 3
    Last Post: 11-05-2008, 05:03 PM
  5. Replies: 9
    Last Post: 01-02-2007, 04:22 PM