In [1]:
from tock import *
Regular expressions in Tock use the same three operators and parentheses as Unix regular expressions; however, because a symbol can have more than one character, consecutive symbols must be separated by a space. Also, for the empty string, you must write &
. And even though the empty set is considered a regular expression, there's no way to write it in Tock.
To create a regular expression in Tock (this one is from Example 1.56):
In [2]:
m = from_regexp('(a b|a)*')
The regular expression is converted into a finite automaton, which you can view, as usual, as either a graph or a table.
In [3]:
to_graph(m)
In [4]:
to_table(m)
Out[4]:
The states are numbered according to the position in the regular expression they came from (so that listing them in alphabetical order is natural). The letter suffixes are explained below.
We can also pass the display_steps=True
option to show the automata created for all the subexpressions.
In [5]:
m = from_regexp('(a b|a)*', display_steps=True)
The to_regexp
function converts in the opposite direction:
In [6]:
e = to_regexp(m)
e
Out[6]:
Again, the display_steps
option causes all the intermediate steps of the conversion to be displayed.
In [7]:
e = to_regexp(m, display_steps=True)