With this code you can convert regular expression given in the Python format to a (close) equivalent formal definition.
For a quick guide on Python regex, see the official documentation.
This is the formal definition we convert Python regex to:
a, b, c, ...: members of the alphabet used in the expressionabora&b: the concatenation ofaandb(i.e.,amust be directly followed byb)a|b: the choice ofaorb(i.e., eitheraorbis accepted); other literature also uses+a*: the Kleene Star operator (i.e.,acan be repeated 0 or more times)
Hint: use
\efor an empty string in inputs.
Run the script with
python main.py <destination> -e <expression> where destination must be one of REGEX, SYA, NFA or DFA.
REGEXsimply converts the given expression into a formal defintion regular expressionSYAconverts the given expression into a post-fix notation using the shunting-yard algorithmNFAcreates a nondeterministic finite automatonDFAcreates a deterministic finite automaton
Other optional arguments include -s for strict mode. Here, an error is thrown instead of making any assumptions when converting an expression (e.g., for lookahead assertion).
Also, -d enables debug mode, which prints conversion steps (i.e., all previous steps in the list above).
For a complete explanation of command-line arguments see
python main.py -hpython main.py REGEX -e '(ab|c)a*'(a&b|c)&a*
python main.py SYA -e '(ab|c)a*'ab&c|a*&
python main.py NFA -e '(ab|c)a*'States
Q: {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
Initial State(s)
q0: 1
Final State(s)
F: [10]
Transition Function
δ:
1 -> 2 with '##e##'
1 -> 5 with '##e##'
4 -> 7 with '##e##'
6 -> 7 with '##e##'
2 -> 3 with 'a'
3 -> 4 with 'b'
5 -> 6 with 'c'
7 -> 8 with '##e##'
7 -> 10 with '##e##'
9 -> 10 with '##e##'
9 -> 8 with '##e##'
8 -> 9 with 'a'
python main.py DFA -e '(ab|c)a*'States
Q: {1, 2, 3, 4, 5}
Initial State(s)
q0: 1
Final State(s)
F: [2, 4, 5]
Transition Function
δ:
1 -> 2 with 'c'
1 -> 3 with 'a'
3 -> 4 with 'b'
4 -> 5 with 'a'
5 -> 5 with 'a'
2 -> 5 with 'a'
python main.py SYA -e '(ab|c)a*' -dEscaped string: (ab|c)a*
Operator-filled string: (a&b|c)&a*
Shunting-yard string: ab&c|a*&
ab&c|a*&
MIT License
Copyright (c) 2022 Tobias Kalmbach
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
kalmbach.dev · GitHub @Tobi2K · Email tobias@kalmbach.dev