Part IA Michaelmas, Lent Term
Regular Expression Matching
The Matching Relation
Definition
The matching relation means “string matches regex .”
Equivalently: .
Inductive Definition of Matching
Atoms
- The empty string matches
- The symbol matches the regex
Note: No string matches (never).
Union
String matches if matches OR matches .
Concatenation
String matches if matches and matches .
Star
The empty string matches , and if matches and matches , then matches .
Example Derivations
Matching against
(ab, Concat(a,b))
/ \
(a, a) (b, b)
Matching against
One possible derivation:
(ab, Concat(Star(a), Star(b)))
/ \
(a, Star(a)) (b, Star(b))
/ \ / \
(a, a) (ε, Star(a)) (b, b) (ε, Star(b))
Matching against
(aaab, Concat(Star(a), b))
/ \
(aaa, Star(a)) (b, b)
/ \
(aa, Star(a)) (a, a)
/ \
(a, Star(a)) (a, a)
|
(ε, Star(a))
Proving Properties by Rule Induction
Example: Star Matches Repetitions
Claim: matches iff where each matches .
Proof:
Rule induction on matching derivation for :
- Base case: = empty concatenation of -matches.
- Inductive case: from and . By IH, with each matching . So where and each match .
By induction on : if with each matching :
- : matches by axiom.
- : matches by concatenating matches.
Non-Matching
Establishing Non-Matching
To prove does NOT match :
- Enumerate all possible derivations
- Show none succeed
- Or use meta-theorems about
Example: does not match
would need to match or concatenation of matches.
- Not , not a single symbol matching
- For concatenation: would need split with matching and matching
- : doesn’t match
- : doesn’t match
No derivation exists.
Complexity of Matching
Decision Problem
Given and , does ?
Algorithms
- Backtracking: Try all ways to match; exponential worst case
- Thompson’s algorithm: Convert to NFA, simulate in
- Glushkov’s algorithm: Direct NFA construction
- DFA simulation: After NFA-to-DFA, matching
Summary
- Matching is defined inductively by rules
- Union: match either branch
- Concat: split and match both parts
- Star: match zero or more repetitions
- Non-matching established by showing no derivation exists
- Efficient algorithms exist for regex matching