# Compilers - COMP 3012 A compiler is a tool that maps one language into another language. It takes a program written in a source programming language and maps it to program written in a target programming language. A compiler is written in an **implementation language**. ![img](img/a.png) Notation of semantics of program $A$: $[\![A]\!]$ An **interpreter** is a program that takes a source program and executes the program. ![img](img/b.png) > NOTE: Java uses both. A java source program is compiled into byte code (by a compiler) which is then executed by an interpreter (called java virtual machine - JVM). JVM will also compile fragments of code so that if there is a call back, it can execute the compiled code. This is called compilation on the fly. Compilers will often use an **intermediate representation (IR)** to bridge the gap between the source language and the executable language. Converting source language to IR is called **front end**, where as converting IR to executable code is called **back end**. * The front end focuses on understand the source-language program. * The back end focuses on mapping programs to the target machine ![img](img/c.png) * The front end, intermediate representation and the back end are all part of the compiler. IR is stored as an Abstract Syntax Tree **AST**. The syntactic details needed for parsing the source program are represented in the structure of the tree. The **IR** could be broken down into many sub-steps i.e. a IR1 could be created which is then ran through an optimiser to create IR2 which is fed into the back end instead of IR1. This is called a *three-phase compiler*. ![img](img/d.png)