Definition
Abstract Syntax
Regular expressions over alphabet Σ are defined inductively:
| Constructor | Syntax | Meaning |
|---|
| Null | ε | Matches the empty string only |
| Never | ∅ | Matches nothing |
| Symbol | a for a∈Σ | Matches the single symbol a |
| Union | $R_1 | R_2$ |
| Concat | R1⋅R2 or R1R2 | Matches R1 followed by R2 |
| Star | R∗ | Matches zero or more repetitions of R |
Concrete Syntax
The typical concrete syntax uses:
| for union (alternation)
- Juxtaposition for concatenation:
ab means a then b
* for Kleene star
() for grouping
Precedence (highest to lowest)
- Star (
*)
- Concatenation (implicit)
- Union (
|)
The Language of a Regular Expression
Definition
Each regex R denotes a language L(R)⊆Σ∗, defined inductively:
| Regex R | Language L(R) |
|---|
| ε | {ε} |
| ∅ | ∅ |
| a | {a} |
| $R_1 | R_2$ |
| R1R2 | L(R1)⋅L(R2)={uv:u∈L(R1),v∈L(R2)} |
| R∗ | L(R)∗=⋃n≥0L(R)n |
Examples
Single Symbols
- L(a)={a}
- L(ab)={ab}
- L(a∣b)={a,b}
Kleene Star
- L(a∗)={ε,a,aa,aaa,…}
- L((a∣b)∗)=Σ∗ (all strings over {a,b})
- L(∅∗)={ε}
Combinations
- L(a∗b∗)={anbm:n,m≥0}
- L((ab)∗)={ε,ab,abab,ababab,…}
- L(a(b∣c)∗)={a,ab,ac,abb,abc,acb,acc,…}
Idioms and Patterns
Every String
(a∣b)∗=Σ∗ (when Σ={a,b})
Strings Starting with a
a(a∣b)∗
Strings Ending with a
(a∣b)∗a
Strings Containing a
(a∣b)∗a(a∣b)∗
Even-Length Strings
((a∣b)(a∣b))∗
At Least One a
(a∣b)∗a(a∣b)∗
Exactly One a
b∗ab∗
Algebraic Laws
Union
- Commutative: R∣S=S∣R
- Associative: (R∣S)∣T=R∣(S∣T)
- Identity: R∣∅=R
- Idempotent: R∣R=R
Concatenation
- Associative: (RS)T=R(ST)
- Identity: Rε=R=εR
- Zero: R∅=∅=∅R
- Distributive over union: R(S∣T)=RS∣RT and (R∣S)T=RT∣ST
Star
- ε∗=ε
- ∅∗=ε
- R∗∗=R∗
- R∗=ε∣RR∗
Limitations
Not all languages are regular. For example:
- {anbn:n≥0} is NOT regular
- {ww:w∈Σ∗} is NOT regular
- Balanced parentheses are NOT regular
These require more expressive formalisms (context-free grammars, etc.).
Summary
- Regular expressions are built from atoms (ε, ∅, symbols) and constructors (union, concat, star)
- Each regex denotes a language L(R)
- Precedence: star > concat > union
- Useful for pattern matching, lexical analysis
- Not all languages are regular