Compiler is large and complex program so we divide compiler into different phases on the base of their complexity. There are four compulsory phases of complier; Lexical Analysis, Syntax Analysis, Semantic Analysis and Target Code Generation. In first phase of compiler we define lexical rules by regular expression. Lexical Analysis is also called Scanning. In this phase source program is checked to have valid characters and words. After detail analysis input stream of tokens are stored in a buffer. The second phase of compiler is Syntax Analysis. In this phase we define syntax rules by Context Free Grammar. Syntax Analysis is also called parser. During parsing, syntax rules are checked. Input of this phase is tokens and output is parse tree or syntax tree. Third phase of complier is Semantic Analysis. Basically the word semantic means the "meaning".
In this phase we define semantic rules by attribute grammars. Semantic Analysis has two types; Declaration Checking and Type Checking. The input of this phase is syntax tree and output is attribute grammar. The last and final phase is Target Code Generation. After analysis of source code is completed, then last step is converting it into target language. The other optional phases are Source Code Optimizer, Target Code Optimizer and Intermediate Code Generator
In this phase we define semantic rules by attribute grammars. Semantic Analysis has two types; Declaration Checking and Type Checking. The input of this phase is syntax tree and output is attribute grammar. The last and final phase is Target Code Generation. After analysis of source code is completed, then last step is converting it into target language. The other optional phases are Source Code Optimizer, Target Code Optimizer and Intermediate Code Generator