Bölüm 5 · Simülatör

String Parsing: Finite State Machine

← Bölüm 5

Rule: the first character must be a letter or underscore.

State Diagram

letter, _ digit, other, end letter, digit, _ end other START IN_ID ACCEPT REJECT

Transition Table

Current StateCharacter ClassNext State
STARTLETTERIN_ID
STARTUNDERSCOREIN_ID
STARTDIGITREJECT
STARTOTHERREJECT
IN_IDLETTERIN_ID
IN_IDDIGITIN_ID
IN_IDUNDERSCOREIN_ID
IN_IDOTHERREJECT

    String Parsing: Finite State Machine

    Validate an identifier step by step using states, character classes, and explicit transitions.

    How to use

    • Type a sample input (up to 24 characters) and press Load Input.
    • Step Forward reads one character, Auto Run continues until the machine accepts or rejects, Reset reloads the input.
    • Yellow cell: the next character to read. Green cells: characters already consumed. Red cell: the character that caused the rejection.
    • The state diagram and the transition table highlight the transition that was just taken.

    The identifier rule

    An identifier starts with a letter or underscore, and the remaining characters are letters, digits, or underscores. Each character is first mapped to a character class (LETTER, DIGIT, UNDERSCORE, OTHER); the next state depends only on the current state and that class.

    • START: nothing read yet. LETTER or UNDERSCORE moves to IN_ID; DIGIT or OTHER rejects.
    • IN_ID: a valid identifier has been read so far. LETTER, DIGIT and UNDERSCORE keep the machine in IN_ID; OTHER rejects.
    • At the end of the input the string is accepted only if the machine is in IN_ID; an empty input is rejected.

    Why a state machine?

    Lexers (tokenizers) in compilers and interpreters recognize identifiers, numbers and keywords with exactly this kind of deterministic finite automaton: one table lookup per character, no backtracking.

    Complexity

    O(n) time for an input of length n (one transition per character) and O(1) extra space; the transition table has |states| × |classes| entries.