ScholarQuill logoScholarQuillUniversity Notes
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
Study Tools
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
Contact
ScholarQuill logo
My Semester
Explore Notes
Past Papers12 New
Video CoursesNew
Search
Settings
GPA/CGPA Calculator
Schedule & To-Do
Student Portal
Contact
ScholarQuill logoScholarQuillUniversity Notes
Study PortalCOMP3149Compiler Structure Pha…
Ctrl + K
Login
Explore
Search
Semester
Papers
CGPA
    COMP3149

    Compiler Construction

    Progress0 / 32 done
    Topics32
    1Introduction to interpreter and compiler2Structure of a Compiler and its Phases3Lexical Analyzer and Input Buffering4Specifications and Recognitions of Tokens5Regular Expressions and Finite Automata6Transition Table and Transition Graph7Definitions of Grammars, Derivations, and Parse Trees8Ambiguity, Associativity, and Precedence of Operators9Syntax Analysis and Role of the Parser10Eliminating Ambiguity, Left Recursion, and Left Factoring11Top-Down Parsing and Recursive-Descent Parsing12First and Follow Sets13LL(1) Grammars and Non-recursive Predictive Parsing
    14Reductions and Shift-Reduce Parsing15LR Parsing and LR(0) Parsers16LR(0) Automaton and Parsing Table17Shift-Reduce Conflicts
    18Automaton and Parsing Table
    19Automaton and Parsing Table
    20Automaton and Parsing Table21Semantic Analysis and Intermediate Code Generation22Three Address Code23Tasks of Semantic Analyzer and Types of Errors24Type Checking and Environments
    25Implicit vs Explicit26Back Patching and Switch Statements27Storage Organization and Stack Allocation of Space28Heap Management and Optimization
    29Design of a Code Generator30Target Language and Addresses in Target Code31Basic Blocks and Flow Graphs32Optimization of Basic Blocks
    4 min read
    Topic 2 of 32
    Topic Study Note Illustration
    02

    Structure of a Compiler and its Phases

    The structure of a compiler refers to how a compiler is organized into different parts (phases) , where each phase performs a specific task in translating a...

    πŸ“˜ 1. Structure of a Compiler

    βœ… Definition

    The structure of a compiler refers to how a compiler is organized into different parts (phases), where each phase performs a specific task in translating a program from high-level language β†’ machine code.


    πŸ“Š Overall Structure (Diagram)

    Source Program
          ↓
    Lexical Analysis
          ↓
    Syntax Analysis
          ↓
    Semantic Analysis
          ↓
    Intermediate Code Generation
          ↓
    Code Optimization
          ↓
    Code Generation
          ↓
    Target (Machine Code)
    

    πŸ”— Supporting Components

    Two important components work with all phases:

    • Symbol Table
    • Error Handler

    πŸ”Ή Symbol Table

    • Stores information about variables, functions, identifiers

    • Example:

      Variable Name | Type | Scope
      x             | int  | global
      

    πŸ”Ή Error Handler

    • Detects and reports errors in each phase
    • Helps in debugging

    🧠 2. Phases of a Compiler (Step-by-Step)

    There are 6 main phases:


    πŸ” 1. Lexical Analysis (Scanner)

    βœ… Definition

    Converts source code into tokens (smallest units).


    πŸ“Œ Example

    int x = 10;
    

    πŸ‘‰ Tokens:

    int | x | = | 10 | ;
    

    πŸ”§ Tasks

    • Remove spaces and comments
    • Identify keywords, identifiers, operators

    ⚠️ Errors

    • Invalid characters
    • Unknown symbols

    ⭐ Key Point (Exam)

    πŸ‘‰ β€œLexical analyzer converts characters β†’ tokens”


    πŸ” 2. Syntax Analysis (Parser)

    βœ… Definition

    Checks whether tokens follow grammar rules of the language.


    πŸ“Œ Example

    Correct:

    int x = 10;
    

    Incorrect:

    int = x 10;
    

    🌳 Syntax Tree Diagram

        =
       / \
      x  10
    

    πŸ”§ Tasks

    • Build parse tree / syntax tree
    • Check structure

    ⚠️ Errors

    • Missing semicolon
    • Wrong structure

    ⭐ Key Point

    πŸ‘‰ β€œParser checks grammar using CFG (Context-Free Grammar)”


    πŸ” 3. Semantic Analysis

    βœ… Definition

    Checks the meaning of the program.


    πŸ“Œ Example

    int x;
    x = "Hello";   ❌ Error
    
    • Type mismatch

    πŸ”§ Tasks

    • Type checking
    • Variable declaration check
    • Scope resolution

    ⚠️ Errors

    • Undeclared variables
    • Type mismatch

    ⭐ Key Point

    πŸ‘‰ β€œEnsures program is logically correct”


    πŸ” 4. Intermediate Code Generation

    βœ… Definition

    Generates machine-independent intermediate code.


    πŸ“Œ Example

    x = a + b
    

    πŸ‘‰ Intermediate Code:

    t1 = a + b
    x = t1
    

    πŸ”§ Tasks

    • Convert to simple instructions
    • Easier for optimization

    ⭐ Key Point

    πŸ‘‰ β€œCreates intermediate representation (IR)”


    πŸ” 5. Code Optimization

    βœ… Definition

    Improves code to make it faster and efficient.


    πŸ“Œ Example

    Before:

    x = a + 0
    

    After:

    x = a
    

    πŸ”§ Tasks

    • Remove unnecessary code
    • Reduce execution time

    ⭐ Key Point

    πŸ‘‰ β€œOptimization improves performance without changing output”


    πŸ” 6. Code Generation

    βœ… Definition

    Converts intermediate code into machine code.


    πŸ“Œ Example

    ADD R1, R2
    MOV R1, x
    

    πŸ”§ Tasks

    • Assign registers
    • Generate final instructions

    ⭐ Key Point

    πŸ‘‰ β€œFinal phase produces executable code”


    πŸ§ͺ 3. Example (Full Flow)

    Input Code:

    int x = a + b;
    

    Output at Each Phase:

    Phase Output
    Lexical int, x, =, a, +, b
    Syntax Parse tree
    Semantic Type checked
    Intermediate t1 = a + b
    Optimization t1 = a + b
    Code Generation Machine instructions

    🎯 4. Important Exam Concepts

    πŸ‘‰ Frequently asked:

    • List and explain all compiler phases
    • Draw compiler structure diagram
    • Role of symbol table
    • Difference between syntax and semantic analysis
    • Examples of each phase

    πŸ“ 5. Short Notes (Quick Revision)

    • Compiler has 6 phases
    • Each phase transforms code step-by-step
    • Symbol table + error handler support all phases
    • Output: efficient machine code

    πŸ“Š 6. Final Summary Table

    Phase Input Output Main Function Errors
    Lexical Analysis Source code Tokens Break code into tokens Invalid symbols
    Syntax Analysis Tokens Parse tree Check grammar Syntax errors
    Semantic Analysis Parse tree Annotated tree Check meaning Type errors
    Intermediate Code Annotated tree IR code Generate intermediate form β€”
    Code Optimization IR code Optimized IR Improve efficiency β€”
    Code Generation Optimized IR Machine code Final output β€”

    βœ… Final Conclusion

    • A compiler works in multiple phases, each with a specific role

    • Understanding each phase helps in:

      • Debugging programs
      • Designing compilers
      • Exam preparation

    ← Previous topic 1
    Introduction to interpreter and compiler
    Next topic 3 β†’
    Lexical Analyzer and Input Buffering

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    4mremaining
    634words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Compiler Construction