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
- Lecture reference: Lexical Analysis
- Lecture reference: Intro (cont’d): Compiler structure with examples
Lecture reference: Lexical Analysis II 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 tieR = Whitespace (R1) | Integer (R2) | Identifier (R3) | '+' (R4)
Input
7 characters
Tokens 6 reported
Scanner
Step 1 of 6
Longest match: "f" (1 character). Only Identifier (R3) matches it → (Identifier, "f").
| x1…xi Rj | i = 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)