0% found this document useful (0 votes)
17 views

Unit 1 CD

Uploaded by

Harsh Singhal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Unit 1 CD

Uploaded by

Harsh Singhal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Unit 1

What is Compiler

A compiler is a software tool that translates code written in a high-


level programming language into machine code, lower-level
language, that can be executed by a computer.

Compiler reads the whole source code at once & execute the whole
program even if some error occurs.
Architecture / Structure / phases of compiler.

1. Lexical Analysis (Scanning) (Frontend Phase): Breaking source


code into tokens(identifier,keywords,operators).
2. Syntax Analysis (Parsing) (Frontend Phase): Parsing tokens into
a syntax tree.
3. Semantic Analysis(Frontend Phase): Checking for semantic
correctness.
4. Intermediate Code Generation: Creating an intermediate
representation.
5. Optimization(Backend Phase): Improving the intermediate code.
6. Code Generation(Backend Phase): Converting IR to machine
code.
7. Error Handling:-Detecting & Reporting errors in code during
compilation process
8. Symbol table management:- Data structure used by compiler to
store information about variable, function used in source code

Lexical Analysis (Scanning):


Purpose: Also known as scanning, this stages involves breaking the
source code into a meaningful sequences called tokens. Tokens are the
basic building blocks of the source code, such as keywords,
identifiers, operators.
Lexical analyser reads/scan the source code character by character &
grouping them in token (such as keywords, identifiers, operators)
Output: A sequence of tokens.

Syntax Analysis (Parsing):


Purpose: This phase takes the sequence of tokens and organizes them
into a syntax tree, often called an Abstract Syntax Tree (AST), based
on the grammar rules of the programming language. It checks that the
syntax is correct and that the tokens are in the right order according to
the language's rules.
Output: A syntax tree
Semantic Analysis:
Purpose: Semantic analysis is the phase in a compiler where the code
is checked for meaning and logic. It ensures that the statements in the
program make sense and follow the rules of the programming
language.
Intermediate Code Generation:
Purpose: The primary function of intermediate code generate is to
create a intermediate representation of a source code that is easier to
analyse and manipulate than the original code. It is the stage between
frontend and backend(Optimisation and code generation).
Output: Intermediate code (IR).
Optimization:
Purpose: This stage involve improving the intermediate code or target
code to make it more efficient in terms of execution time & memory
usage.
Output: Optimized intermediate code.
Code Generation:
Purpose: Code generation is a phase in the compiler where optimized
intermediate representation of source code is translated into target
code. The primary function of Code generation is to produce
executable code that can run on target machine.
Output: Machine code or assembly code.
Symbol Table Management:
Symbol table is the data structure used by the compiler to store
information about variables, functions using source code
Error Handling:-
Detecting & Reporting errors in source code during compilation
process.
Translator:
Translator is a tool or program that convert high level language into
machine code or other intermediate representation that can be
executed by machine (Computer)
Translator helps to detect and report the error during translation

Type of Translator
1.Compiler

A compiler is a software tool that translates code written in a high-


level programming language into machine code, lower-level
language, that can be executed by a computer.

2.Interpreter
An interpreter reads and executes code line-by-line, without
converting it all to machine code at once. It’s like following a recipe
step-by-step while cooking.
An interpreter is a type of translator that converts high level language
into low level language and stop execution in case of error.
3.Assembler
An assembler is a type of translator that translates assembly language
code, which is a low-level but human-readable form of code, directly
into machine code.

What is compiler pass?


A compiler pass refers to the specific phase/stage of the compilation
process that perform a particular task or analysis on source code.
Pass structure
Pass structure refers to how a compiler is organized to process the
source code in multiple stages, each of which is called a pass.
A pass refers to the specific phase/stage of the compilation process
that perform a particular task or analysis on source code.
The overall goal is to transform high-level source code into machine
code or an executable form.
Types of Pass Structures
1. Single-Pass Compilers:
A single-pass compiler processes the source code only once,
performing all stages of compilation (lexical analysis, syntax analysis,
semantic analysis, and code generation) in a single pass.
Advantages:
1. Faster compilation time (Speed)
2. Requires less memory (Memory Efficiency)
Disadvantages:
1.Limited scope for optimization
2.Limited Error Detection
2. Multi-Pass / Two pass Compilers:
A two-pass compiler is a compiler that process the source code twice,
typically in two separate phases or passes.
In First Pass, compiler perform Lexical Analysis and Syntax Analysis,
Semantic Analysis
In Second Pass, compiler perform tasks like Optimization, Code
generation
Advantages:
1. More scope for optimization
2. error detection
Disadvantages:
1. Slower compilation time (Slow Speed)
2. High memory usage
What is the role of lexical analyzer?
The primary role of lexical analyzer is reads/scan the source code
character by character & grouping them into a meaningful sequence
called token (such as keywords, identifiers, operators). Also known as
scanning. Tokens are the basic building blocks of the source code.
These token serve as input to subsequent phases such as parser which
build the syntax tree.

Why is the Lexical Analyzer Important?

Simplifies Parsing: By converting the source code into tokens, the


lexical analyzer simplifies the work for the next stage of the
compiler, which is the parser.

Improves Efficiency: It makes the compilation process faster and


more efficient by removing unnecessary characters and organizing
the code into a structured format.

Early Error Detection: It helps catch and report errors early,


making it easier to fix issues in the code.
Example of Token Types:

 Keywords: if, else, while, return


 Identifiers: variable Name, function Name
 Operators: +, -, *, /, =
 Literals: 42, 3.14, "hello", "%d %d" (string literal)
 Punctuation: ;, ,, (), {}

Syntax specification
syntax specification defines the grammar rule and structure of
language. It outline how the programmer should write code to confirm
the language rules and be understood by compiler or interpreter.
Input buffering
Input buffering in compilers ensures efficient handling of source code
input, from reading the raw code to tokenizing it for further analysis
and processing. By buffering input data, compilers can optimize
resource utilization, improve performance, and enhance the overall
compilation experience.

Cross Compiler:

A cross compiler is a type of compiler that runs on one platform (the


host platform) but generates code for a different platform (the target
platform).

Purpose: It's used when you want to develop software for a platform
that's different from the one you're working on.

Benefits: Allows developers to write and test code on their preferred


platform without needing access to the target platform.
Bootstrap Compiler:

A bootstrap compiler is a compiler that's used to compile itself. Also


known as self-hosting compiler

The bootstrap compiler is capable of compiling its own source code.

You might also like