Skip to content
The Lexer
step 1/5

Reading — step 1 of 5

Read

~2 min readLexing & Parsing

The Lexer

The lexer (or scanner, tokenizer) reads source characters and emits a stream of tokens.

Token types in C:

  • Keywords: int, char, if, else, while, return, etc.
  • Identifiers: variable/function names.
  • Literals: integers (123, 0x7F, 0o17), floats (3.14), strings ("hello"), chars ('A').
  • Operators: + - * / % == != < > <= >= && || ! ~ & | ^ << >> = += -= ...
  • Punctuation: ( ) { } [ ] ; , . -> ?:
python

Tokens drop whitespace + comments — they're not semantically meaningful for parsing (except as separators).

Edge cases:

  • Maximal munch: == should be ONE token, not two =. Always try longest match.
  • Hex/octal/binary: 0x1F, 0o17, 0b1010 (C23+).
  • String escapes: "\n", "\\", "\"".
  • Numeric suffixes: 42L, 3.14f, 1e10.

Real lexers may be hand-written (chibicc, Clang) or generated (lex/flex). Hand-written is more flexible; generated is more declarative.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…