Compiler
Compiler
Compiler-
A compiler is a system software that converts a source language program into an equivalent
target language program. It also validates the input program to be confirming to the source
language specification.
If this is validated the compiler will produce an error message to the user for less critical
violation it may produce warnings.
A compiler is a computer program which helps you transform source code written in a high-level
language into low-level machine language. It translates the code written in one programming
language to some other language without changing the meaning of the code. The compiler also
makes the end code efficient which is optimized for execution time and memory space.
The compiling process includes basic translation mechanisms and error detection. Compiler
process goes through lexical, syntax, and semantic analysis at the front end, and code
generation and optimization at a back-end.
Features of Compilers
Correctness
Speed of compilation
Preserve the correct the meaning of the code
The speed of the target code
Recognize legal and illegal program constructs
Good error reporting/handling
Code debugging help
PHASES OF COMPILER:-
Compiler operates in various phases each phase transforms the source program from
one representation to another. Every phase takes inputs from its previous stage and
feeds its output to the next phase of the compiler.
1. Lexical analysis
2. Syntax analysis
3. Semantic analysis
4. Intermediate code generation
1. BY:- Himanshu Ojha, M.C.A Course(4th Semester),Department Of Statistics,Patna University,Patna.
Compiler Design(Cs-45)
5. Code optimization
6. Code generation
Here, the character stream from the source program is grouped in meaningful sequences by
identifying the tokens. It makes the entry of the corresponding tickets into the symbol table and
passes that token to next phase.
Example:
x = y + 10
Tokens
X identifier
= Assignment operator
Y identifier
+ Addition operator
10 Number
Syntax analysis is all about discovering structure in code. It determines whether or not a text
follows the expected format. The main aim of this phase is to make sure that the source code
was written by the programmer is correct or not.
Syntax analysis is based on the rules based on the specific programing language by
constructing the parse tree with the help of tokens. It also determines the structure of source
language and grammar or syntax of the language.
Example
(a+b)*c
In Parse Tree
Interior node: record with an operator filed and two files for children
Leaf: records with 2/more fields; one for token and other information about the token
Ensure that the components of the program fit together meaningfully
Gathers type information and checks for type compatibility
Checks operands are permitted by the source language
Semantic analysis checks the semantic consistency of the code. It uses the syntax tree of the
previous phase along with the symbol table to verify that the given source code is semantically
consistent. It also checks whether the code is conveying an appropriate meaning.
Semantic Analyzer will check for Type mismatches, incompatible operands, a function called
with improper arguments, an undeclared variable, etc.
Helps you to store type information gathered and save it in symbol table or syntax tree
Allows you to perform type checking
In the case of type mismatch, where there are no exact type correction rules which
satisfy the desired operation a semantic error is shown
Collects type information and checks for type compatibility
Checks if the source language permits the operands or not
Example
float x = 20.2;
float y = x*30;
In the above code, the semantic analyzer will typecast the integer 30 to float 30.0 before
multiplication
Once the semantic analysis phase is over the compiler, generates intermediate code for the
target machine. It represents a program for some abstract machine.
Intermediate code is between the high-level and machine level language. This intermediate
code needs to be generated in such a manner that makes it easy to translate it into the target
machine code.
Example
For example,
t1 := int_to_float(5)
t2 := rate * t1
t3 := count + t2
total := t3
The next phase of is code optimization or Intermediate code. This phase removes unnecessary
code line and arranges the sequence of statements to speed up the execution of the program
without wasting resources. The main goal of this phase is to improve on the intermediate code
to generate a code that runs faster and occupies less space.
Example:
a = intofloat(10)
b=c*a
d=e+b
f=d
Can become
b =c * 10.0
f = e+b
Code generation is the last and final phase of a compiler. It gets inputs from code optimization
phases and produces the page code or object code as a result. The objective of this phase is to
allocate storage and generate relocatable machine code.
It also allocates memory locations for the variable. The instructions in the intermediate code are
converted into machine instructions. This phase coverts the optimize or intermediate code into
the target language.
The target language is the machine code. Therefore, all the memory locations and registers are
also selected and allotted during this phase. The code generated by this phase is executed to
take inputs and generate expected outputs.
Example:
a = b + 60.0
MOVF a, R1
MULF #60.0, R2
ADDF R1, R2
A symbol table contains a record for each identifier with fields for the attributes of the identifier.
This component makes it easier for the compiler to search the identifier record and retrieve it
quickly. The symbol table also helps you for the scope management. The symbol table and
error handler interact with all the phases and symbol table update correspondingly.
In the compiler design process error may occur in all the below-given phases:
Most common errors are invalid character sequence in scanning, invalid token sequences in
type, scope error, and parsing in semantic analysis.
The error may be encountered in any of the above phases. After finding errors, the phase needs
to deal with the errors to continue with the compilation process. These errors need to be
reported to the error handler which handles the error to perform the compilation process.
Generally, the errors are reported in the form of message.