Example of phase of compiler – Lexical/Syntax/Semantic Intermediate Code Target
Example of phase of compiler – Lexical/Syntax/Semantic Intermediate Code Target
Contents [hide]
- 1 Phases of a Compiler – Examples for Each Phase
- 2 1. Lexical Analysis (Tokenization)
- 3 2. Syntax Analysis (Parsing)
- 4 3. Semantic Analysis (Meaning Verification)
- 5 4. Intermediate Code Generation
- 6 Summary Table
- 7 Example of phase of compiler – Lexical/Syntax/Semantic Intermediate Code Target
- 8 Module 1 Compiler Phases of a Compiler ( Structure of …
- 9 Analysis of the source program, Phases of a compiler, …
- 10 Computer Science Compiler Design Module 2
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?