Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
CockeYoungerKasamiVerbose.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <global/GlobalData.h>
9
11#include <string/LinearString.h>
12
13namespace grammar {
14
15namespace generate {
16
21public:
33 template < class TerminalSymbolType, class NonterminalSymbolType >
35
36};
37
38template < class TerminalSymbolType, class NonterminalSymbolType >
40 unsigned stringSize = string.getContent ( ).size ( );
41
42 if ( ( stringSize == 0 ) && grammar.getGeneratesEpsilon ( ) ) return { };
43
45 data.resize ( stringSize );
46
47 for ( unsigned i = 0; i < stringSize; i++ )
48 data[i].resize ( stringSize - i );
49
50 for ( unsigned i = 0; i < stringSize; i++ )
51 for ( const std::pair < const NonterminalSymbolType, ext::set < ext::variant < TerminalSymbolType, ext::pair < NonterminalSymbolType, NonterminalSymbolType > > > > & rule : grammar.getRules ( ) ) {
52 const NonterminalSymbolType & lhs = rule.first;
53
54 for ( const ext::variant < TerminalSymbolType, ext::pair < NonterminalSymbolType, NonterminalSymbolType > > & rhs : rule.second )
55 if ( rhs.template is < TerminalSymbolType > ( ) && ( rhs.template get < TerminalSymbolType > ( ) == string.getContent ( )[i] ) )
56 data[0][i].insert ( lhs );
57
58 }
59
60 for ( unsigned i = 1; i < stringSize; i++ )
61 for ( unsigned j = 0; j < stringSize - i; j++ ) {
62 ext::set < NonterminalSymbolType > & targetCell = data[i][j]; // Element to compute
63
64 for ( unsigned k = 0; k < i; k++ ) {
65 const ext::set < NonterminalSymbolType > & vertical = data[k][j];
66 const ext::set < NonterminalSymbolType > & diagonal = data[i - 1 - k][j + 1 + k]; // Sources of data
67
68 for ( const NonterminalSymbolType & verticalElement : vertical ) {
69 for ( const NonterminalSymbolType & diagonalElement : diagonal )
70
71 for ( const std::pair < const NonterminalSymbolType, ext::set < ext::variant < TerminalSymbolType, ext::pair < NonterminalSymbolType, NonterminalSymbolType > > > > & rule : grammar.getRules ( ) ) {
72 const NonterminalSymbolType & lhs = rule.first;
73
74 for ( const ext::variant < TerminalSymbolType, ext::pair < NonterminalSymbolType, NonterminalSymbolType > > & rhs : rule.second )
76 const ext::pair < NonterminalSymbolType, NonterminalSymbolType > & rhsp = rhs.template get < ext::pair < NonterminalSymbolType, NonterminalSymbolType > > ( );
77
78 if ( ( rhsp.first == verticalElement ) && ( rhsp.second == diagonalElement ) )
79 targetCell.insert ( lhs );
80 }
81
82 }
83
84 }
85 }
86 }
87
88
89
91 for ( const ext::vector < ext::set < NonterminalSymbolType > > & row : data ) {
92 for ( const ext::set < NonterminalSymbolType > & element : row )
93 common::Streams::log << element << " ";
94
95 common::Streams::log << std::endl;
96 }
97
98 return data;
99}
100
101} /* namespace generate */
102
103} /* namespace grammar */
104
static bool verbose
Verbose flag. Some algorithms print additional runtime information about internal datastructures or t...
Definition: GlobalData.h:24
static ext::reference_wrapper< ext::ostream > log
Standard loging stream. Mapped to descriptor 4.
Definition: GlobalData.h:78
Class extending the pair class from the standard library. Original reason is to allow printing of the...
Definition: pair.hpp:43
Definition: set.hpp:44
Implementation of the variant class allowing to store any type of those listed in the template parame...
Definition: variant.hpp:98
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
reverse_iterator insert(const_reverse_iterator pos, const T &value)
Inserts the value on position given by iterator pos.
Definition: vector.hpp:229
Chomsky normal form of a context free grammar in Chomsky hierarchy or type 2 in Chomsky hierarchy....
Definition: CNF.h:66
Definition: CockeYoungerKasamiVerbose.h:20
static ext::vector< ext::vector< ext::set< NonterminalSymbolType > > > generate(const grammar::CNF< TerminalSymbolType, NonterminalSymbolType > &grammar, const string::LinearString< TerminalSymbolType > &string)
Definition: CockeYoungerKasamiVerbose.h:39
Linear string.
Definition: LinearString.h:57
int i
Definition: AllEpsilonClosure.h:118
Definition: ToAutomaton.h:24