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 PortalCOMP3149Lexical Analyzer Input…
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 3 of 32
    Topic Study Note Illustration
    03

    Lexical Analyzer and Input Buffering

    A Lexical Analyzer is the first phase of a compiler that reads the source code character by character and converts it into tokens .

    πŸ“˜ 1. Lexical Analyzer (Scanner)

    βœ… Definition

    A Lexical Analyzer is the first phase of a compiler that reads the source code character by character and converts it into tokens.

    πŸ‘‰ It acts as an interface between source code and the parser (syntax analyzer).


    πŸ”„ Basic Working

    πŸ“Š Diagram

    Source Code β†’ [Lexical Analyzer] β†’ Tokens β†’ Syntax Analyzer
    

    πŸ“Œ What are Tokens?

    A token is the smallest meaningful unit in a program.

    Example:

    int x = 10;
    

    πŸ‘‰ Tokens:

    int | x | = | 10 | ;
    

    🧠 Components of a Token

    Each token has:

    • Token Name (Type) β†’ keyword, identifier, operator
    • Attribute Value β†’ actual value or pointer

    Example:

    Token Type Attribute
    int keyword β€”
    x identifier pointer to symbol table
    10 number value = 10

    πŸ”§ Functions of Lexical Analyzer

    1. Reads input characters
    2. Groups them into tokens
    3. Removes whitespace and comments
    4. Identifies keywords, identifiers, constants
    5. Adds identifiers to Symbol Table
    6. Reports lexical errors

    ⚠️ Lexical Errors

    Examples:

    • Invalid symbols β†’ @, #
    • Wrong identifiers β†’ 2abc
    • Unclosed strings

    ⭐ Key Points (Exam Focus)

    πŸ‘‰ Converts characters β†’ tokens πŸ‘‰ Ignores spaces and comments πŸ‘‰ Works using patterns (regular expressions)


    🧠 2. Role of Lexical Analyzer in Compiler

    • First phase β†’ foundation of compilation
    • Simplifies syntax analysis
    • Improves efficiency

    πŸ” 3. Input Buffering

    βœ… Definition

    Input Buffering is a technique used by the lexical analyzer to read input efficiently by storing it in memory buffers instead of reading one character at a time.


    ❓ Why Input Buffering is Needed?

    Reading character-by-character from file is slow.

    πŸ‘‰ Solution: Use buffers to read large chunks at once.


    πŸ”„ 4. Types of Input Buffering

    πŸ”Ή 1. Single Buffer Scheme

    πŸ“Š Diagram

    [ Input Buffer ]
    | a | b | c | d | e | EOF |
    

    ❌ Problems

    • Frequent I/O operations
    • Slow performance
    • Difficult to handle lookahead

    πŸ”Ή 2. Double Buffer Scheme (Important)

    βœ… Definition

    Uses two buffers to improve efficiency.


    πŸ“Š Diagram

    Buffer 1         Buffer 2
    [ a b c d ]      [ e f g h ]
        ↑                ↑
     forward pointer     reloaded
    

    πŸ”§ Working (Step-by-Step)

    1. Two buffers of equal size are used
    2. Each buffer has a sentinel (EOF marker) at end
    3. Lexical analyzer reads from one buffer
    4. When buffer ends β†’ switch to second buffer
    5. First buffer reloads while second is used

    πŸ“Œ Important Concept: Sentinels

    βœ… Definition

    A sentinel is a special character (usually EOF) placed at the end of buffer to indicate buffer boundary.


    ⭐ Advantages

    • No need to check end-of-buffer repeatedly
    • Faster scanning

    πŸ”‘ Pointers Used

    • Lexeme Begin Pointer β†’ start of token
    • Forward Pointer β†’ scans ahead

    πŸ“Š Pointer Movement Example

    int x = 10;
    ^    ^
    LB   FP
    

    (LB = Lexeme Begin, FP = Forward Pointer)


    πŸ§ͺ 5. Example (Token Recognition with Buffering)

    Input:

    count = count + 1;
    

    Steps:

    1. Buffer loads input

    2. FP moves character by character

    3. Tokens identified:

      count | = | count | + | 1 | ;
      

    🎯 6. Important Exam Concepts

    πŸ‘‰ Frequently asked:

    • Definition of lexical analyzer
    • Functions of lexical analyzer
    • What are tokens?
    • Input buffering techniques
    • Double buffer scheme diagram
    • Sentinel concept
    • Lexeme begin & forward pointer

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

    Lexical Analyzer

    • First phase
    • Converts characters β†’ tokens
    • Removes spaces/comments
    • Uses symbol table

    Input Buffering

    • Improves reading speed
    • Uses buffers instead of char-by-char reading

    Double Buffer

    • Two buffers
    • Uses sentinel
    • Efficient and fast

    πŸ“Š 8. Final Summary Table

    Topic Description Key Feature Advantage Exam Importance
    Lexical Analyzer Converts source code to tokens First compiler phase Simplifies parsing Very High
    Token Smallest unit Type + attribute Easy processing High
    Input Buffering Efficient input reading Uses memory buffer Faster than char reading High
    Single Buffer One buffer Simple Slow Medium
    Double Buffer Two buffers Uses sentinel Fast, efficient Very High
    Sentinel End marker EOF symbol Avoids checks Very High
    Pointers Track positions LB & Forward Helps scanning High

    βœ… Final Conclusion

    • The Lexical Analyzer is crucial because it prepares clean input for the parser.
    • Input Buffering, especially double buffering, makes the process fast and efficient.
    • Concepts like tokens, lexemes, sentinel, and pointers are very important for exams.

    ← Previous topic 2
    Structure of a Compiler and its Phases
    Next topic 4 β†’
    Specifications and Recognitions of Tokens

    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
    693words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Compiler Construction