CH 2 Complier&Linker

Download as pdf or txt
Download as pdf or txt
You are on page 1of 22

Ch2.

Complier and
Interpreter
Complier
• A compiler translates the code written in one language to some
other language without changing the meaning of the program.
It is also expected that a compiler should make the target code
efficient and optimized in terms of time and space.
• Compiler design principles provide an in-depth view of
translation and optimization process. Compiler design covers
basic translation mechanism and error detection & recovery. It
includes lexical, syntax, and semantic analysis as front end, and
code generation and optimization as back-end.
Language Processing System
Preprocessor

• A preprocessor, generally considered as a part of compiler, is a


tool that produces input for compilers. It deals with macro-
processing, augmentation, file inclusion, language extension,
etc.
Phases of Compiler
Analysis Phase and Synthesis Phase
Analysis Phase
• Known as the front-end of the compiler, the analysis phase of the compiler reads
the source program, divides it into core parts and then checks for lexical,
grammar and syntax errors.The analysis phase generates an intermediate
representation of the source program and symbol table, which should be fed to
the Synthesis phase as input.
Synthesis Phase
• Known as the back-end of the compiler, the synthesis phase generates the target
program with the help of intermediate source code representation and symbol
table.
• A compiler can have many phases and passes.
• Pass : A pass refers to the traversal of a compiler through the entire program.
• Phase : A phase of a compiler is a distinguishable stage, which takes input from
the previous stage, processes and yields output that can be used as input for the
next stage. A pass can have more than one phase
Phases of a compiler are:

• Lexical Analysis
• Syntactic Analysis or Parsing
• Semantic Analysis
• Intermediate Code Generation
• Code Optimization
• Code Generation
Lexical Analysis
• Lexical Analysis: Lexical analysis or Lexical analyzer is the initial stage or
phase of the compiler. This phase scans the source code and transforms the
input program into a series of a token.
• A token is basically the arrangement of characters that defines a unit of
information in the source code.
• NOTE: In computer science, a program that executes the process of lexical
analysis is called a scanner, tokenizer, or lexer.
• Roles and Responsibilities of Lexical Analyzer
• It is accountable for terminating the comments and white spaces from the
source program.
• It helps in identifying the tokens.
• Categorization of lexical units.
Syntax Analysis:
• In the compilation procedure, the Syntax analysis is the second stage.
Here the provided input string is scanned for the validation of the
structure of the standard grammar. Basically, in the second phase, it
analyses the syntactical structure and inspects if the given input is
correct or not in terms of programming syntax.
• It accepts tokens as input and provides a parse tree as output. It is
also known as parsing in a compiler.
• Roles and Responsibilities of Syntax Analyzer
• Note syntax errors.
• Helps in building a parse tree.
• Acquire tokens from the lexical analyzer.
• Scan the syntax errors, if any.
Semantic Analysis
• Semantic Analysis: In the process of compilation, semantic analysis is
the third phase. It scans whether the parse tree follows the guidelines
of language. It also helps in keeping track of identifiers and
expressions. In simple words, we can say that a semantic analyzer
defines the validity of the parse tree, and the annotated syntax tree
comes as an output.
• Roles and Responsibilities of Semantic Analyzer:
• Saving collected data to symbol tables or syntax trees.
• It notifies semantic errors.
• Scanning for semantic errors.
Intermediate Code Generation:
• Intermediate Code Generation: The parse tree is semantically
confirmed; now, an intermediate code generator develops three
address codes. A middle-level language code generated by a compiler
at the time of the translation of a source program into the object
code is known as intermediate code or text.
• Few Important Pointers:
• A code that is neither high-level nor machine code, but a middle-level
code is an intermediate code.
• We can translate this code to machine code later.
• This stage serves as a bridge or way from analysis to synthesis.
• Roles and Responsibilities:
• Helps in maintaining the priority ordering of the source language.
• Translate the intermediate code into the machine code.
• Having operands of instructions.
Code optimizer
• The final stage of the compilation process is the code generation
process. In this final phase, it tries to acquire the intermediate code
as input which is fully optimised and map it to the machine code or
language. Later, the code generator helps in translating the
intermediate code into the machine code.
• Roles and Responsibilities:
• Translate the intermediate code to target machine code.
• Select and allocate memory spots and registers.
Symbol Table

• The symbol table is mainly known as the data structure of the compiler. It
helps in storing the identifiers with their name and types. It makes it very
easy to operate the searching and fetching process.
• The symbol table connects or interacts with all phases of the compiler and
error handler for updates. It is also accountable for scope management.
• It stores:
• It stores the literal constants and strings.
• It helps in storing the function names.
• It also prefers to store variable names and constants.
• It stores labels in source languages.
Interpreter
• An interpreter is a program that directly executes the instructions in a
high-level language, without converting it into machine code. In
programming, we can execute a program in two ways. Firstly, through
compilation and secondly, through an interpreter. The common way is
to use a compiler.
Strategies of an Interpreter

• It can work in three ways:


• Execute the source code directly and produce the output.
• Translate the source code in some intermediate code and then execute this
code.
• Using an internal compiler to produce a precompiled code. Then, execute
this precompiled code.
• Some examples of programming languages that use interpreters are
Python, Ruby, Perl, PHP and Matlab. Top Interpreters according to the
computer languages –
• Python- CPython, PyPy, Stackless Python, IronPython
• Ruby- YARV, Ruby MRI (CRuby)
• JAVA- HotSpot, OpenJ9, JRockIt
Memory allocation
• Memory allocation is primarily a computer hardware operation but is
managed through operating system and software applications.
• Memory allocation process is quite similar in physical and virtual memory
management. Programs and services are assigned with a specific memory
as per their requirements when they are executed.
• Once the program has finished its operation or is idle, the memory is
released and allocated to another program or merged within the primary
memory. Memory allocation has two core types;
• Static Memory Allocation: The program is allocated memory at compile
time.
• Dynamic Memory Allocation: The programs are allocated with memory at
run time
Code Optimisation
• The programs represented in the intermediate code form usually
contains much scope for optimisation both in terms of storage space
as well as run time efficiency of the intended output program.
• Sometimes the input program itself contains such scope. Besides that,
the process of generating the intermediate code representation
usually leaves much room for such optimisation.
• Hence, compilers usually implement explicit steps to optimise the
intermediate code.
• Optimization is a program transformation technique, which tries to
improve the code by making it consume less resources (i.e. CPU,
Memory) and deliver high speed. In optimization, high-level general
programming constructs are replaced by very efficient low-level
programming codes.
• A code optimizing process must follow the three rules given below:
The output code must not, in any way, change the meaning of the
program. Optimization should increase the speed of the program
and if possible, the program should demand less number of
resources. Optimization should itself be fast and should not delay
the overall compiling process.
Machine-independent Optimization
• In this optimization, the compiler takes in the intermediate code and
transforms a part of the code that does not involve any CPU registers
and/or absolute memory locations.
• For example

This code involves repeated assignment of the identifier item, which if we put this way:
Machine-dependent Optimization
• Machine-dependent optimization is done after the target code has
been generated and when the code is transformed according to the
target machine architecture.
• It involves CPU registers and may have absolute memory references
rather than relative references. Machine dependent optimizers put
efforts to take maximum advantage of memory hierarchy

You might also like