← Back to blog

How regular expressions are parsed

The first time I heard about regular expressions, regular languages and automatas was in a Theory of Computation course. A very interesting course that served as an introduction to formal languages and compilers. Very boring for some and very fun for lil' nerdies (like me). Here you will find a small project I developed to understand more how regular expressions are processd using a small parser and Non-Deterministic Finite Automatas.

Regular Expressions

Regular expressions are a sequence of characters that represent a text pattern. Let us take as an example the expression that represents strings that start with 'a' and end with 'd', and have zero or more 'b' or 'c' characters between them.

Pattern a(b|c)*d
Matches
  • ✓ad
  • ✓abcbcd
  • ✓accccd
No match
  • ✗abc
  • ✗xyz
  • ✗aabbd

The notation can be a bit confusing the first time, but the idea behind it is simple. As one might imagine, these expressions are very useful to process text in different ways. But how does one do that efficiently? By using automatas.

Finite Automata Theory

Automatas are machines that process an input string one character at a time while moving through a set of states. Very dense definition indeed. Since an image is worth more than a thousand words, below I present a gif representing an automata. I said that we need automatas to process regular expressions. And one might reasonably wonder: "What does a string have anything to do with a graph?". Well my dear readers, some years ago, some researchers in some journal published a paper proving that every regular expression has an equivalent automata that represents it [1] [2]. Magical really. But before going into more details on that, the automata here represents the expression a(b|c)*d.

DFA processing abcbcd DFA processing abcbcd

As the little animation shows, there is an initial state (the one with no incoming edges) and an accepting state (marked in blue). The automata consumes the string, character by character until it is completely parsed. Starting from the initial state, it moves around consuming the string. If by the end of the string the automata is in an accepting state, we say that the input string is recognised by the machine. Very simple and fun stuff if you ask me.

The automata shown in the animation is what we call a Deterministic Finite Atuomata (DFA). It is deterministic since the same input always yields the same sequence of state traversals and output. As one can imagine, there is the non deterministic version of this. Guess what it is called. That's right: Non-Deterministic Finite Automatas (NFA). These automatas have a special transition called ɛ-transition. An ɛ-transition represents a non-deterministic choice. It means that the edges that have this ɛ character, may or may not be crossed, independently of the parsed character. It is as if some kind of external entity could choose which ɛ edges are crossed and which not. That is the beauty of non-determinisim but that's taking us away from the point. We'll see examples of NFA later.

But now, how does one compute this automata? That is the tricky part. We need a few things; first, we need to parse the regular expression and properly recognise where the operators lie. Secondly, once we have parsed the expression, we must build the automata that is equivalent to this expression. Looks hard, but we'll go over the whole process in detail.

Regex syntax parsing

The first thing to do is to analyze the regular expression. This comes as a string in the input, but there are special characters like *, | or () that must be treated specially. Operator precedence must be preserved, since (a|b)* is not the same as a|b*, which is equivalent to a|(b*). To do so, the very wise bible of compilers [2], shows us how to use a Recursive-Descent parser to get the expression ordered by preference. Before getting into the details let us check what this would look like for the previous example.

DFA processing abcbcd DFA processing abcbcd

The symbol "·" represents de concatenation between strings, hence, ab is equivalent to a·b. If anyone is interested, this tree is called an Abstract Syntax Tree. To simplify, it does just what we wanted: represent in a simple form (a tree) the operator preference in the regular expression. Each level in the tree corresponds to a different operator coverage. Such a nice and simple thing to compute. The idea behind it is a bit more complex. It involves LL(1)-grammars and other aspects of formal language theory.

Imagine we could have a way of describing how the regular expressions can be formed. We could say how the operators *, | and ? can be used and what is the syntax that must be used. Just like the way in which grammar explains how sentences can be formed given a set of words. This is exactly the same behavior for formal grammars. Given a set of words in an alphabet, it tells us how we can form "sentences". Notice that the term sentence here applies a bit differently than with spoken languages. If you want more information on this, the Sipser book [1] covers this very well. For the curios people, this is the LL(1) grammar that was used to parse the expressions:

\[\begin{align} expr &\rightarrow term \; (\text{'|' }term)^* \\ term &\rightarrow factor^* \\ factor &\rightarrow atom \; postfix? \\ postfix &\rightarrow \text{'*' | '+' | '?'} \\ atom &\rightarrow CHAR \text{ | '(' } expr \text{ ')'} \end{align}\]

Having the grammar, the recursive-descent parser is very easy to implement. It suffices only to define one method for each rule and properly manage which characters and other methods must be called.

Now we (mostly) know how to parse a regular expression. Let us go now to the real deal.

Thompston's construction

Thompston's algorithm is a method that describes how to build Non-Finite Deterministic Automatas from given regular expressions. The idea is to recursively build the NFA by traversing the AST level by level, starting from the leaves. Given each type of node in the AST, Thompston's method defines how each must be transformed into a NFA. You can see below each transformation.

Thompson's construction rules
ConstructionRegexNFA fragmentIntuition
Empty string ε Move from start to accept without reading any input.
Single symbol a a Read exactly one symbol, then accept.
Concatenation RS R S Finish R, then continue straight into S.
Union R|S R S Branch without consuming input, then rejoin.
Kleene star R* R Loop back to R any number of times, or skip it.
Question R? R Branch if R is not present.
Plus R+ R Traverse R and then repeat or go to final state.

Dashed orange edges are ε-transitions (no input consumed). Solid edges read a symbol. Orange states are the ones the rule adds. Double ring = accept state.

Maybe the idea can be seen better now. The algorithm recursively builds the NFA by starting from the characters and going up to higher priority operators. This ensures a certain structure that makes the graph have some very nice properties.

Let us see how this would be converted with the example before a(b|c)*d.

DFA processing abcbcd DFA processing abcbcd

If one looks at this automata and at the AST, it is easy to see how it was built.

So now, the last question that must be answered. We have parsed the expression, and we have built the Automata. But how do we check if a string matches the expression? Now that is the last step.

Moving through the Automata

Just as we showed with the DFA before, we need to traverse the automata to see if the input character is accepted or not. However, the generated automata with Thompston's method is an NFA, therefore it has ɛ-transitions. When we encounter one of these, we cannot be sure if we have to take it or not. But, thankfully, Thompston's method ensures the following property properties:

Each node has at most two outgoing-edges, one of the following:

  • One edge labeled with a character
  • At most two edges of ɛ-transition

For reasons we will not trouble ourselves with, this helps us traverse the graph by "grouping" all the possible lambda transitions each character we process. Look at the following animation.

DFA processing abcbcd DFA processing abcbcd

At each processed character of the input string, we move through the automata. For character labeled transitions this is straightforward. For ɛ-transitions, the algorithm looks for all the states we can reach with only ɛ-transitions. These can be "joined" into one group, since they do not give any information about the input character. If we keep going doing this at each input character, we will know if the string is accepted when the possible new states reached include the accepting state.

Conclusion

Just to finish off this marvelous post, I want to share a bit of my thoughts. This is a very dense thing to understand from scracth. It took me a while to grasp most of the things and I already had some knowledge of this. This post is just aimed to showcase how the process works to any type of reader. For more information one can look at the references, which explain this far better than I ever could.

Even though this is a nice review on formal language theory, syntax analysis and regular expressions, most of the regular expression engines are much more complex than this. Take the re python library. This allows for many more operators and things such as backtracking. For those cases, Finite Automatas loose power since they do not have the capabilities to deal with certain operations. However, the base ideas always remain and it is nice to know them.

References

  1. Sipser, M. (2013). Introduction to the theory of computation (Third edition). Cengage Learning.
  2. A. V. Aho, M. S. Lam, R. Sethi, and J. D. Ullman, Compilers: Principles, Techniques, and Tools, 2nd ed. Boston, MA: Addison-Wesley, 2006.
  3. R. Cox, "Regular Expression Matching Can Be Simple and Fast," Jan. 2007. [Online]. Available: https://swtch.com/~rsc/regexp/regexp1.html