Class NFA

A nondeterministic finite automaton.

This class implements NFAs with the following properties:

  • There is exactly one initial state.

  • There may be any number of final states.

    This is implemented using a Set of states.

  • No epsilon transitions.

  • A transitions always consumes a character.

    (All character sets are guaranteed to be non-empty.)

  • Transitions are unordered.

    As a consequence, /aa|bb/ and /bb|aa/ have the same state machine in this NFA implementation.

    (The underlying data structure may be a JavaScript Map but the key order is ignored.)

  • Between any two states, there can at most be one transition.

    This means that all transitions between two nodes will be merged into one. This is implemented as a simple union. As a consequence, /a|a/ and /a/ have the same state machine in this NFA implementation.

Normal form

The normal form of this NFA implementation has the following restriction:

  • The initial state must not have incoming transitions.

Non-normalized NFAs will either be tolerated or normalized by operations.

Hierarchy

  • NFA

Implements

Properties

finals: Set<NFA.Node>

The set of final states of the NFA.

This set may be empty or contain nodes not reachable from the initial state.

initial: NFA.Node

The initial state of the NFA.

maxCharacter: Char

The maximum character that is part of the alphabet of the words that this FA can accept.

Accessors

  • get isEmpty(): boolean
  • Returns whether this FA accepts the empty language meaning that it doesn't accept any words.

    Returns boolean

  • get isFinite(): boolean
  • Returns whether the formal language accepted by this FA contains finitely many words.

    Note: Finite does not mean that all words can be iterated in practice. E.g. the set of all Unicode words with 10 or less characters contains 2.6e54 many words and can be accepted by a DFA with only 11 states.

    Returns boolean

  • get isNormalized(): boolean
  • Whether this NFA is in its normal form.

    Returns boolean

    See

    NFA

Methods

  • Modifies this NFA to accept the concatenation of this NFA and the given FA.

    This is implemented by simply moving the nodes from the given NFA into this NFA. The given NFA will be empty after this operation as nodes are moved, not shared.

    Parameters

    Returns void

  • Returns the number of nodes reachable from the initial state including the initial state.

    This returns the number of nodes returned by nodes.

    Returns number

  • Yields all nodes reachable from the initial state including the initial state.

    This may include trap states, but it will not include unreachable final states.

    The order in which nodes will be returned is implementation defined and may change after any operation that modifies the NFA.

    Modifying the NFA while iterating will result in implementation-defined behavior. The implementation may stop the iteration or yield an nodes.

    This operation runs in O(E + V) where E is the number of nodes reachable from the initial state and V is the number of transitions.

    Returns Iterable<NFA.Node>

  • Brings this NFA is in its normal form.

    This operation will create at most 1 node with the given factory.

    Parameters

    Returns void

    See

    NFA

  • Modifies this NFA such that all prefixes of all accepted words are also accepted.

    If the language of this NFA is empty, then it will remain empty.

    Unreachable states will be removed by this operation.

    Returns void

  • Modifies this NFA to accept the concatenation of the given NFA and this FA.

    This is implemented by simply moving the nodes from the given NFA into this NFA. The given NFA will be empty after this operation as nodes are moved, not shared.

    Parameters

    Returns void

  • Modifies this NFA to accept at least min and at most max concatenations of itself.

    Both min and max both have to be non-negative integers with min <= max. max is also allowed to be Infinity.

    Parameters

    Returns void

  • Removes all states that are unreachable.

    Only the following states will remain after this operation:

    1. The initial state.
    2. All states that are reachable from the initial state and can reach one of the final states.

    Returns void

  • Modifies this NFA such that it accepts the reverse of all words it currently accepts.

    If the language of this NFA is empty, then it will remain empty.

    Unreachable states will be removed by this operation.

    Returns void

  • Modifies this NFA such that all suffixes of all accepted words are also accepted.

    If the language of this NFA is empty, then it will remain empty.

    Unreachable states will be removed by this operation.

    Returns void

  • Returns whether this FA accepts the given word.

    Parameters

    Returns boolean

  • Returns the string representation of this FA in the DOT format.

    The output of this function can passed to any graph visualization program. This can be a local installation or an online editor.

    By default, toUnicodeString is used to represent CharSets. It's possible to provide a custom stringify function using the charSetToString parameter.

    Parameters

    • Optional charSetToString: ((charSet: CharSet) => string)
        • (charSet: CharSet): string
        • Parameters

          Returns string

    Returns string

  • Returns the string representation of this FA in the Mermaid format.

    By default, toUnicodeString is used to represent CharSets. It's possible to provide a custom stringify function using the charSetToString parameter.

    Parameters

    • Optional charSetToString: ((charSet: CharSet) => string)
        • (charSet: CharSet): string
        • Parameters

          Returns string

    Returns string

  • Returns a string representation of this FA.

    Returns string

  • Modifies this NFA to accept all words from this NFA and the given NFA.

    This is implemented by simply moving the nodes from the given NFA into this NFA. The given NFA will be empty after this operation as nodes are moved, not shared.

    Parameters

    Returns void

  • Removes the empty word from the accepted languages of this NFA.

    Returns void

  • Returns an iterable that will yield all word sets accepted by this FA. Word sets are yielded by ascending length.

    If this FA accepts infinitely many words, the iterable will never end. If this FA is finite, the iterable will end after at most 2^O(n) word sets (n = number of states).

    If you analyse the words of an FA, consider using this method instead of words. If this method yields k word sets, then words will yield up to O(k * m ^ l) words (m = number of possible characters, l = the maximum length of any of the k word sets).

    Returns Iterable<WordSet>

  • Returns an iterable that will yield all words accepted by this FA. Words are yielded by ascending length.

    If this FA accepts infinitely many words, the iterable will never end.

    Returns Iterable<Word>

  • Creates a new NFA which matches no words. The language of the returned NFA is empty.

    This operation will create exactly 1 node with the given factory.

    Parameters

    Returns NFA