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.
Code:
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 */
switch(kw_lexeme[0]){
case 'E':
if(kw_lexeme[1]=='L' && kw_lexeme[2]=='S' && kw_lexeme[3]=='E'){
return 0;
break;
}
case 'I':
if(kw_lexeme[1]=='F')
return 1;
else if(kw_lexeme[1]=='N' && kw_lexeme[2]=='P' && kw_lexeme[3]=='U' && kw_lexeme[4]=='T')
return 2;
break;
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;
break;
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;
break;
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;
break;
case 'T':
if(kw_lexeme[1]=='H' && kw_lexeme[2]=='E' && kw_lexeme[3]=='N')
return 6;
break;
case 'U':
if(kw_lexeme[1]=='S' && kw_lexeme[2]=='I' && kw_lexeme[3]=='N' && kw_lexeme[4]=='G')
return 7;
break;
default:
return -1;
}
}
So basically why is ElSe returning a 0 when it should be the default -1?
Thanks