Example of phase of compiler – Lexical/Syntax/Semantic Intermediate Code Target
Contents
- 0.1 Phases of a Compiler – Examples for Each Phase
- 0.2 1. Lexical Analysis (Tokenization)
- 0.3 2. Syntax Analysis (Parsing)
- 0.4 3. Semantic Analysis (Meaning Verification)
- 0.5 4. Intermediate Code Generation
- 0.6 Summary Table
- 0.7 Example of phase of compiler – Lexical/Syntax/Semantic Intermediate Code Target
- 0.8 Module 1 Compiler Phases of a Compiler ( Structure of …
- 0.9 Analysis of the source program, Phases of a compiler, …
- 0.10 Computer Science Compiler Design Module 2
- 1 Example Line of Code:
- 1.1 1. Lexical Analysis (Tokenization)
- 1.2 2. Syntax Analysis (Parsing)
- 1.3 3. Semantic Analysis
- 1.4 4. Intermediate Code Generation
- 1.5 5. Code Optimization (Optional)
- 1.6 6. Target Code Generation
- 1.7 Summary Table:
- 1.8 Example of phase of compiler – Lexical/Syntax/Semantic Intermediate Code Target
- 1.9 UNIT- I Introduction to Compiling
- 1.10 Compiler Design by Md. Ajij (5th Semester)
Phases of a Compiler – Examples for Each Phase
A compiler translates high-level code into machine code through multiple phases. These phases can be categorized into front-end (analysis) and back-end (synthesis) processes.
1. Lexical Analysis (Tokenization)
-
Purpose: Converts source code into a sequence of tokens.
-
Example:
Input Code (C language):Output (Tokens):
Tool Used: Lexical Analyzer (Lexer)
2. Syntax Analysis (Parsing)
-
Purpose: Checks if tokens form a valid syntax according to the language grammar.
-
Example:
Input Tokens:Parsing Tree (Abstract Syntax Tree – AST):
Error Detected: Missing semicolon (
;
) or incorrect placement of keywords.
Tool Used: Parser
3. Semantic Analysis (Meaning Verification)
-
Purpose: Ensures code makes logical sense (type checking, scope checking, etc.).
-
Example:
Input Code:Error:
x
is declared as an integer, but assigned a string value.
Tool Used: Semantic Analyzer
4. Intermediate Code Generation
-
Purpose: Converts the code into an intermediate representation (IR) that is independent of machine architecture.
-
Example (Three-Address Code Representation):
Input Code:Intermediate Code (Three-Address Code – TAC):
Tool Used: Intermediate Code Generator
5. Target Code Generation
-
Purpose: Converts intermediate code into machine code (assembly language) for execution.
-
Example (x86 Assembly Code):
Tool Used: Code Generator
Summary Table
Phase | Purpose | Example Output |
---|---|---|
Lexical Analysis | Converts code into tokens | int → Keyword, x → Identifier |
Syntax Analysis | Creates syntax tree | Parses expressions a = b + c; |
Semantic Analysis | Checks for logical errors | Type mismatch: int vs string |
Intermediate Code | Converts code to IR (Three-Address Code) | t1 = c * d |
Target Code Generation | Generates machine/assembly code | MOV R1, c |
Would you like me to explain Compiler Optimizations as well?
Example of phase of compiler – Lexical/Syntax/Semantic Intermediate Code Target
Module 1 Compiler Phases of a Compiler ( Structure of …
Analysis of the source program, Phases of a compiler, …
Computer Science Compiler Design Module 2
Here is a simple and clear example of all major phases of a compiler, showing how one line of code is processed step by step:
Example Line of Code:
Now let’s go through each phase of the compiler and see what it does with this line:
1. Lexical Analysis (Tokenization)
Breaks the code into tokens:
Token Type | Token |
---|---|
Identifier | a |
Operator | = |
Identifier | b |
Operator | + |
Identifier | c |
Symbol | ; |
2. Syntax Analysis (Parsing)
Checks grammar (structure) of the code.
-
Builds a parse tree or syntax tree:
3. Semantic Analysis
Checks meaning of the code.
-
Is
a
declared before use? -
Are
b
andc
of same type? -
Can we add them?
b
is int
and c
is string
, shows semantic error.
4. Intermediate Code Generation
Converts code into an intermediate form (machine-independent):
Example:
5. Code Optimization (Optional)
Improves performance without changing meaning.
Before:
After:
6. Target Code Generation
Converts into machine code (assembly or binary):
Example (in simple assembly-like code):
Summary Table:
Compiler Phase | What it Does | Output |
---|---|---|
Lexical Analysis | Breaks into tokens | Tokens |
Syntax Analysis | Checks structure | Parse Tree |
Semantic Analysis | Checks meaning | Annotated Tree / Errors |
Intermediate Code Gen. | Machine-independent code | 3-address code |
Code Optimization | Improves code | Optimized code |
Target Code Generation | Creates machine code | Assembly / Binary Code |
Would you like a diagram of the compiler phases, a PDF summary, or a quiz to test your understanding?