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 }