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 PortalCOMP3149Token Specification Re…
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
    3 min read
    Topic 4 of 32
    Topic Study Note Illustration
    04

    Specifications and Recognitions of Tokens

    A token is the smallest meaningful unit of a program that the compiler understands.

    πŸ“˜ 1. Introduction to Tokens

    βœ… Definition

    A token is the smallest meaningful unit of a program that the compiler understands.


    πŸ“Œ Example

    int x = 10;
    

    πŸ‘‰ Tokens:

    int | x | = | 10 | ;
    

    🧠 Key Terms

    Term Meaning
    Token Category (e.g., identifier, keyword)
    Lexeme Actual text (e.g., x, 10)
    Pattern Rule used to define token

    ⭐ Important (Exam)

    πŸ‘‰ Token = <Token Name, Attribute Value>

    Example:

    (id, pointer to symbol table)
    (num, 10)
    

    🧾 2. Specification of Tokens

    βœ… Definition

    Specification of tokens means defining tokens using rules or patterns so that the lexical analyzer can identify them.


    πŸ”§ How Tokens are Specified?

    Tokens are specified using:

    πŸ”Ή 1. Regular Expressions (Very Important)

    πŸ‘‰ A regular expression is a pattern used to describe a set of strings.


    πŸ“Œ Common Token Specifications

    1. Identifier

    πŸ‘‰ Rule:

    letter (letter | digit)*
    

    πŸ‘‰ Examples:

    x, count, total1
    

    2. Number

    πŸ‘‰ Rule:

    digit+
    

    πŸ‘‰ Examples:

    10, 25, 100
    

    3. Keyword

    πŸ‘‰ Predefined words:

    int, if, while, return
    

    4. Operators

    +, -, *, /, =
    

    5. Delimiters

    ;, (, ), {, }
    

    πŸ“Š Example of Token Specification Table

    Token Type Regular Expression Example
    Identifier letter (letter digit)* var1
    Number digit+ 123
    Operator + - *
    Whitespace space/tab/newline β€”

    ⭐ Key Points (Exam Focus)

    • Tokens are defined using regular expressions
    • Each token has a pattern
    • Keywords are fixed, identifiers are variable

    πŸ” 3. Recognition of Tokens

    βœ… Definition

    Recognition of tokens is the process of identifying tokens in the input string using the specified patterns.


    πŸ”§ How Tokens are Recognized?

    Using:

    πŸ”Ή Finite Automata (Very Important)

    • DFA (Deterministic Finite Automaton)
    • NFA (Non-Deterministic Finite Automaton)

    πŸ“Š Flow of Recognition

    Input String β†’ Pattern Matching β†’ Token Identified
    

    🧠 Step-by-Step Recognition Process

    1. Read input character
    2. Match with token patterns
    3. Form lexeme
    4. Assign token type
    5. Move to next token

    πŸ“Œ Example

    Input:

    sum = a + 5;
    

    Recognition:

    sum β†’ identifier
    =   β†’ operator
    a   β†’ identifier
    +   β†’ operator
    5   β†’ number
    ;   β†’ delimiter
    

    πŸ”„ 4. Role of Finite Automata

    βœ… What is Finite Automaton?

    A machine used to recognize patterns in input strings.


    πŸ“Š Example: Identifier Recognition

    Start β†’ letter β†’ (letter/digit)* β†’ Accept
    

    🌳 Simple DFA Diagram

    (q0) --letter--> (q1)
    (q1) --letter/digit--> (q1)
    
    • q0 = start state
    • q1 = accepting state

    ⭐ Key Point (Exam)

    πŸ‘‰ β€œRegular Expressions β†’ Finite Automata β†’ Token Recognition”


    ⚠️ 5. Errors in Token Recognition

    Types of Errors:

    • Invalid symbol β†’ @
    • Invalid identifier β†’ 1abc
    • Unterminated string

    πŸ§ͺ 6. Complete Example (Specification + Recognition)

    Input:

    int total = 25;
    

    Step 1: Specification

    • int β†’ keyword
    • total β†’ identifier
    • 25 β†’ number

    Step 2: Recognition

    Lexeme Token
    int keyword
    total identifier
    = operator
    25 number
    ; delimiter

    🎯 7. Important Exam Concepts

    πŸ‘‰ Frequently asked:

    • Define token, lexeme, pattern
    • What is token specification?
    • Role of regular expressions
    • What is token recognition?
    • DFA for identifier
    • Difference between specification and recognition

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

    Specification

    • Defines tokens
    • Uses regular expressions

    Recognition

    • Identifies tokens
    • Uses DFA/NFA

    Key Relation

    Regular Expression β†’ DFA β†’ Token Recognition
    

    πŸ“Š 9. Final Summary Table

    Aspect Specification of Tokens Recognition of Tokens
    Definition Defining token patterns Identifying tokens in input
    Tool Used Regular Expressions Finite Automata (DFA/NFA)
    Purpose Describe tokens Detect tokens
    Stage Before scanning During scanning
    Example digit+ Recognizing "123" as number
    Output Token rules Actual tokens
    Importance High Very High

    βœ… Final Conclusion

    • Specification tells what tokens look like
    • Recognition tells how tokens are found
    • Both are essential for lexical analysis

    ← Previous topic 3
    Lexical Analyzer and Input Buffering
    Next topic 5 β†’
    Regular Expressions and Finite Automata

    Past Papers

    Open this section to view and download past papers

    Click on Show Past Papers to load available exam papers.

    Reading Progress
    0%
    3mremaining
    588words
    Beginnerlevel
    On This Page

    No headings found

    Study Toolkit

    Subject Portal

    Compiler Construction