I wonder how compiler detects keywords? For example : when i run a C compiler , it detects keywords and highlight them.
My question is , what kind of algorithm that compiler uses for detecting keywords?
It must know the grammar, the list of keywords and it must be able to identify single words, e.g. with regex like this: \w(\w+)\w (disclaimer: I write it out of top of my head, i've not tested it, so the actual regex may not work).
Also, it can utilize services provided by compiler or parser, which is exactly the subsystem which identify the lexical elements like words, numbers, delimiters.