0% found this document useful (0 votes)
2 views4 pages

Compiler Important

Compiler design consists of several phases including Lexical Analysis, Syntax Analysis, Semantic Analysis, Intermediate Code Generation, Code Optimization, Target Code Generation, and Code Linking and Assembly. Each phase serves a specific purpose, such as breaking source code into tokens, checking syntax, ensuring semantic correctness, generating intermediate code, optimizing for performance, and producing the final executable. Tools like Lex, Yacc, and Bison are commonly used in these processes.

Uploaded by

ftr3189
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)
2 views4 pages

Compiler Important

Compiler design consists of several phases including Lexical Analysis, Syntax Analysis, Semantic Analysis, Intermediate Code Generation, Code Optimization, Target Code Generation, and Code Linking and Assembly. Each phase serves a specific purpose, such as breaking source code into tokens, checking syntax, ensuring semantic correctness, generating intermediate code, optimizing for performance, and producing the final executable. Tools like Lex, Yacc, and Bison are commonly used in these processes.

Uploaded by

ftr3189
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/ 4

Phases of Compiler Design

Compiler design involves multiple phases, each responsible for a specific part of translating a
high-level program (like C, Java, or Python) into machine code. Below are the main phases of a
compiler, explained with examples for better understanding.

🔷 1. Lexical Analysis (Scanner)

🔸 Purpose:

 Breaks the source code into tokens (keywords, identifiers, operators, etc.)

🔸 Input:
c
CopyEdit
int x = 10 + 20;

🔸 Output (Tokens):
css
CopyEdit
[int], [identifier: x], [=], [10], [+], [20], [;]

🔸 Tool: Lex / Flex

🔷 2. Syntax Analysis (Parser)

🔸 Purpose:

 Checks if the token sequence follows the grammar rules (syntax) of the language
 Creates a parse tree or abstract syntax tree (AST)

🔸 Example:

For: x = 10 + 20;
AST:

markdown
CopyEdit
=
/ \
x +
/ \
10 20

🔸 Tool: Yacc / Bison

🔷 3. Semantic Analysis

🔸 Purpose:

 Ensures semantic correctness (meaning)


 Type checking, variable declarations, scope validation

🔸 Example:
c
CopyEdit
int x;
x = "Hello"; // Error: assigning string to an integer

🔸 Output:

 Symbol Table
 Type Error (if any)

🔷 4. Intermediate Code Generation

🔸 Purpose:

 Converts AST into an intermediate representation (IR), platform-independent

🔸 Example:

For x = 10 + 20;
Intermediate Code (Three-address code):

ini
CopyEdit
t1 = 10 + 20
x = t1

🔷 5. Code Optimization

🔸 Purpose:

 Improves IR to make the final code efficient, without changing output

🔸 Example:
c
CopyEdit
x = 10 + 20; // Constant Folding

Optimized IR:

ini
CopyEdit
x = 30

🔷 6. Target Code Generation

🔸 Purpose:

 Converts optimized IR to machine code or assembly for a specific architecture

🔸 Example:

Assembly (x86-like):

nginx
CopyEdit
MOV R1, #30
MOV x, R1
🔷 7. Code Linking and Assembly

🔸 Purpose:

 Assembles machine code and links libraries or other object files to create the final
executable

📌 Summary Table:
Phase Task Example Output
int x = 10 + 20; → [int], [x],
Lexical Analysis Token generation
[=], ...
Syntax Analysis Parse Tree creation x = 10 + 20 AST
Semantic Analysis Type and scope checking Error if x = "text";
Intermediate Code
IR generation t1 = 10 + 20; x = t1
Gen
Code Optimization Improve performance x = 30
Assembly or Machine
Target Code Gen MOV R1, #30
Code
Linking & Assembly Executable file creation .exe, .out, etc.

You might also like