What is Compiler?


Definition of Compiler:
1. A computer program which reads source code and outputs assembly code or executable code is called compiler.
2. A program that translates software written in source code into instructions that a computer can understand Software used to translate the text that a programmer writes into a format the CPU can use.

3. A piece of software that takes third-generation language code and translates it into a specific assembly code. Compilers can be quite complicated pieces of software.
Compiler Overview:
Consider process of Executing Simple C Language Code –
We have opened C Code editor and wrote simple C Program, whenever we try to
Compile code and try to execute code our code is given to the compiler.
int main()
{
printf("Hello");
return(0);
}
Now our main intention is to convert program into lower level language which can
Be read by Machine. Compilers have different phases, and program undergoes through these 6 phases. After Passing through all the compilation Phases our code
is converted into the machine level language code which can be read by machine.

 
Different Phases of Compiler
 1. Lexical Analysis Phase
2. Syntax Analysis
3. Semantic Analysis
4. Intermediate Code Generation
5. Code Optimizer
6. Code Generation


Lexical Analysis – Compiler:

Compiler :

Compiler takes high level human readable program as input and convert it into the lower level code.This conversion takes place using different phases.First phase of compiler is lexical analysis.

Must Read : [What is Compiler ?]

Different phases of compilers :

1.     Analysis Phase

2.     Synthesis Phase

1.Analysis Phase :

 Lexical analysis

 Syntax analysis

 Semantic analysis

Lexical Analysis Phase :

Task of Lexical Analysis is to read the input characters and produce as output a sequence of tokens that the parser uses for syntax analysis.



1.  Lexical Analyzer is First Phase Of Compiler.

2.   Input to Lexical Analyzer is “Source Code

3.   Lexical Analysis Identifies Different Lexical Units in a Source Code.

4.   Different Lexical Classes or Tokens or Lexemes
  • Identifiers
  • Constants
  • Keywords
  • Operators                                                   

5. Example : sum = num1 + num2 


So Lexical Analyzer Will Produce following Symbol Table -

Token
Type


sum
Identifier


=
Operator


num1
Identifier


+
Operator


num2
Identifier


;
Seperator


6.Lexical Analyzer is also called “Linear Phase” or “Linear Analysis” or “Scanning

7.Individual Token is also Called Lexeme

8.Lexical Analyzer’s Output is given to Syntax Analysis


Syntax Analysis – Compiler

Analysis Phase : 2nd Phase of Compiler (Syntax Analysis)

During the first Scanning phase i.e Lexical Analysis Phase of the compiler , symbol table is created by the compiler which contain the list of leximes or tokens.

Syntax Analysis :
  
1.     It is Second Phase Of Compiler after Lexical Analyzer

2.   It is also Called as Hierarchical Analysis or Parsing.

3.   It Groups Tokens of source Program into Grammatical Production

4.   In Short Syntax Analysis Generates Parse Tree
   
   Parse Tree Generation :

SUM  = NUM1 + NUM2

Now Consider above C Programming statement. In this statement we Syntax Analyzer will create a parse tree from the tokens.


Explanation : Syntax Analysis

 We know , Addition operator plus (‘+’) operates on two Operands

 Syntax analyzer will just check whether plus operator has two operands or not . It does not checks the type of operands.

 Suppose One of the Operand is String and other is Integer then it does not throw error as it only checks whether there are two operands associated with ‘+’ or not .

 So this Phase is also called Hierarchical Analysis as it generates Parse Tree Representation of the Tokens generated by LexicalAnalyzer

Semantic Analysis – Compiler

Syntax analyzer will just create parse tree. Semantic Analyzer will check actual meaning of the statement parsed in parse tree. Semantic analysis can compare information in one part of a parse tree to that in another part (e.g., compare reference to variable agrees with its declaration, or that parameters to a function call match the function definition).

Semantic Analysis is used for the following -

1.     Maintaining the Symbol Table for each block.

   2.   Check Source Program for Semantic Errors.

   3.   Collect Type Information for Code Generation.

   4.   Reporting compile-time errors in the code (except syntactic errors, which are caught by syntactic analysis)

   5.   Generating the object code (e.g., assembler or intermediate code)

Now In the Semantic Analysis Compiler Will Check -

1.     Data Type of First Operand

   2.   Data Type of Second Operand

   3.   Check Whether + is Binary or Unary.
 
4.     Check for Number of Operands Supplied to Operator Depending on Type of
     Operator (Unary | Binary | Ternary)