A compiler translates source code from a high-level programming language to lower-level machine code, enabling the creation of executable programs. It consists of several parts, including a lexical analyzer, syntax analyzer, and code optimizer, each responsible for different transformations and error-checking of the source program. Techniques from compiler design are also applicable to various problems in computer science, such as natural language processing and database querying.