ScholarQuill logoScholarQuillUniversity Notes
  • Notes
  • Past Papers
  • Blogs
  • Todo
Login
ScholarQuill logoScholarQuillUniversity Notes
Login
NotesPast PapersBlogsTodo
More
SubjectsDiscussionCGPA CalculatorGPA CalculatorStudent PortalCourse Outline
About
About usPrivacy PolicyReportContact
Notes
Past Papers
Blogs
Todo
Analytics
    Current Subject
    🧩
    Compiler Construction
    COMP3149
    Progress0 / 32 topics
    Topics
    1. Introduction to interpreter and compiler2. Structure of a Compiler and its Phases3. Lexical Analyzer and Input Buffering4. Specifications and Recognitions of Tokens5. Regular Expressions and Finite Automata6. Transition Table and Transition Graph7. Definitions of Grammars, Derivations, and Parse Trees8. Ambiguity, Associativity, and Precedence of Operators9. Syntax Analysis and Role of the Parser10. Eliminating Ambiguity, Left Recursion, and Left Factoring11. Top-Down Parsing and Recursive-Descent Parsing12. First and Follow Sets13. LL(1) Grammars and Non-recursive Predictive Parsing14. Bottom-Up Parsing: Reductions and Shift-Reduce Parsing15. LR Parsing and LR(0) Parsers16. LR(0) Automaton and Parsing Table17. Shift-Reduce Conflicts18. SLR(1) Parsers: Automaton and Parsing Table19. LR(1) Parsers: Automaton and Parsing Table20. LALR Parsing: Automaton and Parsing Table21. Semantic Analysis and Intermediate Code Generation22. Three Address Code23. Tasks of Semantic Analyzer and Types of Errors24. Type Checking and Environments25. Type Conversions: Implicit vs Explicit26. Back Patching and Switch Statements27. Storage Organization and Stack Allocation of Space28. Heap Management and Optimization29. Code Generation: Design of a Code Generator30. Target Language and Addresses in Target Code31. Basic Blocks and Flow Graphs32. Optimization of Basic Blocks
    COMP3149›Specifications and Recognitions of Tokens
    Compiler ConstructionTopic 4 of 32

    Specifications and Recognitions of Tokens

    3 minread
    588words
    Beginnerlevel

    📘 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 load past papers

    Click on Show Past Papers to see past papers.
    On This Page
      Reading Stats
      Est. reading time3 min
      Word count588
      Code examples0
      DifficultyBeginner