Lecture 21: regular expressions and pattern matching [0174]
- February 11, 2025
- Jon Sterling, with contributions from Andrew Pitts, Ian Leslie, Frank Stajano
These lecture notes were prepared by Jon Sterling by adapting prior notes created by Andrew Pitts that I inherited from Frank Stajano.
Concrete and abstract syntax [0175]
- February 11, 2025
- Jon Sterling
Syntax comes in two forms: there is concrete syntax, and this is what people are talking about when they say things like:
I prefer the syntax of Rust to C++.
I (can’t stand, love) the way that Haskell and Python have indentation-sensitive syntax.
Concrete syntax is usually defined in terms of strings of symbols. In general, concrete syntax may use a variety of tools (including brackets, whitespace, etc.) to disambiguiate what would not be ambiguous in a structured representation, e.g. as a syntax tree. The structured representation of syntax as trees or a similar data structure (like a directed acyclic graph) is called abstract syntax. The process of converting concrete syntax to abstract syntax is called parsing.
Here is an example of concrete syntax, in the OCaml language:
if x > 100 then x - 10 else f (f (x + 11))
The OCaml parser then converts this into a tree, with vertices labelled by syntactic forms and subtrees given by arguments. Parenthesisation and operator precedence is now replaced by the parent-child relationship.
Regular expressions [0178]
- February 11, 2025
- Jon Sterling
Using the tools that we have built up in Lecture 20, we can smoothly study both concrete and abstract syntax. Somewhat confusingly, our running example will be the language of regular expressions (this might be confusing because regular expressions are a streamlined way to present the grammar of certain kinds of formal languages—but we are treating the grammar of regular expressions themselves).
Definition Regular expressions (concrete syntax) [0176]
- February 11, 2025
- Jon Sterling
Let \Sigma be an alphabet. We then let \Sigma _{\mathsf {rx}} be the disjoint union of \Sigma with the set {\mathopen {}\left \{\underline {\epsilon },\varnothing , \vert ,*,(,)\right \}\mathclose {}}. Then a concrete syntax of regular expressions over \Sigma can be defined as a formal language over \Sigma _{\mathsf {rx}}, which we present by means of the following syntactic rules:
\begin {gathered} \displaystyle \frac {\displaystyle }{\displaystyle \quad a\quad } \qquad \displaystyle \frac {\displaystyle }{\displaystyle \quad \underline {\epsilon }\quad } \qquad \displaystyle \frac {\displaystyle }{\displaystyle \quad \varnothing \quad } \\ \displaystyle \frac {\displaystyle r}{\displaystyle \quad (r)\quad } \qquad \displaystyle \frac {\displaystyle r\quad s}{\displaystyle \quad r\vert s\quad } \qquad \displaystyle \frac {\displaystyle r\quad s}{\displaystyle \quad rs\quad } \qquad \displaystyle \frac {\displaystyle r}{\displaystyle \quad r*\quad } \end {gathered}Above we let a range over \Sigma and r,s range over \Sigma _{\mathsf {rx}}^\star . We are careful to distinguish the formal symbol \underline {\epsilon } from \epsilon : \epsilon is literally the empty string, whereas \underline {\epsilon } is the symbol representing the regular expression that matches the empty string!
Example Derivation of a concrete regular expression [0177]
- February 11, 2025
- Jon Sterling
The (concrete) regular expression \underline {\epsilon }\vert ab* can be derived in the formal language presented in Definition [0176] as follows:
\displaystyle \frac {\displaystyle \displaystyle \frac {\displaystyle }{\displaystyle \quad \underline {\epsilon }\quad } \quad \displaystyle \frac {\displaystyle \displaystyle \frac {\displaystyle }{\displaystyle \quad a\quad } \quad \displaystyle \frac {\displaystyle \displaystyle \frac {\displaystyle }{\displaystyle \quad b\quad } }{\displaystyle \quad b*\quad } }{\displaystyle \quad ab*\quad } }{\displaystyle \quad \underline {\epsilon }\vert ab* \quad }Definition Regular expressions (abstract syntax) [0179]
- February 11, 2025
- Jon Sterling
The abstract syntax of regular expressions over an alphabet \Sigma will be represented by trees whose vertices are labelled by the symbols \texttt {Union}, \texttt {Concat}, \texttt {Star}, \texttt {Null}, \texttt {Never}, and \texttt {Sym}_{a} for any a:\Sigma . More specifically, each node of a tree will be of the following form:
- A node labelled by either \texttt {Union} or \texttt {Concat} has exactly two children.
- A node labelled by \texttt {Star} has exactly one child.
- A node labelled by \texttt {Null}, \texttt {Never}, or \texttt {Sym}_{a} has no children.
The plain English description above could be expressed as an inductive definition of a subset of the set of all trees.
Example Examples of abstract syntax trees [017A]
- February 11, 2025
- Jon Sterling
We recall the derivation for the concrete regular expression \underline {\epsilon }\vert ab*. This will correspond eventually to the following abstract syntax tree as specified by Definition [0179]:
Observe that we had to decide whether how tightly the concatenation and union operators bind with respect to each other. That is part of defining the parsing relation.
Example Precedence parsing for regular expressions [017B]
- February 11, 2025
- Jon Sterling
Parsing is decribed by a relation from concrete syntax to abstract syntax. Ideally, this relation would be functional and so give rise to a partial function. In order to achieve this, we address ahead of time any ambiguity in how tightly operators bind: for example, ab\vert c should be the union of ab and c, rather than the concatenation of a with b\vert c.
This can be achieved by defining a more complex three-place relation {r} \leadsto {R} \mathrel {@} {n} on concrete syntax r, abstract syntax R, and precedences n. The idea is that we parse operations in the order of increasing tightness/precedence: in this case, that will be \texttt {Union} followed by \texttt {Concat} followed by \texttt {Star} followed by all the atomic regular expressions: \texttt {Sym}_{a}, \texttt {Null}, and \texttt {Never}.
\begin {gathered} \displaystyle \frac {\displaystyle {r} \leadsto {R} \mathrel {@} {0} \quad {s} \leadsto {S} \mathrel {@} {1} }{\displaystyle \quad {r\vert s} \leadsto {\texttt {Union}{\mathopen {}\left (R,S\right )\mathclose {}}} \mathrel {@} {0} \quad } \qquad \displaystyle \frac {\displaystyle {r} \leadsto {R} \mathrel {@} {1} \quad {s} \leadsto {S} \mathrel {@} {2} }{\displaystyle \quad {rs} \leadsto {\texttt {Concat}{\mathopen {}\left (R,S\right )\mathclose {}}} \mathrel {@} {1} \quad } \\[10pt] \displaystyle \frac {\displaystyle {r} \leadsto {R} \mathrel {@} {2} }{\displaystyle \quad {r*} \leadsto {\texttt {Star}{\mathopen {}\left (R\right )\mathclose {}}} \mathrel {@} {2} \quad } \\[10pt] \displaystyle \frac {\displaystyle }{\displaystyle \quad {a} \leadsto {\texttt {Sym}_{a}} \mathrel {@} {3} \quad } \qquad \displaystyle \frac {\displaystyle }{\displaystyle \quad {\underline {\epsilon }} \leadsto {\texttt {Null}} \mathrel {@} {3} \quad } \qquad \displaystyle \frac {\displaystyle }{\displaystyle \quad {\varnothing } \leadsto {\texttt {Never}} \mathrel {@} {3} \quad } \end {gathered}If we are parsing, e.g., unions but we encounter only a symbol, that needs to be allowed too. So we add a rule that coerces a higher precedence to a lower one silently:
\displaystyle \frac {\displaystyle {r} \leadsto {R} \mathrel {@} {n+k} }{\displaystyle \quad {r} \leadsto {R} \mathrel {@} {n} \quad }Finally, we allow the use of parentheses to coerce a precedences in the other direction; this is what would make it possible to concatenate a union with another regular expression.
\displaystyle \frac {\displaystyle {r} \leadsto {R} \mathrel {@} {n - k} }{\displaystyle \quad {(r)} \leadsto {R} \mathrel {@} {n} \quad }We then define the parsing relation {r}\leadsto {R} to hold if and only if {r} \leadsto {R} \mathrel {@} {0} holds. We demonstrate parsing derivation for \underline {\epsilon }\vert ab* and (\underline {\epsilon }\vert a)b* as follows:
\displaystyle \frac {\displaystyle \displaystyle \frac {\displaystyle \displaystyle \frac {\displaystyle }{\displaystyle \quad {\underline {\epsilon }} \leadsto {\texttt {Null}} \mathrel {@} {3}\quad } }{\displaystyle \quad {\underline {\epsilon }} \leadsto {\texttt {Null}} \mathrel {@} {0}\quad } \quad \displaystyle \frac {\displaystyle \displaystyle \frac {\displaystyle \displaystyle \frac {\displaystyle }{\displaystyle \quad {a} \leadsto {\texttt {Sym}_{a}} \mathrel {@} {3}\quad } }{\displaystyle \quad {a} \leadsto {\texttt {Sym}_{a}} \mathrel {@} {1}\quad } \quad \displaystyle \frac {\displaystyle \displaystyle \frac {\displaystyle \displaystyle \frac {\displaystyle }{\displaystyle \quad {b} \leadsto {\texttt {Sym}_{b}} \mathrel {@} {3}\quad } }{\displaystyle \quad {b} \leadsto {\texttt {Sym}_{b}} \mathrel {@} {2}\quad } }{\displaystyle \quad {b*} \leadsto {\texttt {Star}{\mathopen {}\left (\texttt {Sym}_{b}\right )\mathclose {}}} \mathrel {@} {2}\quad } }{\displaystyle \quad {ab*} \leadsto {\texttt {Concat}{\mathopen {}\left (\texttt {Sym}_{a},\texttt {Star}{\mathopen {}\left (\texttt {Sym}_{b}\right )\mathclose {}}\right )\mathclose {}}} \mathrel {@} {1}\quad } }{\displaystyle \quad {\underline {\epsilon }\vert ab*} \leadsto {\texttt {Union}{\mathopen {}\left (\texttt {Null},\texttt {Concat}{\mathopen {}\left (\texttt {Sym}_{a},\texttt {Star}{\mathopen {}\left (\texttt {Sym}_{b}\right )\mathclose {}}\right )\mathclose {}}\right )\mathclose {}}} \mathrel {@} {0} \quad } \displaystyle \frac {\displaystyle \displaystyle \frac {\displaystyle \displaystyle \frac {\displaystyle \displaystyle \frac {\displaystyle \displaystyle \frac {\displaystyle \displaystyle \frac {\displaystyle }{\displaystyle \quad {\underline {\epsilon }} \leadsto {\texttt {Null}} \mathrel {@} {3}\quad } }{\displaystyle \quad {\underline {\epsilon }} \leadsto {\texttt {Null}} \mathrel {@} {0}\quad } \quad \displaystyle \frac {\displaystyle \displaystyle \frac {\displaystyle }{\displaystyle \quad {\underline {\epsilon }} \leadsto {\texttt {Sym}_{a}} \mathrel {@} {3}\quad } }{\displaystyle \quad {\underline {\epsilon }} \leadsto {\texttt {Null}} \mathrel {@} {\texttt {Sym}_{a}}{0}\quad } }{\displaystyle \quad {\underline {\epsilon }\vert a} \leadsto { \texttt {Union}{\mathopen {}\left (\texttt {Null},\texttt {Sym}_{a}\right )\mathclose {}} } \mathrel {@} {0} \quad } }{\displaystyle \quad {{\mathopen {}\left (\underline {\epsilon }\vert a\right )\mathclose {}}} \leadsto { \texttt {Union}{\mathopen {}\left (\texttt {Null},\texttt {Sym}_{a}\right )\mathclose {}} } \mathrel {@} {1} \quad } \quad \displaystyle \frac {\displaystyle \displaystyle \frac {\displaystyle \displaystyle \frac {\displaystyle }{\displaystyle \quad {b} \leadsto {\texttt {Sym}_{b}} \mathrel {@} {3}\quad } }{\displaystyle \quad {b} \leadsto {\texttt {Sym}_{b}} \mathrel {@} {2}\quad } }{\displaystyle \quad {b*} \leadsto {\texttt {Star}{\mathopen {}\left (\texttt {Sym}_{b}\right )\mathclose {}}} \mathrel {@} {2} \quad } }{\displaystyle \quad {{\mathopen {}\left (\underline {\epsilon }\vert a\right )\mathclose {}}b*} \leadsto { \texttt {Concat}{\mathopen {}\left (\texttt {Union}{\mathopen {}\left (\texttt {Null},\texttt {Sym}_{a}\right )\mathclose {}},\texttt {Star}{\mathopen {}\left (\texttt {Sym}_{b}\right )\mathclose {}}\right )\mathclose {}} } \mathrel {@} {1} \quad } }{\displaystyle \quad {{\mathopen {}\left (\underline {\epsilon }\vert a\right )\mathclose {}}b*} \leadsto { \texttt {Concat}{\mathopen {}\left (\texttt {Union}{\mathopen {}\left (\texttt {Null},\texttt {Sym}_{a}\right )\mathclose {}},\texttt {Star}{\mathopen {}\left (\texttt {Sym}_{b}\right )\mathclose {}}\right )\mathclose {}} } \mathrel {@} {0} \quad }Regular expression matching [017C]
- February 11, 2025
- Jon Sterling
We have seen the syntax of regular expressions in both concrete and abstract form. What about semantics, i.e. what do regular expressions actually do? The meaning of a given regular expression over an alphabet \Sigma is a matching predicate on strings over \Sigma that says when a given string is matched by the regular expression. In this course, we are particularly interested in the class of formal languages that can be recognised by regular expressions, and later on we will develop tools that allow us to show that some formal languages are too complex to be recognised by regular expressions. In the meanwhile, we will first explain how matching works.
Definition The matching relation for regular expressions [017D]
- February 11, 2025
- Jon Sterling
The matching relation u\mathrel {\textsf {matches}}R is given by an inductive definition over \Sigma ^\star \times \mathsf {Regexp}_{\Sigma }.
\begin {gathered} \displaystyle \frac {\displaystyle }{\displaystyle \quad {\mathopen {}\left ({a},{\texttt {Sym}_{a}}\right )\mathclose {}} \quad } \quad \displaystyle \frac {\displaystyle }{\displaystyle \quad {\mathopen {}\left ({\epsilon },{\texttt {Null}}\right )\mathclose {}} \quad } \\[10pt] \displaystyle \frac {\displaystyle {\mathopen {}\left ({u},{R}\right )\mathclose {}} }{\displaystyle \quad {\mathopen {}\left ({u},{\texttt {Union}{\mathopen {}\left (R,S\right )\mathclose {}}}\right )\mathclose {}} \quad } \qquad \displaystyle \frac {\displaystyle {\mathopen {}\left ({u},{S}\right )\mathclose {}} }{\displaystyle \quad {\mathopen {}\left ({u},{\texttt {Union}{\mathopen {}\left (R,S\right )\mathclose {}}}\right )\mathclose {}} \quad } \\[10pt] \displaystyle \frac {\displaystyle {\mathopen {}\left ({v},{R}\right )\mathclose {}} \quad {\mathopen {}\left ({w},{S}\right )\mathclose {}} }{\displaystyle \quad {\mathopen {}\left ({vw},{\texttt {Concat}{\mathopen {}\left (R,S\right )\mathclose {}}}\right )\mathclose {}} \quad } \\[10pt] \displaystyle \frac {\displaystyle }{\displaystyle \quad {\mathopen {}\left ({\epsilon },{\texttt {Star}{\mathopen {}\left (R\right )\mathclose {}}}\right )\mathclose {}} \quad } \qquad \displaystyle \frac {\displaystyle {\mathopen {}\left ({u},{R}\right )\mathclose {}} \quad {\mathopen {}\left ({v},{\texttt {Star}{\mathopen {}\left (R\right )\mathclose {}}}\right )\mathclose {}} }{\displaystyle \quad {\mathopen {}\left ({uv},{\texttt {Star}{\mathopen {}\left (R\right )\mathclose {}}}\right )\mathclose {}} \quad } \end {gathered}We will write \mathcal {L}{\mathopen {}\left (R\right )\mathclose {}} for the formal language matching or “recognised” by the regular expression R:
\mathcal {L}{\mathopen {}\left (R\right )\mathclose {}} = {\mathopen {}\left \{ u:\Sigma ^\star \mid u\mathrel {\textsf {matches}}R \right \}\mathclose {}}We can also obtain a version of the matching relation between strings over \Sigma and concrete regular expressions by relational composition with the (transpose of) the parsing relation. Alternatively, we can simply write the concrete syntax as a mere notation (to be eliminated by the reader immediately) for the abstract syntax. In fact, that is what we will do in the future.
Example Examples of regular expression matching [017E]
- February 11, 2025
- Jon Sterling
Consider regular expression matching over the alphabet \Sigma ={\mathopen {}\left \{a,b\right \}\mathclose {}}.
- a\vert b is matched by each symbol in \Sigma .
- (a\vert b)* is matched every string over \Sigma .
- b(a\vert b)* is matched by any string over \Sigma that starts with a b.
- ((a\vert b)(a\vert b))* is matched by any string of even length over \Sigma .
- (\underline {\epsilon }\vert a)(\underline {\epsilon }\vert b)\vert bb is matched just by the strings \epsilon , a, b, ab, and bb.
- \varnothing b\vert a is matched only by the symbol a.
Discussion Questions about regular expression matching [017F]
- February 11, 2025
- Jon Sterling
Here are some questions for discussion.
- Is there an algorithm which, given a string u and a regular expression r, computes whether or not u matches r?
- In formulating the definition of regular expressions, have we missed out on some practically useful notions of pattern?
- Is there an algorithm that will compute whether two regular expressions generate the same language?
- Is every formal language of generated by a regular expression?
We will come back to these questions in subsequent lectures. In the meantime, we can say:
- Is there an algorithm which, given a string u and a regular expression r, computes whether or not u matches r? Yes. One very simple way is to simply use the rules of Definition [017D] as a search algorithm, but this is not very efficient; better ways to decide regular expression matching can be achieved using finite automata, which we will study in the next lecture.
- In formulating the definition of regular expressions, have we missed out on some practically useful notions of pattern? Yes! For example, it can be useful to specify a range of symbols (including all symbols!) over an alphabet like most practical regular expression libraries allow. A more sophisticated example is complementation, where a string matches \mathord {\sim }(r) if and only if it does not match r. We will ultimately see that the complement of any regular expression already exists—but it can be useful to include it as a built-in feature.
- Is there an algorithm that will compute whether two regular expressions generate the same language? Yes, but we will need the theory of finite automata to see that this is the case.
- Is every formal language of generated by a regular expression? No: we will ultimately learn a technique, called the pumping lemma to find counter-examples.