Converts NFAs (and DFAs) to a regular expressions using the state removal method

nfa2regex: convert NFAs (and DFAs) to regular expressions

An implementation of the state removal technique for converting an NFA to a regular expression.

No optimization is done, so results may be surprising.

Examples

A Simple NFA

A simple example NFA:

nfa := nfa2regex.NewNFA()
nfa.AddEdge("1", "1", "a")
nfa.AddEdge("1", "2", "b")
nfa.AddEdge("2", "2", "c")
nfa.AddEdge("2", "3", "d")
nfa.AddEdge("3", "3", "e")
nfa.AddEdge("3", "1", "x")
nfa.Nodes["1"].IsInitial = true
nfa.Nodes["3"].IsTerminal = true

Which produces the state diagram:

And the regular expression:

a*bc*d(e|xa*bc*d)*

Binary Multiples of 3

An NFA which matches multiples of 3:

func MakeNFAMultiplesOfX(x int) *NFA {
    str := func(i int) string { return strconv.Itoa((i)) }
    nfa := NewNFA()
    for i := 0; i < x; i += 1 {
        nfa.AddEdge(str(i), str((i*2)%x), "0")
        nfa.AddEdge(str(i), str((i*2+1)%x), "1")
    }
    nfa.AddEdge("start", "0", "0")
    nfa.AddEdge("start", "1", "1")
    nfa.Nodes["start"].IsInitial = true
    nfa.Nodes["0"].IsTerminal = true
    return nfa
}

Which produces the state diagram:

And the regular expression:

(0(0|11|10(1|00)*01)*|11(0|11|10(1|00)*01)*|10(1|00)*01(0|11|10(1|00)*01)*)

State Removal Steps

Given an NFA with multiple initial and terminal nodes:

nfa := NewNFA()
nfa.AddEdge("1", "2", "a")
nfa.AddEdge("2", "3", "b")
nfa.AddEdge("2", "2", "l")
nfa.AddEdge("4", "2", "x")
nfa.AddEdge("2", "5", "y")
nfa.Nodes["1"].IsInitial = true
nfa.Nodes["4"].IsInitial = true
nfa.Nodes["3"].IsTerminal = true
nfa.Nodes["5"].IsTerminal = true

These are the steps used to convert it to a regular expression:

  1. Initial NFA:

  1. Creation of initial and terminal states:

  1. Removal of node 4:

  1. Removal of node 5:

  1. Removal of node 1:

  1. Removal of node 2:

  1. And finally, the removal of node 3:

Which yields the regular expression:

(xl*b|xl*y|al*b|al*y)
Similar Resources

Plinko - a Fluent State Machine for Go

 Plinko - a Fluent State Machine for Go

Plinko - a Fluent State Machine for Go Build Status Create state machines and lightweight state machine-based workflows directly in golang code The pr

Jan 3, 2023

Package fsm allows you to add finite-state machines to your Go code.

fsm Package fsm allows you to add finite-state machines to your Go code. States and Events are defined as int consts: const ( StateFoo fsm.State =

Dec 9, 2022

Finite State Machine for Go

Finite State Machine for Go

FSM for Go Finite State Machine for Go It is heavily inspired from looplab/fsm library but with more abstractions and optimizations License FSM is lic

Nov 30, 2021

An ease to use finit state machine golang implementation.Turn any struct to a fsm with graphviz visualization supported.

go-fsm An ease to use finit state machine golang implementation.Turn any struct to a fsm with graphviz visualization supported. usage import github.co

Dec 26, 2021

Finite-state machine with processors

FSM Finite-state machine with processors. Builder Register state processors type state1Processor struct { // clients } func NewState1Processor(..

Jan 7, 2022

Get notifications about unexpected system state from your local Gesundheitsdienst.

Get notifications about unexpected system state from your local Gesundheitsdienst.

Nov 1, 2022

Purpose: dump slack messages, users and files using browser token and cookie.

Slack Dumper Purpose: dump slack messages, users and files using browser token and cookie. Typical usecase scenarios: You want to archive your private

Jan 2, 2023

A distributed unique ID generator of using Sonyflake and encoded by Base58

Indigo About A distributed unique ID generator of using Sonyflake and encoded by Base58. ID max length is 11 characters by unsigned int64 max value. A

Nov 24, 2022

An example event-driven application using Atmo and NATS

Atmo + NATS Example Project This repo is an example of using Atmo with NATS as a streaming messaging layer. In this example, Atmo connects to NATS and

Oct 27, 2021
Comments
  • Would it be possible to add a canonical regex step or a simplify/unique step?

    Would it be possible to add a canonical regex step or a simplify/unique step?

    For example I am getting the following results:

    NFA: 
    ^gdt-[0-9A-Za-z]([0-9A-Za-z])*-s$(s$)*
    ^gdt-[0-9A-Za-z]([0-9A-Za-z])*-c$(s$)*
    ^gdt-[0-9A-Za-z]([0-9A-Za-z])*-s$(s$)*
    ^gdt-[0-9A-Za-z]([0-9A-Za-z])*-c$(s$)*
    

    NOTE: I replaced the | w/ newlines

    With the following graph

    Simplified (aka what I need):

    ^gdt-[0-9A-Za-z]+-c$
    ^gdt-[0-9A-Za-z]+-s$
    

    or unified

    '^gdt-[0-9A-Za-z]+-(c|s)$'
    

    Is that possible?

A simulated-annealing approach to solving a max-flow removal problem
A simulated-annealing approach to solving a max-flow removal problem

RESISTANCE IS FUTILE How to run: Install the latest version of golang to your computer (1.16?) Run a postgres instance on your computer attatched to p

Aug 26, 2022
Transform PromQL Expressions on the fly

promql-transform Transforms PromQL expressions on the fly Usage Given the expression job:request_latency_seconds:mean5m{job=\"myjob\"} > 0.5 Running

Jun 21, 2022
converts f to c and c to f

temperature-converter converts f to c and c to f Example package main import ( "github.com/Omvik/temperature-converter/calcTemp" "fmt" "log" )

Nov 2, 2021
Provide Go Statistics Handler, Struct, Measure Method

Go Statistics Handler About The gosh is an abbreviation for Go Statistics Handler. This Repository is provided following functions. Go runtime statist

Jan 8, 2023
An effective time-series data compression/decompression method based on Facebook's Gorilla.

Gorilla This package provides the effective time-series data compression method based on Facebook's Gorilla.. In a nutshell, it uses delta-of-delta ti

Sep 26, 2022
Go implementation Welford’s method for one-pass variance computation

Variance and standard deviation caluculation using variance's algorithm Table of Contents Introduction Installation Usage Contributing License Introdu

Jun 5, 2022
This project is an implementation of Fermat's factorization method in which multiples of prime numbers are factored into their constituent primes

This project is an implementation of Fermat's factorization method in which multiples of prime numbers are factored into their constituent primes. It is a vanity attempt to break RSA Encryption which relies on prime multiples for encryption.

Jun 3, 2022
A method dispatcher written in go powered by reflection.

go-dispatcher A single-file dispatcher written in Golang. Description Inspired by the JSON-RPC module of polygon-edge and geth, this package provides

Feb 21, 2022
Converts grouped transactions in a ZKB transaction CSV (incl. details) to single transactions

ZKB Converter Converts grouped transactions in a ZKB transaction CSV (incl. deta

Dec 26, 2021
Go library for creating state machines
Go library for creating state machines

Stateless Create state machines and lightweight state machine-based workflows directly in Go code: phoneCall := stateless.NewStateMachine(stateOffHook

Jan 6, 2023