There are many ways in understand how compilers are built. In the simplest definition compilers are programs that take your source code and convert it to a form valid executable in a form or another(VM or machine language).
So in order to convert them, it has to first understand them. Its like the compiler program you write has to successfully understand millions of possible combination of valid programs that can be written in the language. There for in order to understand them it has to ...
a. Parse them : This step itself is composed of many steps. Since the program it self can have data and other stuff. It has to first recognize valid lexical tokens(What you call keywords). Doing this requires reading characters one by one, and then matching it against a template. Its like this, how do you recognize that a sentence is a valid English statement? You take the rules of English grammar and apply to the vocabulary in question.
A similar thing happens inside compilers. There is something called as a language grammar! Which fundamentally defines what is legal syntactically. Now writing a parser for each grammar that comes down the way is manually laborious and not practical. Hence there are parser generators. They work by, taking a grammar and then generating a parser for it. How does the parser look? There are many ways to do that, starting from using regular expressions to reading each character one by one and matching them until a valid lexical token is encountered.
b.Making sense of what has been read: You can make a grammatically valid sentence yet not make sense out of it. Same thing needs to be checked in a compiler too. What does the syntax notation mean... This is nothing but semantics.
c.You now what it means, now you want to mean the same in other languages: What is the equivalent of what I just parsed in Assembly. Its like you now perfectly parsed and recognized lets say a if statement. Now you take that convert it to equivalent of that in assembly.
d.Meanwhile optimize,what you can optimize: If there are any reasonable code optimizations can be done along the way, do it.
This is brief overview of compilers work in common language. Of course there is a lot to it. You can write volumes on it(And there are volumes on it, already written).
What you should do:
- Read some good theory on Compilers. Dragon book recommended.
- Download a opensource Compiler. Plenty available.
- Try to map what you have read from the book to the code.
- Break something and see how it works.
- Add something and see how it works.
Then:
- Write something on your own.
- Search for interesting problems to solve and solve them.
- Browse bug lists in those open source projects, Try sending patches to them.
Remember you can read a lot, but you will tested only when you write code. So write code. Fail often, learn from it. Use the feedback... Repeat the cycle again.