Thread: Case Sensitivity... matching words in a string.

  1. #1
    Registered User
    Join Date
    Jan 2010

    Case Sensitivity... matching words in a string.

    Hi there,

    I am writing a simple scanner for school to better understand compilers. I can't seem to figure out why in the following code the token ElSe still matches when I compare it character by character to capital letters (ELSE). So in the scanner ElSe would be a variable not a keyword as it is not all in caps.

    int iskeyword(char * kw_lexeme){
    /*	NOT A KEYWORD = -1 */
    /*	"ELSE" = 0 */
    /*  "IF"   = 1 */
    /*  "INPUT" = 2 */
    /*  "OUTPUT" = 3 */
    /* "PLATYPUS" = 4 */
    /* "REPEAT" = 5 */
    /* "THEN" = 6 */
    /* "USING" = 7 */
       	case 'E':
          	if(kw_lexeme[1]=='L' && kw_lexeme[2]=='S' && kw_lexeme[3]=='E'){
                return 0;
       	case 'I':
             	return 1;
             else if(kw_lexeme[1]=='N' && kw_lexeme[2]=='P' && kw_lexeme[3]=='U' && kw_lexeme[4]=='T')
             	return 2;
       	case 'O':
          	if(kw_lexeme[1]=='U' && kw_lexeme[2]=='T' && kw_lexeme[3]=='P' && kw_lexeme[4]=='U' && kw_lexeme[5]=='T')
             	return 3;
       	case 'P':
          	if(kw_lexeme[1]=='L' && kw_lexeme[2]=='A' && kw_lexeme[3]=='T' && kw_lexeme[4]=='Y' && kw_lexeme[5]=='P' && kw_lexeme[6]=='U' && kw_lexeme[7]=='S')
             	return 4;
       	case 'R':
          	if(kw_lexeme[1]=='E' && kw_lexeme[2]=='P' && kw_lexeme[3]=='E' && kw_lexeme[4]=='A' && kw_lexeme[5]=='T')
             	return 5;
       	case 'T':
          	if(kw_lexeme[1]=='H' && kw_lexeme[2]=='E' && kw_lexeme[3]=='N')
             	return 6;
       	case 'U':
          	if(kw_lexeme[1]=='S' && kw_lexeme[2]=='I' && kw_lexeme[3]=='N' && kw_lexeme[4]=='G')
             	return 7;
          		return -1;
    So basically why is ElSe returning a 0 when it should be the default -1?


  2. #2
    Registered User
    Join Date
    Mar 2010
    kw_lexeme[1]=='L' and not 'l'

  3. #3
    Registered User
    Join Date
    Jan 2010
    Quote Originally Posted by themsaman View Post
    kw_lexeme[1]=='L' and not 'l'
    Yes I have it as 'L' now because I only want capitals to match.

    ELSE\0 as argument should return 0
    ElSe\0 SHOULD return -1 but is returning 0.

  4. #4
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    London, United Kingdom
    Uhhh... beats me to be honest. Did you check what is happening with a debugger? Is it going through that branch
    Last edited by claudiu; 03-13-2010 at 07:47 PM.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    ElSe should return lkasjhltiuq3gto4997aguwhfl, and not -1, since no return statement is encountered in the program flow for that input. The exact value of lkasjhltiuq3gto4997aguwhfl is going to vary from machine to machine, of course.

    *Note: The bit about lkasjhltiuq3gto4997aguwhfl is not true. But the "no return statement" part is true, meaning the return value could be (and will be) any number at all -- this is why your compiler is telling you "not all control paths return a value" or similar (unless you turned off warnings, which you generally shouldn't do). The default case will not get hit if you go into a specific case and then use break. (Also, you may notice an interesting result if you use "EF" as input as well....)

  6. #6
    Registered User
    Join Date
    Jan 2010
    Quote Originally Posted by braden87 View Post
    ELSE\0 as argument should return 0
    ElSe\0 SHOULD return -1 but is returning 0.
    According to your code "ElSe" should return an undefined value, which in this case happens to be 0.
    The default case in a switch case only gets executed if no other case match, or if the case before it doesn't have a break statement.
    The "not all code paths return a value" warning you should be getting from the compiler is not something that should be ignored

    Looks like tabstop beat me to it..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 04:42 PM
  3. New Theme
    By XSquared in forum A Brief History of
    Replies: 160
    Last Post: 04-01-2004, 07:00 PM
  4. error with code
    By duffy in forum C Programming
    Replies: 8
    Last Post: 10-22-2002, 09:45 PM