grammar::fa(n)
π grammar::fa(n) β Finite automaton operations and usage π
okay so bestie, welcome to grammar::fa(n). That's the manual page for the grammar::fa package, and it's giving finite automaton operations and usage, and honestly? Iconic. Grab a snack, this one's long (we're doing it in parts, so don't panic).
πΈ NAME πΈ
grammar::fa β Create and manipulate finite automatons
π¦ SYNOPSIS π¦
package require Tcl 8.5 9
package require snit 1.3
package require struct::list
package require struct::set
package require grammar::fa::op ?0.3?
package require grammar::fa ?0.6?
::grammar::fa faName ?=|:=|<--|as|deserialize src|fromRegex re ?over??
faName option ?arg arg ...?
faName destroy
faName clear
faName = srcFA
faName --> dstFA
faName serialize
faName deserialize serialization
faName states
faName state add s1 ?s2 ...?
faName state delete s1 ?s2 ...?
faName state exists s
faName state rename s snew
faName startstates
faName start add s1 ?s2 ...?
faName start remove s1 ?s2 ...?
faName start? s
faName start?set stateset
faName finalstates
faName final add s1 ?s2 ...?
faName final remove s1 ?s2 ...?
faName final? s
faName final?set stateset
faName symbols
faName symbols@ s ?d?
faName symbols@set stateset
faName symbol add sym1 ?sym2 ...?
faName symbol delete sym1 ?sym2 ...?
faName symbol rename sym newsym
faName symbol exists sym
faName next s sym ?--> next?
faName !next s sym ?--> next?
faName nextset stateset sym
faName is deterministic
faName is complete
faName is useful
faName is epsilon-free
faName reachable_states
faName unreachable_states
faName reachable s
faName useful_states
faName unuseful_states
faName useful s
faName epsilon_closure s
faName reverse
faName complete
faName remove_eps
faName trim ?what?
faName determinize ?mapvar?
faName minimize ?mapvar?
faName complement
faName kleene
faName optional
faName union fa ?mapvar?
faName intersect fa ?mapvar?
faName difference fa ?mapvar?
faName concatenate fa ?mapvar?
faName fromRegex regex ?over?
π DESCRIPTION π
So this package is a container class for finite automatons (short: FA). It lets you build up the definition of an automaton bit by bit β incremental definition, manipulation, and querying of that definition. Obsessed.
Now, real talk: while the package gives you the complex operations on the automaton (those come via the package grammar::fa::op), it does not have the ability to actually execute a definition for a stream of symbols. For that, bestie, you want the packages grammar::fa::dacceptor and grammar::fa::dexec. π
Another related one is grammar::fa::compiler. It turns a FA into an executor class which has the definition of the FA hardwired into it. The output of this package is configurable to suit a large number of different implementation languages and paradigms. Versatile queen. β¨
For more info on what a finite automaton actually is, go look at the section FINITE AUTOMATONS (it's coming, keep reading).
π API π
The package exports the API described here. Periodt.
π ::grammar::fa faName ?=|:=|<--|as|deserialize src|fromRegex re ?over??
Creates a new finite automaton with an associated global Tcl command whose name is faName. That command is your little remote control β you use it to invoke all kinds of operations on the automaton. It has this general form:
faName option ?arg arg ...?
option and the args decide exactly what the command does. (More on that in section FA METHODS, keep scrolling π
.)
The new automaton will be empty if no src is specified. Otherwise it'll contain a copy of the definition contained in the src. And here's the catch, bestie: src has to be a FA object reference for all the operators except deserialize and fromRegex. The deserialize operator needs src to be the serialization of a FA instead, and fromRegex takes a regular expression in the form of a syntax tree. See ::grammar::fa::op::fromRegex for more detail on that one.
β¨ FA METHODS β¨
All automatons provide the following methods for their manipulation:
π faName destroy
Destroys the automaton β including its storage space and its associated command. Gone. Poof. π
π faName clear
Clears out the definition of the automaton contained in faName, but does not destroy the object. She's still here, just empty.
π faName = srcFA
Assigns the contents of the automaton contained in srcFA to faName, overwriting any existing definition. This is the assignment operator for automatons. It copies the automaton contained in the FA object srcFA over the automaton definition in faName. The old contents of faName are deleted by this operation. No cap.
This operation is in effect equivalent to
faName deserialize [srcFA serialize]
π faName --> dstFA
This is the reverse assignment operator for automatons. It copies the automation contained in the object faName over the automaton definition in the object dstFA. The old contents of dstFA are deleted by this operation.
This operation is in effect equivalent to
dstFA deserialize [faName serialize]
π faName serialize
This method serializes the automaton stored in faName. In other words, it returns a tcl value that completely describes that automaton. This lets you do stuff like transfer automatons over arbitrary channels, do persistence, etc. It's also the basis for both the copy constructor and the assignment operator. She's the blueprint AND the builder. π₯
The result of this method has to be semantically identical over all implementations of the grammar::fa interface. That's what enables us to copy automatons between different implementations of the same interface. Slay.
The result is a list of three elements with the following structure:
- The constant string
grammar::fa. - A list containing the names of all known input symbols. The order of elements in this list is not relevant.
- The last item in the list is a dictionary, however the order of the keys is important as well. The keys are the states of the serialized FA, and their order is the order in which to create the states when deserializing. This is relevant to preserve the order relationship between states.
The value of each dictionary entry is a list of three elements describing the state in more detail.
- A boolean flag. If its value is true then the state is a start state, otherwise it is not.
- A boolean flag. If its value is true then the state is a final state, otherwise it is not.
- The last element is a dictionary describing the transitions for the state. The keys are symbols (or the empty string), and the values are sets of successor states.
Assuming the following FA (which describes the life of a truck driver in a very simple way :)
Drive -- yellow --> Brake -- red --> (Stop) -- red/yellow --> Attention -- green --> Drive
(...) is the start state.
a possible serialization is
grammar::fa \
{yellow red green red/yellow} \
{Drive {0 0 {yellow Brake}} \
Brake {0 0 {red Stop}} \
Stop {1 0 {red/yellow Attention}} \
Attention {0 0 {green Drive}}}
A possible one, because I did not care about creation order here.
π faName deserialize serialization
This is the complement to serialize. It replaces the automaton definition in faName with the automaton described by the serialization value. The old contents of faName are deleted by this operation.
π faName states
Returns the set of all states known to faName.
π faName state add s1 ?s2 ...?
Adds the states s1, s2, et cetera to the FA definition in faName. The operation will fail if any of the new states is already declared. π
π faName state delete s1 ?s2 ...?
Deletes the state s1, s2, et cetera, and all associated information, from the FA definition in faName. And by "associated information" we mean the info about in- or outbound transitions gets deleted as well. If the deleted state was a start or final state then that information is invalidated too. The operation will fail if the state s is not known to the FA.
π faName state exists s
A predicate. It tests whether the state s is known to the FA in faName. The result is a boolean value β true if the state s is known, false otherwise.
π faName state rename s snew
Renames the state s to snew. Fails if s is not a known state. Also fails if snew is already known as a state.
π faName startstates
Returns the set of states which are marked as start states, also known as initial states. See FINITE AUTOMATONS for explanations of what this means. π
π faName start add s1 ?s2 ...?
Mark the states s1, s2, et cetera in the FA faName as start (aka initial).
π faName start remove s1 ?s2 ...?
Mark the states s1, s2, et cetera in the FA faName as not start (aka not accepting).
π faName start? s
A predicate. It tests if the state s in the FA faName is start or not. The result is a boolean value β true if the state s is start, false otherwise.
π faName start?set stateset
A predicate. It tests if the set of states stateset contains at least one start state. The operation will fail if the set contains an element which is not a known state. The result is a boolean value β true if a start state is present in stateset, false otherwise.
π faName finalstates
Returns the set of states which are marked as final states, also known as accepting states. See FINITE AUTOMATONS for explanations of what this means. π
π faName final add s1 ?s2 ...?
Mark the states s1, s2, et cetera in the FA faName as final (aka accepting).
π faName final remove s1 ?s2 ...?
Mark the states s1, s2, et cetera in the FA faName as not final (aka not accepting).
π faName final? s
A predicate. It tests if the state s in the FA faName is final or not. The result is a boolean value β true if the state s is final, false otherwise.
faName final?set statesetβ bestie this one is a predicate π It tests whether the set of statesstatesetcontains at least one final state. It will fail if the set contains an element that isn't a known state π. What you get back is a boolean value β set totrueif a final state is present instateset, andfalseotherwise. slay.faName symbolsβ hands you the set of all symbols known to the FAfaName. that's it, that's the whole vibe β¨faName symbols@ s ?d?β returns the set of all symbols for which the stateshas transitions. If the empty symbol is present in there, thenshas epsilon transitions π. If two states are specified, the result is the set of symbols which have transitions fromstot. Heads up: that set may be empty if there are no transitions between the two specified states πfaName symbols@set statesetβ returns the set of all symbols for which at least one state in the set of statesstatesethas transitions. In other words? It's the union of[faName symbols@ s]for all statessinstatesetπ If the empty symbol is present, then at least one state contained instatesethas epsilon transitions.faName symbol add sym1 ?sym2 ...?β adds the symbolssym1,sym2, et cetera to the FA definition infaName. The operation will fail if any of the symbols is already declared π. Also, the empty string is not allowed as a value for the symbols. no cap.faName symbol delete sym1 ?sym2 ...?β deletes the symbolssym1,sym2et cetera, and all associated information, from the FA definition infaName. The "associated information" part means all transitions using those symbols get deleted too π. The operation will fail if any of the symbols is not known to the FA.faName symbol rename sym newsymβ renames the symbolsymtonewsym. Fails ifsymisn't a known symbol. Also fails ifnewsymis already known as a symbol. glow-up denied π«ΆfaName symbol exists symβ another predicate πΈ It tests whether the symbolsymis known to the FA infaName. The result is a boolean value β set totrueif the symbolsymis known, andfalseotherwise.
faName next s sym ?--> next?β this one defines or queries transition information, iykyk π
If next is specified, then the method will add a transition from the state s to the successor state next labeled with the symbol sym to the FA contained in faName. The operation will fail if s or next are not known states, or if sym is not a known symbol. One exception to that last bit: sym is allowed to be the empty string π In that case the new transition is an epsilon transition, which will not consume input when traversed. The operation will also fail if the combination of (s, sym, and next) is already present in the FA.
If next was not specified, then the method will return the set of states which can be reached from s through a single transition labeled with symbol sym. cute.
faName !next s sym ?--> next?β remove one or more transitions from the FA infaNameπ§Ή
If next was specified, then the single transition from the state s to the state next labeled with the symbol sym is removed from the FA. Otherwise all transitions originating in state s and labeled with the symbol sym will be removed. bye bestie π
The operation will fail if s and/or next are not known as states. It will also fail if a non-empty sym is not known as a symbol. The empty string is acceptable, and allows the removal of epsilon transitions β¨
faName nextset stateset symβ returns the set of states which can be reached by a single transition originating in a state in the setstatesetand labeled with the symbolsym.
In other words, this is the union of [faName next s symbol] for all states s in stateset. periodt.
faName is deterministicβ a predicate π¦ It tests whether the FA infaNameis a deterministic FA or not. The result is a boolean value β set totrueif the FA is deterministic, andfalseotherwise.faName is completeβ a predicate π¦ It tests whether the FA infaNameis a complete FA or not. A FA is complete if it has at least one transition per state and symbol. This also means that a FA without symbols, or states is also complete. The result is a boolean value β set totrueif the FA is deterministic, andfalseotherwise.
Note: when a FA has epsilon-transitions, transitions over a symbol for a state S can be indirect, i.e. not attached directly to S, but to a state in the epsilon-closure of S. The symbols for such indirect transitions count when computing completeness. don't forget it π«Ά
faName is usefulβ a predicate π¦ It tests whether the FA infaNameis a useful FA or not. A FA is useful if all states are reachable and useful. The result is a boolean value β set totrueif the FA is deterministic, andfalseotherwise.faName is epsilon-freeβ a predicate π¦ It tests whether the FA infaNameis an epsilon-free FA or not. A FA is epsilon-free if it has no epsilon transitions. This definition means that all deterministic FAs are epsilon-free as well, and epsilon-freeness is a necessary pre-condition for deterministic'ness. The result is a boolean value β set totrueif the FA is deterministic, andfalseotherwise.faName reachable_statesβ returns the set of states which are reachable from a start state by one or more transitions.faName unreachable_statesβ returns the set of states which are not reachable from any start state by any number of transitions. This is
[faName states] - [faName reachable_states]
faName reachable sβ a predicate π¦ It tests whether the statesin the FAfaNamecan be reached from a start state by one or more transitions. The result is a boolean value β set totrueif the state can be reached, andfalseotherwise.faName useful_statesβ returns the set of states which are able to reach a final state by one or more transitions.faName unuseful_statesβ returns the set of states which are not able to reach a final state by any number of transitions. This is
[faName states] - [faName useful_states]
faName useful sβ a predicate π¦ It tests whether the statesin the FAfaNameis able to reach a final state by one or more transitions. The result is a boolean value β set totrueif the state is useful, andfalseotherwise.faName epsilon_closure sβ returns the set of states which are reachable from the statesin the FAfaNameby one or more epsilon transitions, i.e. transitions over the empty symbol, transitions which do not consume input π. This is called the epsilon closure ofs.
And then we got the glow-up squad of complex operations π The original page doesn't spell out each one here, it just lists them, so here they are in all their glory:
faName reversefaName completefaName remove_epsfaName trim ?what?faName determinize ?mapvar?faName minimize ?mapvar?faName complementfaName kleenefaName optionalfaName union fa ?mapvar?faName intersect fa ?mapvar?faName difference fa ?mapvar?faName concatenate fa ?mapvar?faName fromRegex regex ?over?
These methods provide more complex operations on the FA π Please see the same-named commands in the package grammar::fa::op for descriptions of what they do β that's where the lore lives, bestie π«Ά
β¨ EXAMPLES β¨
π₯ FINITE AUTOMATONS π₯
For the mathematically inclined π§ a FA is a 5-tuple (S,Sy,St,Fi,T) where
Sis a set of states,Sya set of input symbols,Stis a subset ofS, the set of start states, also known as initial states.Fiis a subset ofS, the set of final states, also known as accepting.Tis a function fromS x (Sy + epsilon)to{S}, the transition function. Hereepsilondenotes the empty input symbol and is distinct from all symbols inSy; and{S}is the set of subsets ofS. In other words,Tmaps a combination of State and Input (which can be empty) to a set of successor states β¨
In computer theory a FA is most often shown as a graph where the nodes represent the states, and the edges between the nodes encode the transition function: For all n in S' = T (s, sy) we have one edge between the nodes representing s and n resp., labeled with sy. The start and accepting states are encoded through distinct visual markers, i.e. they are attributes of the nodes π
FA's are used to process streams of symbols over Sy.
A specific FA is said to accept a finite stream sy_1 sy_2 ... sy_n if there is a path in the graph of the FA beginning at a state in St and ending at a state in Fi whose edges have the labels sy_1, sy_2, etc. to sy_n. The set of all strings accepted by the FA is the language of the FA. One important equivalence is that the set of languages which can be accepted by an FA is the set of regular languages π
Another important concept is that of deterministic FAs. A FA is said to be deterministic if for each string of input symbols there is exactly one path in the graph of the FA beginning at the start state and whose edges are labeled with the symbols in the string. While it might seem that non-deterministic FAs to have more power of recognition, this is not so π For each non-deterministic FA we can construct a deterministic FA which accepts the same language (--> Thompson's subset construction). no cap.
While one of the premier applications of FAs is in parsing, especially in the lexer stage (where symbols == characters), this is not the only possibility by far π¦
Quite a lot of processes can be modeled as a FA, albeit with a possibly large set of states. For these the notion of accepting states is often less or not relevant at all. What is needed instead is the ability to act to state changes in the FA, i.e. to generate some output in response to the input. This transforms a FA into a finite transducer, which has an additional set OSy of output symbols and also an additional output function O which maps from "S x (Sy + epsilon)" to "(Osy + epsilon)", i.e. a combination of state and input, possibly empty, to an output symbol, or nothing πΈ
For the graph representation this means that edges are additional labeled with the output symbol to write when this edge is traversed while matching input. Note that for an application "writing an output symbol" can also be "executing some code" π
Transducers are not handled by this package. They will get their own package in the future. watch this space π
π BUGS, IDEAS, FEEDBACK π
okay bestie, real talk for a sec π«Ά this document? and the whole package it describes? yeah, they will undoubtedly have bugs and other problems. like, not maybe, not hypothetically β undoubtedly. that's just how software is. no cap.
so if you find one, please report it in the category grammar_fa of the Tcllib Trackers over at [http://core.tcl.tk/tcllib/reportlist]. that's the spot. that's where the receipts go. β¨
and listen β this isn't just for "omg it's broken" moments. if you have ideas for enhancements for either the package and/or the documentation, please report those too. your big-brain glow-up ideas are welcome, fr fr. π
when you're proposing code changes, please provide unified diffs, i.e. the output of diff -u. that's the format, that's the vibe, periodt.
also important, and lowkey the part everyone skips: attachments are strongly preferred over inlined patches. do not paste a whole patch into the ticket body like it's a group chat message, bestie, i'm begging π
how do you attach? easy:
- go to the Edit form of the ticket β right after you create it, immediately, don't wander off
- then use the left-most button in the secondary navigation bar
that's it. that's the whole ritual. π§
β¨ KEYWORDS β¨
the tags, the searchable lore, the iykyk list π
automatonfinite automatongrammarparsingregular expressionregular grammarregular languagesstatetransducer
all of them. iconic behaviour, honestly. π
π CATEGORY π
Grammars and finite automata π¦
that's the shelf this one lives on. the main character section of the library. π
π COPYRIGHT π
Copyright (c) 2004-2009 Andreas Kupries <andreas_kupries@users.sourceforge.net> π
give the person their flowers, they earned them. πΈ
and that's a wrap on the whole page, bestie!! π you made it to the end, which is genuinely slay of you. this gorgeous thing came to you from tcllib, version 0.6, as grammar::fa(n) β so go forth, build your finite automatons, and remember: every detail stayed exactly where it was, only the vibe got upgraded. mwah. π
β¨π«Ά
π¦ where this page comes from
- fedora 43: tcllib 2.0-3.fc43 Β· TCL
- fedora 44: tcllib 2.0-4.fc44 Β· TCL
- nixos 26.05: tcllib 2.0
- nixpkgs unstable: tcllib 2.0
- Copyright (c) 2004-2009 Andreas Kupries <andreas_kupries@users\&.sourceforge\&.net>
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms