Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
ToRegExpAlgebraic.h
Go to the documentation of this file.
1
6/*
7 * This file is part of Algorithms library toolkit.
8 * Copyright (C) 2017 Jan Travnicek (jan.travnicek@fit.cvut.cz)
9
10 * Algorithms library toolkit is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 3 of the License, or
13 * (at your option) any later version.
14
15 * Algorithms library toolkit is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19
20 * You should have received a copy of the GNU General Public License
21 * along with Algorithms library toolkit. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24#pragma once
25
27
28#include <automaton/FSM/DFA.h>
29#include <automaton/FSM/NFA.h>
32
35
36namespace automaton {
37
38namespace convert {
39
40// http://cs.stackexchange.com/questions/2016/how-to-convert-finite-automata-to-regular-expressions
41// https://github.com/ferno/greenery/blob/bcc0a136335edbe94cd7725fc6e8cce0268d850c/fsm.py
42
49public:
55 template < class SymbolType, class StateType >
57
61 template < class SymbolType, class StateType >
63
67 template < class T >
68 requires isDFA < T > || isNFA < T >
70};
71
72template < class SymbolType, class StateType >
75
76 // initialize equations
77 solver.setVariableSymbols ( automaton.getStates ( ) );
78
79 for ( const StateType & q : automaton.getFinalStates ( ) )
81
82 for ( const auto & p : automaton.getSymbolTransitions ( ) )
83 solver.addEquation ( p.first.first, p.second, regexp::UnboundedRegExpSymbol < SymbolType > { p.first.second } );
84
85 for( const auto & p : automaton.getEpsilonTransitions ( ) )
86 solver.addEquation ( p.first, p.second, regexp::UnboundedRegExpEpsilon < SymbolType > { } );
87
88 return solver.solve ( automaton.getInitialState ( ) );
89}
90
91template < class SymbolType, class StateType >
94
95 // initialize equations
96 solver.setVariableSymbols ( automaton.getStates ( ) );
97
98 for ( const StateType & q : automaton.getFinalStates ( ) )
100
101 for ( const auto & p : automaton.getTransitions ( ) )
102 solver.addEquation ( p.first.first, p.second, regexp::UnboundedRegExpSymbol < SymbolType > { p.first.second } );
103
105 for ( const StateType & initialSymbol : automaton.getInitialStates ( ) )
106 alternation.appendElement ( solver.solve ( initialSymbol ).getRegExp ( ).getStructure ( ) ); // set symbol for which the solver will solve equation system
107
109}
110
111template < class T >
112requires isDFA < T > || isNFA < T >
114 using SymbolType = typename T::SymbolType;
115 using StateType = typename T::StateType;
116
118
119 // initialize equations
120 solver.setVariableSymbols ( automaton.getStates ( ) );
121
122 for ( const StateType & q : automaton.getFinalStates ( ) )
124
125 for ( const auto & p : automaton.getTransitions ( ) )
126 solver.addEquation ( p.first.first, p.second, regexp::UnboundedRegExpSymbol < SymbolType > { p.first.second } );
127
128 return solver.solve ( automaton.getInitialState ( ) );
129}
130
131} /* namespace convert */
132
133} /* namespace automaton */
134
Epsilon nondeterministic finite automaton. Accepts regular languages.
Definition: EpsilonNFA.h:74
Nondeterministic finite automaton with multiple initial states. Accepts regular languages.
Definition: MultiInitialStateNFA.h:69
Definition: ToRegExpAlgebraic.h:48
static regexp::UnboundedRegExp< typename T::SymbolType > convert(const T &automaton)
static regexp::UnboundedRegExp< SymbolType > convert(const automaton::EpsilonNFA< SymbolType, StateType > &automaton)
Definition: ToRegExpAlgebraic.h:73
Definition: RightRegularEquationSolver.h:16
Represents the alternation operator in the regular expression. The node can have 0 to n children in l...
Definition: UnboundedRegExpAlternation.h:44
void appendElement(UnboundedRegExpElement< SymbolType > &&element)
Definition: UnboundedRegExpAlternation.h:195
Represents the epsilon expression in the regular expression. The node can't have any children.
Definition: UnboundedRegExpEpsilon.h:41
Represents unbounded regular expression structure. Regular expression is stored as a tree of Unbounde...
Definition: UnboundedRegExpStructure.h:47
Represents the symbol in the regular expression. The can't have any children.
Definition: UnboundedRegExpSymbol.h:42
Unbounded regular expression represents regular expression. It describes regular languages....
Definition: UnboundedRegExp.h:80
equations::RightRegularEquationSolver< SymbolType, StateType > solver
Definition: ToRegExpAlgebraic.h:117
typename T::StateType StateType
Definition: ToGrammarLeftRG.h:64
typename T::SymbolType SymbolType
Definition: ReachableStates.h:176
q
Definition: SingleInitialStateEpsilonTransition.h:85
Definition: ToGrammar.h:31
Definition: converterCommon.hpp:8