Example. Precedence parsing for regular expressions [017B]
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 }