Welcome to FAdo’s documentation¶
FAdo: Tools for Language Models Manipulation
Authors: Rogério Reis & Nelma Moreira
The support of transducers and all its operations, as well of Set Specifications, is a joint work with Stavros Konstantinidis (St. Mary’s University, Halifax, NS, Canada) (http://cs.smu.ca/~stavros/).
Contributions by
|
|
Page of the project: http://fado.dcc.fc.up.pt.
Version: 2.0
Copyright: 1999-2022 Rogério Reis & Nelma Moreira {rogerio.reis,nelma.moreira}@fc.up.pt
Faculdade de Ciências da Universidade do Porto
Centro de Matemática da Universidade do Porto
Licence:
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
What is FAdo?¶
The FAdo system aims to provide an open source extensible high-performance software library for the symbolic manipulation of automata and other models of computation.
To allow high-level programming with complex data structures, easy prototyping of algorithms, and portability (to use in computer grid systems for example), are its main features. Our main motivation is the theoretical and experimental research, but we have also in mind the construction of a pedagogical tool for teaching automata theory and formal languages.
Regular Languages¶
It currently includes most standard operations for the manipulation of regular languages. Regular languages can be represented by regular expressions (regexp) or finite automata, among other formalisms. Finite automata may be deterministic (DFA), non-deterministic (NFA) or generalized (GFA). In FAdo these representations are implemented as Python classes.
Elementary regular languages operations as union, intersection, concatenation, complementation and reverse are implemented for each class. Also several combined operations are available for specific models.
Several conversions between these representations are implemented:
NFA -> DFA: subset construction
NFA -> RE: recursive method
GFA -> RE: state elimination, with possible choice of state orderings
RE -> NFA: Thompson method, Glushkov method, follow, Brzozowski, and partial derivatives.
For DFAs several minimization algorithms are available: Moore, Hopcroft, and some incremental algorithms. Brzozowski minimization is available for NFAs.
An algorithm for hyper-minimization of DFAs
Language equivalence of two DFAs can be determined by reducing their correspondent minimal DFA to a canonical form, or by the Hopcroft and Karp algorithm.
Enumeration of the first words of a language or all words of a given length (Cross Section)
Some support for the transition semigroups of DFAs
Finite Languages¶
Special methods for finite languages are available:
Construction of a ADFA (acyclic finite automata) from a set of words
Minimization of ADFAs
Several methods for ADFAs random generation
Methods for deterministic cover finite automata (DCFA)
Transducers¶
Several methods for transducers in standard form (SFT) are available:
Rational operations: union, inverse, reversal, composition, concatenation, star
Test if a transducer is functional
Input intersection and Output intersection operations
Codes¶
A language property is a set of languages. Given a property specified by a transducer, several language tests are possible.
Satisfaction i.e. if a language satisfies the property
Maximality i.e. the language satisfies the property and is maximal
Properties implemented by transducers include: input preserving, input altering, trajectories, and fixed properties
Computation of the edit distance of a regular language, using input altering transducers
Module: Finite Automata (fa)¶
Finite automata manipulation.
Deterministic and non-deterministic automata manipulation, conversion and evaluation. .. Authors: Rogério Reis & Nelma Moreira .. This is part of FAdo project http://fado.dcc.fc.up.pt.
Classes¶
- class FAdo.fa.FA[source]¶
Base class for Finite Automata.
Note
This is just an abstract class. Not to be used directly!!
- class FAdo.fa.NFA[source]¶
Class for Non-deterministic Finite Automata (epsilon-transitions allowed).

- Return type
- class FAdo.fa.NFAr[source]¶
Class for Non-deterministic Finite Automata with reverse delta function added by construction.

Note
Includes efficient methods for merging states.
- Return type
- class FAdo.fa.SSemiGroup[source]¶
Class support for the Syntactic SemiGroup.
- Variables
elements – list of tuples representing the transformations
words – a list of pairs (index of the prefix transformation, index of the suffix char)
gen – a list of the max index of each generation
Sigma – set of symbols
- class FAdo.fa.EnumL(aut, store=False)[source]¶
Class for enumerate FA languages
- Variables
New in version 0.9.8.
See also
Efficient enumeration of words in regular languages, M. Ackerman and J. Shallit, Theor. Comput. Sci. 410, 37, pp 3461-3470. 2009. http://dx.doi.org/10.1016/j.tcs.2009.03.018
Functions¶
- FAdo.fa.saveToString(aut, sep='&')[source]¶
Finite automata definition as a string using the input format.
New in version 0.9.5.
Changed in version 0.9.6: Names are now used instead of indexes.
Changed in version 0.9.7: New format with quotes and alphabet
Module: Conversion of Finite Automata: (FAdo.conversions)¶
Conversions between objects.
Deterministic and non-deterministic automata manipulation, conversion and evaluation. .. Authors: Rogério Reis & Nelma Moreira .. This is part of FAdo project http://fado.dcc.fc.up.pt.
Classes¶
Functions¶
- FAdo.conversions.FAallRegExps(aut)[source]¶
Evaluates the alphabetic length of the equivalent regular expression using every possible order of state elimination.
- FAdo.conversions.cutPoints(aut)[source]¶
Set of FA’s cut points
- Parameters
aut (OFA) – the automaton
- Returns
set of states
- Return type
set of int
- FAdo.conversions.FA2regexpSE(aut)[source]¶
A regular expression obtained by state elimination algorithm whose language is recognised by the FA aut.
- Parameters
aut (OFA) – the automaton
- Returns
the equivalent regular expression
- Return type
- FAdo.conversions.SP2regexp(aut)[source]¶
Checks if FA is SP (Serial-PArallel), and if so returns the regular expression whose language is recognised by the FA
- Parameters
aut (OFA) – the automaton
- Returns
equivalent regular expression
- Return type
- Raises
NotSP – if the automaton is not Serial-Parallel
See also
Moreira & Reis, Fundamenta Informatica, Series-Parallel automata and short regular expressions, n.91 3-4, pag 611-629. https://www.dcc.fc.up.pt/~nam/publica/spa07.pdf
Note
Automata must be Serial-Parallel
- FAdo.conversions.FAeliminateSingles(aut)[source]¶
Eliminates every state that only have one successor and one predecessor.
- FAdo.conversions.FA2regexpCG(aut)[source]¶
Regular expression from state elimination whose language is recognised by the FA. Uses a heuristic to choose the order of elimination.
- Parameters
aut (OFA) – the automaton
- Returns
the equivalent regular expression
- Return type
- FAdo.conversions.FA2regexpCG_nn(aut: FAdo.fa.OFA)[source]¶
Regular expression from state elimination whose language is recognised by the FA. Uses a heuristic to choose the order of elimination. The FA is not normalized before the state elimination.
- Parameters
aut (OFA) – the automaton
- Returns
the equivalent regular expression
- Return type
- FAdo.conversions.FA2regexpSEO(aut, order=None)[source]¶
Regular expression from state elimination whose language is recognised by the FA. The FA is normalized before the state elimination.
- Parameters
- Returns
the equivalent regular expression
- Return type
- FAdo.conversions.FA2regexpDynamicCycleHeuristic(aut)[source]¶
State elimination Heuristic based on the number of cycles that passes through each state. Here those numbers are evaluated dynamically after each elimination step
- Parameters
aut (OFA) – the automaton
- Returns
an equivalent regular expression
- Return type
See also
Nelma Moreira, Davide Nabais, and Rogério Reis. State elimination ordering strategies: Some experimental results. Proc. of 11th Workshop on Descriptional Complexity of Formal Systems (DCFS10), pages 169-180.2010. DOI: 10.4204/EPTCS.31.16
- FAdo.conversions.FA2regexpStaticCycleHeuristic(aut)[source]¶
State elimination Heuristic based on the number of cycles that passes through each state. Here those numbers are evaluated statically in the beginning of the process
- Parameters
aut (OFA) – the automaton
- Returns
a equivalent regular expression
- Return type
See also
Nelma Moreira, Davide Nabais, and Rogério Reis. State elimination ordering strategies: Some experimental results. Proc. of 11th Workshop on Descriptional Complexity of Formal Systems (DCFS10), pages 169-180.2010. DOI: 10.4204/EPTCS.31.16
- FAdo.conversions.FA2regexpSE_nn(aut, order=None)[source]¶
Regular expression from state elimination whose language is recognised by the FA. The FA is not normalized before the state elimination.
- Parameters
- Returns
the equivalent regular expression
- Return type
- FAdo.conversions.DFA2regexpDijkstra(aut) FAdo.reex.regexp[source]¶
Returns a regexp for the current DFA considering the recursive method. Very inefficent.
- Parameters
aut (DFA) – the automaton
- Returns
a regexp equivalent to the current DFA
- Return type
Module: Common Definitions (common)¶
Common definitions for FAdo files
Classes¶
Module: FAdo IO Functions (fio)¶
In/Out.
FAdo IO.
Class BuildFadoObject (Semantics of the FAdo grammars’ objects)¶
Functions¶
- FAdo.fio.readFromFile(FileName)[source]¶
Reads list of finite automata definition from a file.
The format of these files must be the as simple as possible:
#begins a comment@DFAor@NFAbegin a new automata (and determines its type) and must be followed by the list of the final states separated by blanksfields are separated by a blank and transitions by a CR:
statesymbolnew statein case of a NFA declaration, the “symbol” @epsilon is interpreted as a epsilon-transition
the source state of the first transition is the initial state
in the case of a NFA, its declaration
@NFAcan, after the declaration of the final states, have a*followed by the list of initial statesboth, NFA and DFA, may have a declaration of alphabet starting with a
$followed by the symbols of the alphabeta line with a sigle name, decrares a state
FAdo ::= FA | FA CR FAdo FA ::= DFA | NFA | Transducer DFA ::= "@DFA" LsStates Alphabet CR dTrans NFA ::= "@NFA" LsStates Initials Alphabet CR nTrans Transducer ::= "@Transducer" LsStates Initials Alphabet Output CR tTrans Initials ::= "*" LsStates | /epsilon Alphabet ::= "$" LsSymbols | /epsilon Output ::= "$" LsSymbols | /epsilon nSymbol ::= symbol | "@epsilon" LsStates ::= stateid | stateid , LsStates LsSymbols ::= symbol | symbol , LsSymbols dTrans ::= stateid symbol stateid | | stateid symbol stateid CR dTrans nTrans ::= stateid nSymbol stateid | | stateid nSymbol stateid CR nTrans tTrans ::= stateid nSymbol nSymbol stateid | | stateid nSymbol nSymbol stateid CR nTransNote
If an error occur, either syntactic or because of a violation of the declared automata type, an exception is raised
Changed in version 0.9.6.
Changed in version 1.0.
- FAdo.fio.readOneFromString(s)[source]¶
Reads one finite automata definition from a file.
See also
readFromFile for description of format
- FAdo.fio.saveToFile(FileName, fa, mode='a')[source]¶
Saves a list finite automata definition to a file using the input format
Changed in version 0.9.5.
Changed in version 0.9.6.
Changed in version 0.9.7: New format with quotes and alphabet
- FAdo.fio.saveToJson(FileName, aut, mode='w')[source]¶
Saves a finite automata definition to a file using the JSON format
Constants¶
- const
FAdo.fio.FAdoGrammar
Module: Regular Expressions (reex)¶
Regular expressions manipulation
Regular expression classes and manipulation
Classes¶
- class FAdo.reex.regexp(sigma=None)[source]¶
Base class for regular expressions.
- Variables
Sigma – alphabet set of strings

- class FAdo.reex.specialConstant(sigma=None)[source]¶
Base class for Epsilon and EmptySet

- Parameters
sigma – alphabet
- class FAdo.reex.epsilon(sigma=None)[source]¶
Class that represents the empty word.

- Parameters
sigma – alphabet
- class FAdo.reex.emptyset(sigma=None)[source]¶
Class that represents the empty set.

- Parameters
sigma – alphabet
- class FAdo.reex.sigmaP(sigma=None)[source]¶
- Special regular expressions modulo associativity, commutativity, idempotence of disjunction and intersection;
associativity of concatenation; identities Sigma^* and Sigma^+.
sigmaP: Class that represents the complement of the emptyset word (Sigma^+)

- Parameters
sigma – alphabet
- class FAdo.reex.sigmaS(sigma=None)[source]¶
- Special regular expressions modulo associativity, commutativity, idempotence of disjunction and intersection;
associativity of concatenation; identities Sigma^* and Sigma^+.
sigmaS: Class that represents the complement of the emptyset set (Sigma^*)

- Parameters
sigma – alphabet
- class FAdo.reex.connective(arg1, arg2, sigma=None)[source]¶
Base class for (binary) operations: concatenation, disjunction, etc

- class FAdo.reex.star(arg, sigma=None)[source]¶
Class for iteration operation (aka Kleene star, or Kleene closure) on regular expressions.

- class FAdo.reex.concat(arg1, arg2, sigma=None)[source]¶
Class for catenation operation on regular expressions.

- class FAdo.reex.disj(arg1, arg2, sigma=None)[source]¶
Class for disjuction operation on regular expressions.

- class FAdo.reex.power(arg, n=1, sigma=None)[source]¶
Class for power operation on regular expressions.

- class FAdo.reex.atom(val, sigma=None)[source]¶
Simple atom (symbol)
- Variables
Sigma – alphabet set of strings
val – the actual symbol

Constructor of a regular expression symbol.
- Parameters
val – the actual symbol
- class FAdo.reex.position(val, sigma=None)[source]¶
Class for marked regular expression symbols.

Constructor of a regular expression symbol.
- Parameters
val – the actual symbol
- class FAdo.reex.sconnective(arg, sigma=None)[source]¶
- Special regular expressions modulo associativity, commutativity, idempotence of disjunction and intersection;
- associativity of concatenation; identities Sigma^* and Sigma^+. Connectives are:
sdisj: disjunction sconj: intersection sconcat: concatenation
For parsing use str2sre

- class FAdo.reex.sconcat(arg, sigma=None)[source]¶
Class that represents the concatenation operation.

- class FAdo.reex.sstar(arg, sigma=None)[source]¶
- Special regular expressions modulo associativity, commutativity, idempotence of disjunction and intersection;
associativity of concatenation; identities Sigma^* and Sigma^+.
sstar: Class that represents Kleene star

- class FAdo.reex.sdisj(arg, sigma=None)[source]¶
Class that represents the disjunction operation for special regular expressions.

- class FAdo.reex.snot(arg, sigma=None)[source]¶
- Special regular expressions modulo associativity, commutativity, idempotence of disjunction and intersection;
associativity of concatenation; identities Sigma^* and Sigma^+. snot: negation

- class FAdo.reex.dag(reg)[source]¶
Class to support dags representing regexps
- …seealso: P. Flajolet, P. Sipala, J.-M. Steyaert, Analytic variations on the common subexpression problem,
in: Automata, Languages and Programmin, LNCS, vol. 443, Springer, New York, 1990, pp. 220–234.
- Variables
reg (reex) – regular expression
- class FAdo.reex.m_atom(val, mark, sigma=None)[source]¶
Base class for pointed (marked) regular expressions
Used directly to represent atoms (characters). This class is used to obtain Yamada or Asperti automata. There is no evident use for it, outside this module.
- Parameters
val – symbol
sigma – alphabet
Functions¶
- FAdo.reex.str2regexp(s, parser=Lark(open('<string>'), parser='lalr', lexer='contextual', ...), sigma=None, strict=False)[source]¶
Reads a regexp from string.
- Parameters
s (string) – the string representation of the regular expression
parser – a parser generator for regexps
sigma (list or set of symbols) – alphabet of the regular expression
strict (boolean) – if True tests if the symbols of the regular expression are included in sigma
- Return type
- FAdo.reex.str2sre(s, parser=Lark(open('<string>'), parser='lalr', lexer='contextual', ...), sigma=None, strict=False)[source]¶
Reads a sre from string. Arguments as str2regexp.
- Return type
reex.sre
Module: Transducers (transducers)¶
Finite Tranducer Support
Transducer manipulation.
New in version 1.0.
Classes¶
- class FAdo.transducers.SFT[source]¶
Standard Form Tranducer
- Variables
Output (set) – output alphabet

- Return type
Functions¶
- FAdo.transducers.hypercodeTransducer(alphabet, preserving=False)[source]¶
Creates an hypercode property transducer based on given alphabet
- FAdo.transducers.infixTransducer(alphabet, preserving=False)[source]¶
Creates an infix property transducer based on given alphabet
- FAdo.transducers.isLimitExceed(NFA0Delta, NFA1Delta)[source]¶
Decide if the size of NFA0 and NFA1 exceed the limit.
Size of NFA0 is denoted as N, and size of NFA1 is denoted as M. If N*N*M exceeds 1000000, return False, else return True. If bothNFA is False, then NFA0 should be NFA, and NFA1 should be Transducer. If both NFA is True, then NFA0 and NFA1 are both NFAs.
- FAdo.transducers.outfixTransducer(alphabet, preserving=False)[source]¶
Creates an outfix property transducer based on given alphabet
Module: Finite Languages (fl)¶
Finite languages and related automata manipulation
Finite languages manipulation
Classes¶
- class FAdo.fl.FL(wordsList=None, Sigma=None)[source]¶
Finite Language Class
- Variables
Words – the elements of the language
Sigma – the alphabet
Functions¶
- FAdo.fl.sigmaInitialSegment(Sigma, l, exact=False)[source]¶
Generates the ADFA recognizing Sigma^i for i<=l :param set Sigma: the alphabet :param int l: length :param bool exact: only the words with exactly that length? :returns: the automaton :rtype: ADFA
- FAdo.fl.genRndTrieBalanced(maxL, Sigma, safe=True)[source]¶
Generates a random trie automaton for a binary language of balanced words of a given leght for max word :param int maxL: length of the max word :param set Sigma: alphabet to be used :param bool safe: should a word of size maxl be present in every language? :return: the generated trie automaton :rtype: ADFA
- FAdo.fl.genRndTrieUnbalanced(maxL, Sigma, ratio, safe=True)[source]¶
Generates a random trie automaton for a binary language of balanced words of a given length for max word
- FAdo.fl.genRandomTrie(maxL, Sigma, safe=True)[source]¶
Generates a random trie automaton for a finite language with a given length for max word :param int maxL: length of the max word :param set Sigma: alphabet to be used :param bool safe: should a word of size maxl be present in every language? :return: the generated trie automaton :rtype: ADFA
- FAdo.fl.genRndTriePrefix(maxL, Sigma, ClosedP=False, safe=True)[source]¶
Generates a random trie automaton for a finite (either prefix free or prefix closed) language with a given length for max word :param int maxL: length of the max word :param set Sigma: alphabet to be used :param bool ClosedP: should it be a prefix closed language? :param bool safe: should a word of size maxl be present in every language? :return: the generated trie automaton :rtype: ADFA
- FAdo.fl.stringToADFA(s)[source]¶
Convert a canonical string representation of a ADFA to a ADFA :param list s: the string in its canonical order :returns: the ADFA :rtype: ADFA
See also
Marco Almeida, Nelma Moreira, and Rogério Reis. Exact generation of minimal acyclic deterministic finite automata. International Journal of Foundations of Computer Science, 19(4):751-765, August 2008.
Module: graphs (graph creation and manipulation)¶
Graph support
Basic Graph object support and manipulation
Module: Context Free Grammars Manipulation (cfg)¶
Context Free Grammars Manipulation.
Basic context-free grammars manipulation for building uniform random generetors
Classes¶
- class FAdo.cfg.CFGrammar(gram)[source]¶
Class for context-free grammars
- Variables
Rules – grammar rules
Terminals – terminals symbols
Nonterminals – nonterminals symbols
Start (str) – start symbol
ntr – dictionary of rules for each nonterminal
Initialization
- Parameters
gram – is a list for productions; each production is a tuple (LeftHandside, RightHandside) with LeftHandside nonterminal, RightHandside list of symbols, First production is for start symbol
- class FAdo.cfg.CNF(gram, mark='A@')[source]¶
No useless nonterminals or epsilon rules are ALLOWED… Given a CFG grammar description generates one in CNF Then its possible to random generate words of a given size. Before some pre-calculations are nedded.
Initialization
- Parameters
gram – is a list for productions; each production is a tuple (LeftHandside, RightHandside) with LeftHandside nonterminal, RightHandside list of symbols, First production is for start symbol
- class FAdo.cfg.cfgGenerator(cfgr, size)[source]¶
CFG uniform genetaror
Object initialization :param cfgr: grammar for the random objects :type cfgr: CNF :param size: size of objects :type size: integer
- class FAdo.cfg.reStringRGenerator(Sigma=None, size=10, cfgr=None, epsilon=None, empty=None, ident='Ti')[source]¶
Uniform random Generator for reStrings
- Uniform random generator for regular expressions. Used without arguments generates an uncollapsible re
over {a,b} with size 10. For generate an arbitary re over an alphabet of 10 symbols of size 100: reStringRGenerator (smallAlphabet(10),100,reGrammar[“g_regular_base”])
- Parameters
Note
the grammar can have already this symbols
Functions¶
- FAdo.cfg.gRules(rules_list, rulesym='->', rhssep=None, rulesep='|')[source]¶
Transforms a list of rules into a grammar description.
- Parameters
rules_list – is a list of rule where rule is a string of the form: Word rulesym Word1 … Word2 or Word rulesym []
rulesym – LHS and RHS rule separator
rhssep – RHS values separator (None for white chars)
- Returns
a grammar description
Constants¶
- const
reGrammar
Module: Random DFA Generator (rndfap)¶
Random DFA generation (alternative version in python)
ICDFA Random generation binding
New in version 1.0.
Classes¶
- class FAdo.rndfap.ICDFArgen(n, k, nd=False, pn=1, seed=0)[source]¶
Generic ICDFA random generator class
- Variables
See also
Marco Almeida, Nelma Moreira, and Rogério Reis. Enumeration and generation with a string automata representation. Theoretical Computer Science, 387(2):93-102, 2007
Changed in version 1.3.4: seed added to the random generator
- class FAdo.rndfap.ICDFArnd(n, k, seed=0)[source]¶
Complete ICDFA random generator class
This is the class for the uniform random generator for Initially Connected DFAs
- Variables
Note
This is an abstract class, not to be used directly
Changed in version 1.3.4: seed added to the random generator
Module: Random ADFA Generator (rndadfa)¶
Random ADFA generation
ADFA Random generation binding
New in version 1.2.1.
Classes¶
Module: Combo Operations (comboperations)¶
Several combined operations for DFAs
Combined operations
Functions¶
- FAdo.comboperations.starConcat(fa1, fa2, strict=False)[source]¶
Star of concatenation of two languages: (L1.L2)*
- Parameters
- Return type
See also
Yuan Gao, Kai Salomaa, and Sheng Yu. ‘The state complexity of two combined operations: Star of catenation and star of reversal’. Fundamenta Informaticae, 83:75–89, Jan 2008.
- FAdo.comboperations.concatWStar(fa1, fa2, strict=False)[source]¶
Concatenation combined with star: (L1.L2*)
- Parameters
- Return type
See also
Bo Cui, Yuan Gao, Lila Kari, and Sheng Yu. ‘State complexity of two combined operations: Reversal-catenation and star-catenation’. CoRR, abs/1006.4646, 2010.
- FAdo.comboperations.starWConcat(fa1, fa2, strict=False)[source]¶
Star combined with concatenation: (L1*.L2)
- Parameters
- Return type
See also
Bo Cui, Yuan Gao, Lila Kari, and Sheng Yu. ‘State complexity of catenation combined with star and reversal’. CoRR, abs/1008.1648, 2010
- FAdo.comboperations.starDisj(fa1, fa2, strict=False)[source]¶
Star of Union of two DFAs: (L1 + L2)*
- Parameters
- Return type
See also
Arto Salomaa, Kai Salomaa, and Sheng Yu. ‘State complexity of combined operations’. Theor. Comput. Sci., 383(2-3):140–152, 2007.
- FAdo.comboperations.starInter0(fa1, fa2, strict=False)[source]¶
Star of Intersection of two DFAs: (L1 & L2)*
- Parameters
- Return type
See also
Arto Salomaa, Kai Salomaa, and Sheng Yu. ‘State complexity of combined operations’. Theor. Comput. Sci., 383(2-3):140–152, 2007.
- FAdo.comboperations.starInter(fa1, fa2, strict=False)[source]¶
Star of Intersection of two DFAs: (L1 & L2)*
- FAdo.comboperations.disjWStar(f1, f2, strict=True)[source]¶
Union with star: (L1 + L2*)
- Parameters
- Return type
See also
Yuan Gao and Sheng Yu. ‘State complexity of union and intersection combined with star and reversal’. CoRR, abs/1006.3755, 2010.
Module: Codes (codes)¶
Code theory module
New in version 1.0.
Classes¶
- class FAdo.codes.CodeProperty(name, alph)[source]¶
See also
K. Dudzinski and S. Konstantinidis: Formal descriptions of code properties: decidability, complexity, implementation. International Journal of Foundations of Computer Science 23:1 (2012), 67–85.
- Variables
Sigma – the alphabet
- class FAdo.codes.IPTProp(aut, name=None)[source]¶
Input Preserving Transducer Property

Constructor :param SFT aut: Input preserving transducer
- class FAdo.codes.IATProp(aut, name=None)[source]¶
Input Altering Transducer Property

Constructor :param SFT aut: Input preserving transducer
- FAdo.codes.ErrDetectProp¶
alias of
FAdo.codes.IPTProp
Functions¶
- FAdo.codes.buildErrorDetectPropF(fname)[source]¶
Builds an Error Detecting Property
- Parameters
fname (str) – file name
- Return type
ErrDetectProp
- FAdo.codes.buildErrorCorrectPropF(fname)[source]¶
Builds an Error Correcting Property
- Parameters
fname (str) – file name
- Return type
- FAdo.codes.buildErrorDetectPropS(s)[source]¶
Builds an Error Detecting Property from string
- Parameters
s (str) – transducer string
- Return type
ErrDetectProp
- FAdo.codes.buildErrorCorrectPropS(s)[source]¶
Builds an Error Correcting Property from string
- Parameters
s (str) – transducer string
- Return type
- FAdo.codes.buildPrefixProperty(alphabet)[source]¶
Builds a Prefix Code Property
- Parameters
alphabet (set) – alphabet
- Return type
- FAdo.codes.editDistanceW(auto)[source]¶
Compute the edit distance of a given regular language accepted by the NFA via Input-altering transducer.
Attention
language should have at least two words
See also
Lila Kari, Stavros Konstantinidis, Steffen Kopecki, Meng Yang. An efficient algorithm for computing the edit distance of a regular language via input-altering transducers. arXiv:1406.1041 [cs.FL]
- FAdo.codes.exponentialDensityP(aut)[source]¶
Checks if language density is exponential
Using breadth first search (BFS)
Attention
aut should not have Epsilon transitions
- FAdo.codes.createInputAlteringSIDTrans(n, sigmaSet)[source]¶
Create an input-altering SID transducer based
# ============================================================
# Module: Set Specification Transducers and Automata (sst)
# ============================================================
#
# .. automodule:: sst
# :no-members:
#
# ———
# Class PSP
# ———
#
# .. autoclass:: PSP
#
==============
Small Tutorial
==============
A small tutorial for FAdo¶
FAdo system is a set tools for regular languages manipulation.
Regular languages can be represented by regular expressions (regexp) or finite automata, among other formalisms. Finite automata may be deterministic (DFA) or non-deterministic (NFA). In FAdo these representations are implemented as Python classes. A full documentation of all classes and methods is here.
To work with FAdo, after installation, import the following modules on a Python interpreter:
>>> from FAdo.fa import *
>>> from FAdo.reex import *
>>> from FAdo.fio import *
The module fa implements the classes for finite automata and the module reex the classes for regular expressions. The module fio implements methods for IO of automata and related models.
General conventions
Methods which name ends in P test if the object verifies a given property and return True or False.
Finite Automata
The top class for finite automata is the class FA,which has two main subclasses: OFA for one way finite automata and the class TFA for two-way finite automata. The class OFA implements the basic structure of a finite automaton shared by DFAs and NFAs. This class defines the following attributes:
Sigma: the input alphabet (set)
States: the list of states. It is a list such that each state is referred by its index whenever it is used (transitions, Final, etc).
Initial:the initial state (or a set of initial states for NFA). It is an index or list of indexes.
Final: the set of final states. It is a list of indexes.
In general, one should not create instances (objects) of class OFA. The class DFA and NFA implement DFAs and NFAs, respectively. The class GFA implements generalized NFAs that are used in the conversion between finite automata and regular expressions. All three classes inherit from class OFA.
For each class there are special methods for add/delete/modify alphabet symbols, states and transitions.
DFAs
The following example shows how to build a DFA that accepts the words of {0,1}* that are multiples of 3.
>>> m3= DFA()
>>> m3.setSigma(['0','1'])
>>> m3.addState('s1')
>>> m3.addState('s2')
>>> m3.addState('s3')
>>> m3.setInitial(0)
>>> m3.addFinal(0)
>>> m3.addTransition(0, '0', 0)
>>> m3.addTransition(0, '1', 1)
>>> m3.addTransition(1, '0', 2)
>>> m3.addTransition(1, '1', 0)
>>> m3.addTransition(2, '0', 1)
>>> m3.addTransition(2, '1', 2)
It is now possible, for instance, to see the structure of the automaton or to test if a word is accepted by it.
>>> m3
DFA((['s1', 's2', 's3'], ['1', '0'], 's1', ['s1'], "[('s1', '1', 's2'), ('s1', '0', 's1'), ('s2', '1', 's1'), ('s2', '0', 's3'), ('s3', '1', 's3'), ('s3', '0', 's2')]"))
>>> m3.evalWordP("011")
True
>>> m3.evalWordP("1011")
False
>>>
If graphviz is installed it is also possible to display the diagram of an automaton as follows:
>>>m3.display()
Instead of constructing the DFA directly we can load (and save) it in a simple text format. For the previous automaton the description will be:
Then, if this description is saved in file mul3.fa, we have
>>> m3=readFromFile(“mul3.fa”)[0]
As the set of states is represented by a Python list , the list method len can be used to determine the number of states of a FA:
>>> len(m3.States)
3
For the number of Transitions the countTransitions() method must be used
>>> m3.countTransitions()
6
To minimize a DFA any of the minimization algorithms implemented can be used:
>>> min=m3.minimalHopcroft()
In this case, the DFA was already minimal so min has the same number of states as m3.
Several (regularity preserving) operations of DFAs are implemented in FAdo: boolean (union (| or __or__), intersection (& or __and__) and complementation (~ or __invert__)), concatenation (concat), reversal (reversal) and star (star).
>>> u = m3 | ~m3
>>> u
DFA(([(1, 1), (0, 0), (2, 2)], set(['1', '0']), 0,set([0, 1, 2]), {0: {'1': 1, '0': 0}, 1: {'1': 0, '0': 2}, 2:{'1': 2, '0': 1}}))
>>> m = u.minimal()
>>> m
DFA((['(1, 1)'], ['1', '0'], '(1, 1)', ['(1, 1)'], "[('(1, 1)', '1', '(1, 1)'), ('(1, 1)', '0', '(1, 1)')]"))
State names can be renamed in-place using:
>>> m.renameStates(range(len(m)))
DFA(([‘0’], [‘1’, ‘0’], ‘0’, [‘0’], “[(0, ‘1’, 0), (0, ‘0’, 0)]”))
Notice that m recognize all words over the alphabet {0.1}.
It is possible to generate a word recognisable by an automata (witness)
>>> u.witness()
'@epsilon'
In this case this allows to ensure that u recognizes the empty word.
This method is also useful for obtain a witness for the difference of two DFAs (witnessDiff).
To test if two DFAs are equivalent the the operator == (equivalenceP) can be used.
NFAs
NFAs can be built and manipulated in a similar way. There is no distinction between NFAs with and without epsilon-transitions. But it is possible to test if a NFA has epsilon-transitions and convert between a NFA with epsilon-transitions to a (equivalent) NFA without them.
Converting between NFAs and DFAs
The method toDFA allows to convert a NFA to an equivalent DFA by the subset construction method. The method toNFA migrates trivially a DFA to a NFA.
Regular Expressions
A regular expression can be a symbol of the alphabet, the empty set (@epmtyset), the empty word (@epsilon) or the concatenation or the union (+) or the Kleene star (*) of a regular expression. Examples of regular expressions are a+b, (a+ba)*, and (@epsilon+ a)(ba+ab+@emptyset).
The class regexp is the base class for regular expressions and is used to represent an alphabet symbol. The classes epsilon and emptyset are the subclasses used for the empty set and empty word, respectively. Complex regular expressions are concat, disj, and star.
As for DFAs (and NFAs) we can build directly a regular expressions as a Python class:
>>> r = star(disj(regexp("a"),concat(regexp("b"),regexp("a"))))
>>> print r
(a + (b a))*
But we can convert a string to a regexp class or subclass, using the method str2regexp.
>>> r = str2regexp("(a+ba)*")
>>> print r
(a + (b a))*
For regular expressions there are several measures available: alphabetic size, (parse) tree size, string length, number of epsilons and star height. It is also possible to explicitly associate an alphabet to regular expression (even if some symbols do not appear in it) (setSigma)
There are several algebraic properties that can be used to obtain equivalent regular expressions of a smaller size. The method reduced transforms a regular expression into one equivalent without some obvious unnecessary epsilons, emptysets or stars.
Several methods that allows the manipulation of derivatives (or partial derivatives) by a symbol or by a word are implemented. However, the class regexp does not deal with regular expressions module ACI properties (associativity, commutativity and idempotence of the union) (see class xre) , a so it is not possible to obtain all word derivatives of a given regular expression. This is not the case for partial derivatives.
To test if two regular expressions are equivalent the method compare can be used.
>>> r.compare(str2regexp(\"(a*(ba)*a*)*\"))
True
>>>
Converting Finite Automata to Regular Expressions
For pedagogical purposes, it is implemented a recursive method that constructs a regular expression equivalent to a given DFA (regexp).
>>> print m3.regexp()
((0 + ((@epsilon + 0) (0* (@epsilon + 0)))) + ((1 +((@epsilon + 0) (0* 1))) ((1 (0* 1))* (1 + (1 (0*(@epsilon + 0))))))) + (((1 + ((@epsilon + 0) (0* 1)))((1 (0* 1))* 0)) ((1 + (0 ((1 (0* 1))* 0)))* (0 ((1(0* 1))* (1 + (1 (0* (@epsilon + 0))))))))
Methods based on state elimination techniques are usually more efficient, and produces much smaller regular expressions. We have implemented several heuristics for the elimination order.
>>> print m3.reCG()
((0 + (1 1)) + (((1 0) (1 + (0 0))*) (0 1)))*
Converting Regular Expressions to Finite Automata
Several methods to convert between regular expressions and NFAs are implemented. With the Thompson construction a NFA with epsilon transitions is obtained (nfaThompson). Epsilon free NFAs can be obtained by the Glushkov method (Position automata) (nfaPosition,) the partial derivatives method (nfaPD – several implementations) or by the follow method (nfaFollow). The two last methods usually allows to obtain smaller NFAs.
>>> r.nfaThompson()
NFA((['', '', '', '', '0', '1', '2', '3', '8', '9'], ['a', 'b'], ['8'], ['9'], "[('', '@epsilon', ''), ('', '@epsilon', 0), ('', '@epsilon', '9'), ('', 'a', ''), ('', '@epsilon', ''), (0, 'b', 1), (1, '@epsilon', 2), (2, 'a', 3), (3, '@epsilon', ''), ('8', '@epsilon', ''), ('8', '@epsilon', '9'), ('9', '@epsilon', '8')]"))
>>> r.nfaPosition()
NFA((['Initial', "('a', 1)", "('b', 2)", "('a', 3)"], ['a', 'b'], ['Initial'], ['Initial', "('a', 1)", "('a', 3)"], '[(\'Initial\', \'a\', "(\'a\', 1)"), (\'Initial\', \'b\', "(\'b\', 2)"), ("(\'a\', 1)", \'a\', "(\'a\', 1)"), ("(\'a\', 1)", \'b\', "(\'b\', 2)"), ("(\'b\', 2)", \'a\', "(\'a\', 3)"), ("(\'a\', 3)", \'a\', "(\'a\', 1)"), ("(\'a\', 3)", \'b\', "(\'b\', 2)")]'))
>>> r.nfaPD()
NFA((['(a + (b a))*', 'a (a + (b a))*'], ['a', 'b'], ['(a + (b a))*'], ['(a + (b a))*'], "[(star(disj(regexp(a),concat(regexp(b),regexp(a)))), 'a', star(disj(regexp(a),concat(regexp(b),regexp(a))))), (star(disj(regexp(a),concat(regexp(b),regexp(a)))), 'b', concat(regexp(a),star(disj(regexp(a),concat(regexp(b),regexp(a)))))), (concat(regexp(a),star(disj(regexp(a),concat(regexp(b),regexp(a))))), 'a', star(disj(regexp(a),concat(regexp(b),regexp(a)))))]"))
General Example
Considering the several methods described before it is possible to convert between the different equivalent representations of regular languages, as well to perform several regularity preserving operations.
>>> r.nfaPosition().toDFA().minimal(complete=False)
DFA((['0', '2'], ['a', 'b'], '0', ['0'], "[('0', 'a', '0'), ('0', 'b', '2'), ('2', 'a', '0')]"))
>>> m3 == m3.reCG().nfaPD().toDFA().minimal()
True
>>>
More classes and modules¶
Several other classes and modules are also available, including:
class ICDFArnd (module rndfa.py): Random DFA generation
class FL (module fl.py): special methods for finite languages
module comboperations.py: implementation of several algorithms for several combined operations with DFAs and NFAs
# module grail.py: compatibility with GRAIL
module transducers.py: several classes and methods for transducers
module codes.py: language tests for a property (set of languages) specified by a transducer














