Code:
void lex(char *line, int start, int end, int length, char *typeN){
char token[MAXTOKEN];
while (start <length && isspace(line[start])) {
start++;
}
if (start >=end){
return;
}
strncpy(token, &line[start], end-start);
token[end-start] = '\0';
printf("%s: %s\n", typeN, token);
}
void tokenize(char *line, int length)
{
int start= 0;
int end=0;
static int inComment=0;
for (end=0; end <length; end++){
if(line[end]=='/' && line[end+1]=='*' && line[end+2]=='\n'){
lex(line, start, end, length, "Token");
start=end;
end++;
inComment=1;
while(inComment){
end++;
if(line[end]=='*' && line[end+1]=='/'){
inComment=0;
end+=2;
lex(line, start, end, length, "Comment");
}
}
}
else if (line[end]=='/' && line[end+1]=='*') {
lex(line, start, end, length, "Token");
start=end;
end++;
while(end<length && (line[end]!='*' || line[end+1]!='/')){
end++;
}
end+=2;
lex(line, start, end, length, "Comment");
start=end;
end++;
//Finds start of comment. Does Token then goes through comment.
}
else if (line[end] == '"') //If double qotes then
{
lex(line, start, end, length,"Token");
start=end;
end++;
while(end<length && line[end]!='"')
{
end++;
}
if (end<length){
end++;
lex(line, start, end, length, "String");
start=end;
end--;
}
}
//If sees ' then is a char. end++ to enter char until ' then end++ and lex.
else if (line[end] =='\''){
end++;
while(end<length && line[end]!='\''){
end++;
}
end++;
lex(line, start, end, length, "Char");
start=end;
end--;
}
//while+lex After anything at start/zero has been tokened. It adds to the index, through the characters. Once it finds the double quote it lexes from start=wherever the last token ended to end+zero to get the ending quotes.
else if(isspace(line[end])) {
lex(line, start, end, length, "Token");
start = end;
// printf("%i: ", end);
}
}
}
A multi-line comment is supposed to look like this