lanaguage proc by a student of engineering college

jenilkalsariya210 12 views 9 slides Mar 01, 2025
Slide 1
Slide 1 of 9
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9

About This Presentation

lanaguage proc


Slide Content

Phases of a Compiler ID: 22IT051 SUB: LP (IT365) Lab Faculty: Rajnik Sir 1 22IT051(LP)

What is a Compiler? 2 22IT051(LP) A compiler is a software tool that translates source code written in a high-level programming language into machine code or another lower-level form that a computer can execute. Key Roles of a Compiler: Translation: Converts human-readable code to machine-readable instructions. Error Detection: Identifies syntax, semantic, and type-related errors in the code. Optimization: Enhances the performance of the generated code by improving execution speed and reducing resource usage. Importance of a Compiler: Makes programming languages like C, C++, Java, etc., usable for software development. Ensures efficient execution of programs on hardware.

Overview of Compilation Phases 3 22IT051(LP)

Lexical Analysis 4 22IT051(LP) The first phase of a compiler is lexical analysis, also known as scanning. This phase reads the source code and breaks it into a stream of tokens, which are the basic units of the programming language. The tokens are then passed on to the next phase for further processing. Role in Compilation : Detects invalid characters or tokens in the source code. Simplifies input for the next phase (syntax analysis). Eliminates whitespaces and comments. Input: int x = 5; number of tokens: 5

Syntax Analysis 5 22IT051(LP) The second phase of a compiler is syntax analysis, also known as parsing. This phase takes the stream of tokens generated by the lexical analysis phase and checks whether they conform to the grammar of the programming language. The output of this phase is usually an Abstract Syntax Tree (AST). Example: int x = 5;

Semantic Analysis and Intermediate Code Generation 6 22IT051(LP) 1) Semantic Analysis : Ensures the code is semantically correct by performing type checking and verifying rules. like variable declarations, function calls, and type compatibility. Detects errors such as undeclared variables or incompatible operations. 2)Intermediate Code Generation : Transforms the source code into an intermediate representation (IR) , which is simpler and machine-independent. Acts as a bridge between source code and machine code for easier translation.

Code Optimization and Code Generation 7 22IT051(LP) 1)Code Optimization : Enhances the intermediate code to improve performance by minimizing execution time and resource usage. Common techniques include eliminating redundant calculations , loop unrolling , and strength reduction (e.g., replacing x * 2 with x + x ). 2) Code Generation : Converts optimized intermediate code into target machine code , such as assembly language or binary. Ensures the generated code is efficient, accurate, and compatible with the target hardware.

References Geeksforgeeks : https://www.geeksforgeeks.org/phases-of-a-compiler/?ref=lbp 8 22IT051(LP)

9 22IT051(LP)
Tags