CSE411

CSE411: Introduction to Compilers

Lexer (Scanner)

Jooyong Yi (UNIST)

2026 Fall
CSE411

Lexing

        position  =  initial  +  rate   *   60               source program
                           ↓
        |------------------------------------|
        |      Lexical Analyzer (Lexer)      |
        |------------------------------------|
                           ↓
        <ID, 1> <=> <ID, 2> <+> <ID, 3> <*> <INT, 60>        a sequence of tokens
2026 Fall
CSE411

How would you write a lexer?

2026 Fall
CSE411

How would you write a lexer?

        position  ⟼ ID
        initial   ⟼ ID
        rate      ⟼ ID

        60        ⟼ INT
        15        ⟼ INT
        0         ⟼ INT

        =         ⟼ =
        +         ⟼ +
        *         ⟼ *
2026 Fall
CSE411

Regular Expression for INT?

        60        ⟼ INT
        15        ⟼ INT
        0         ⟼ INT
2026 Fall
CSE411

Regular Expression for INT

        60        ⟼ INT
        15        ⟼ INT
        0         ⟼ INT
'0' | ('1'| '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9') ('0'| '1'| '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9')*
  • Shorthand notation: 0 | [1-9] [0-9]*
2026 Fall
CSE411

Regular Expression for ID?

        position  ⟼ ID
        initial   ⟼ ID
        RATE      ⟼ ID
        a_123     ⟼ ID
        _abc      ⟼ ID
        a_        ⟼ ID
2026 Fall
CSE411

Regular Expression for ID

        position  ⟼ ID
        initial   ⟼ ID
        RATE      ⟼ ID
        a_123     ⟼ ID
        _abc      ⟼ ID
        a_b_      ⟼ ID
[a-zA-Z_] [a-zA-Z0-9_]*
2026 Fall
CSE411

Regular Expressions

  • 'a', 'int', 'if': An ordinary character or a string stands for itself literally.
  • : The empty string
  • : Alternation, choosing from or .
  • [0-9]: 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
  • : Concatenation, an followed by an .
  • : Repetition (zero or more times)
  • : Repetition (one or more times)
  • : Optional, zero or one occurrences of .
2026 Fall
CSE411
  • Input (RE): 0 | [1-9] [0-9]*
  • Q: Is '0' INT?
  • Q: Is '10' INT?
  • Q: Is '01' INT?
  • Q: Given an arbitrary input string s, how can we determine if it is an INT?
2026 Fall
CSE411

RE and DFA (Deterministic Finite Automaton)

  • Input (RE): 0 | [1-9] [0-9]*
  • Output (DFA):

2026 Fall
CSE411

RE and DFA (Deterministic Finite Automaton)

Let be a regular expression. Then there exists a deterministic finite automaton that accepts .

2026 Fall
CSE411

How can we convert RE into DFA?

  • Input (RE): 0 | [1-9] [0-9]*
  • Output (DFA):

2026 Fall
CSE411

How can we convert RE into DFA?

  • Input (RE): ID | IF
    • ID: [A-Za-z_] [A-Za-z0-9_]*
    • IF: 'if'
  • Output (DFA):
2026 Fall
CSE411

How can we convert RE into DFA?

  • Input (RE): ID | IF
    • ID: [A-Za-z_] [A-Za-z0-9_]*
    • IF: 'if'
  • Output (DFA):

2026 Fall
CSE411

How can we convert RE into DFA?

  • Input (RE): ID | IF
    • ID: [A-Za-z_] [A-Za-z0-9_]*
    • IF: 'if'
  • Intermediate Output (NFA):

2026 Fall
CSE411

DFA and NFA

  • DFA and NFA
    • : a finite set of states.
    • : an input alphabet.
      • For DFA only:
    • : the initial state
    • : a set of final (accepting) states
    • : a transition function.
      • For DFA:
      • For NFA:
2026 Fall
CSE411

Theorems

  • Let be a regular expression. Then there exists a deterministic finite automaton that accepts .
  • Let be a regular expression. Then there exists an NFA that accepts .
  • For every NFA , there exists a DFA such that and accept the same language.
  • There exists an algorithm to convert any NFA into an equivalent DFA .
2026 Fall
CSE411

How can we convert RE into DFA?

  • RE NFA DFA
2026 Fall
CSE411

RE NFA

  • RE:
  • NFA:
2026 Fall
CSE411

RE NFA

  • RE:
  • NFA:
2026 Fall
CSE411

RE NFA

  • RE:
  • NFA:
2026 Fall
CSE411

RE NFA

  • RE:
  • NFA:
2026 Fall
CSE411

RE NFA

  • RE:
  • NFA:
2026 Fall
CSE411

RE NFA

  • RE:
  • NFA:
2026 Fall
CSE411

NFA DFA

How to transform NFA to DFA?

2026 Fall
CSE411

NFA DFA

  • Map multiple NFA states to a single DFA state
2026 Fall
CSE411

NFA DFA

  • Bisimulate both automata
NFA State DFA State a b
{0, 1, 2, 4, 7} A ? ?
2026 Fall
CSE411

NFA DFA

NFA State DFA State a b
{0, 1, 2, 4, 7} A B ?
{1, 2, 3, 4, 6, 7, 8} B ? ?
2026 Fall
CSE411

NFA DFA

NFA State DFA State a b
{0, 1, 2, 4, 7} A B C
{1, 2, 3, 4, 6, 7, 8} B ? ?
{1, 2, 4, 5, 6, 7} C ? ?
2026 Fall
CSE411

NFA DFA

NFA State DFA State a b
{0, 1, 2, 4, 7} A B C
{1, 2, 3, 4, 6, 7, 8} B B ?
{1, 2, 4, 5, 6, 7} C ? ?
2026 Fall
CSE411

NFA DFA

NFA State DFA State a b
{0, 1, 2, 4, 7} A B C
{1, 2, 3, 4, 6, 7, 8} B B D
{1, 2, 4, 5, 6, 7} C ? ?
{1, 2, 4, 5, 6, 7, 9} D ? ?
2026 Fall
CSE411

NFA DFA

NFA State DFA State a b
{0, 1, 2, 4, 7} A B C
{1, 2, 3, 4, 6, 7, 8} B B D
{1, 2, 4, 5, 6, 7} C B C
{1, 2, 4, 5, 6, 7, 9} D ? ?
2026 Fall
CSE411

NFA DFA

NFA State DFA State a b
{0, 1, 2, 4, 7} A B C
{1, 2, 3, 4, 6, 7, 8} B B D
{1, 2, 4, 5, 6, 7} C B C
{1, 2, 4, 5, 6, 7, 9} D B ?
2026 Fall
CSE411

NFA --> DFA

NFA State DFA State a b
{0, 1, 2, 4, 7} A B C
{1, 2, 3, 4, 6, 7, 8} B B D
{1, 2, 4, 5, 6, 7} C B C
{1, 2, 4, 5, 6, 7, 9} D B E
{1, 2, 4, 5, 6, 7, 10} E ? ?
2026 Fall
CSE411

NFA DFA

NFA State DFA State a b
{0, 1, 2, 4, 7} A B C
{1, 2, 3, 4, 6, 7, 8} B B D
{1, 2, 4, 5, 6, 7} C B C
{1, 2, 4, 5, 6, 7, 9} D B E
{1, 2, 4, 5, 6, 7, 10} E B C
2026 Fall
CSE411

Subset Construction Algorithm

  1. Create the DFA start state: =
2026 Fall
CSE411

-Closure

  • Given a set and its -closure ,
2026 Fall
CSE411

-Closure

  • Given a set , its -closure is the smallest set satisfying the following:
2026 Fall
CSE411

Subset Construction Algorithm

  1. For each DFA state and each input character , find all NFA states reachable by : .
  2. Include all states reachable afterward through -transitions: .
  3. Make a DFA transition: .
NFA State DFA State a b
{0, 1, 2, 4, 7} A B C
2026 Fall
CSE411

Subset Construction Algorithm

D0 := ε-closure({N0})
D := {D0}
W := {D0}  // worklist
while W ≠ ∅:
   remove Q from W
   for α in Σ:
      T := ∅
      for S in Q:
         T := T ∪ δ_N(S, α)
      T' := ε-closure(T)
      D' := D ∪ {T'}
      δ_D(Q, α) = T' // update δ_D
      if T' ∉ D:
         W := W ∪ {T'}
      D := D'
F_D = {Q ∊ D | Q ∩ F_N ≠ ∅} // define F_D      
2026 Fall