Lexical analysis

Scanner Rules

Write token rules as regular expressions and watch maximal munch split an input into (token, lexeme) pairs.

  • Lecture reference: Lexical Analysis II · slides 4–12
  • Lecture reference: Lexical Analysis · slides 4–17
  • Lecture reference: Intro (cont’d): Compiler structure with examples · slide 5
Lecture reference: Lexical Analysis II · slide 7 f+3 +g

R = Whitespace | Integer | Identifier | '+'. The slide lists (Whitespace, " "); maximal munch matches both spaces.

Token tuples are ?

Show answer

(Identifier, "f"), (Plus, "+"), (Integer, "3"), (Whitespace, " "), (Plus, "+"), (Identifier, "g"). The slide writes (Whitespace, " ") for the two spaces.

Token rules

Rules

Listed first wins a tie

R = Whitespace (R1) | Integer (R2) | Identifier (R3) | '+' (R4)

Input

Tab inserts a tab. Press Escape, then Tab, to move focus out of the editor.

7 characters

Tokens 6 reported

Scanner

Step 1 of 6
Longest match: "f" (1 character). Only Identifier (R3) matches it → (Identifier, "f").
Which rules match each prefix of the input from position 0. Rows are rules, columns are prefix lengths.
x1…xi Rji = 1 "f"i = 2 "f+"
R1 Whitespace ' '+
no match no match
R2 Integer digit+
no match no match
R3 Identifier letter (letter | digit)*
chosen match no match
R4 Plus '+'
no match no match

✓x1…xi ∈ L(Rj) longest match, max(i, k) first rule listed, min(j, k)