Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
ToGrammarRightRG.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/NFA.h>
29#include <automaton/FSM/DFA.h>
30
32
33namespace automaton {
34
35namespace convert {
36
41public:
51 template < class T >
52 requires isDFA < T > || isNFA < T >
54};
55
56template < class T >
57requires isDFA < T > || isNFA < T >
59 using SymbolType = typename T::SymbolType;
60 using StateType = typename T::StateType;
61
62 // step 3 - set start symbol of G
64
65 grammar.setTerminalAlphabet ( automaton.getInputAlphabet ( ) );
66 grammar.setNonterminalAlphabet ( automaton.getStates ( ) );
67
68 // step 2 - create set of P in G
69 for ( const auto& transition : automaton.getTransitions ( ) ) {
70 const auto & from = transition.first.first;
71 const auto & input = transition.first.second;
72 const auto & to = transition.second;
73
74 grammar.addRule ( from, ext::make_pair ( input, to ) ); // 2a
75
76 if ( automaton.getFinalStates ( ).contains ( to ) ) // 2b
77 grammar.addRule ( from, input );
78 }
79
80 // step 4
81 if ( automaton.getFinalStates ( ).contains ( automaton.getInitialState ( ) ) )
82 grammar.setGeneratesEpsilon ( true ); // okay this feature makes the algorithm a bit different but at the same time it simplifies the code :))
83
84 return grammar;
85}
86
87} /* namespace convert */
88
89} /* namespace automaton */
90
Definition: ToGrammarRightRG.h:40
static grammar::RightRG< typename T::SymbolType, typename T::StateType > convert(const T &automaton)
Right regular grammar in Chomsky hierarchy or type 3 in Chomsky hierarchy. Generates regular language...
Definition: RightRG.h:70
typename T::StateType StateType
Definition: ToGrammarLeftRG.h:64
return grammar
Definition: ToGrammarLeftRG.h:99
typename T::SymbolType SymbolType
Definition: ReachableStates.h:176
Definition: ToGrammar.h:31
Definition: converterCommon.hpp:8
constexpr auto make_pair(T1 &&x, T2 &&y)
Definition: pair.hpp:79
Definition: ToAutomaton.h:24