CH 2 Complier&Linker
CH 2 Complier&Linker
CH 2 Complier&Linker
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
• 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
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