Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
GrammarToStringComposerCommon.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <ostream>
9
10#include <core/stringApi.hpp>
11
12#include <grammar/RawRules.h>
14
15namespace grammar {
16
18public:
19 template < class T, class TerminalSymbolType = typename grammar::TerminalSymbolTypeOfGrammar < T >, class NonterminalSymbolType = typename grammar::NonterminalSymbolTypeOfGrammar < T > >
20 static void composeCFLikeGrammar(ext::ostream& output, const T& grammar);
21 template < class T, class SymbolType = typename grammar::TerminalSymbolTypeOfGrammar < T > >
22 static void composeCSLikeGrammar(ext::ostream& output, const T& grammar);
23 template < class T, class SymbolType = typename grammar::TerminalSymbolTypeOfGrammar < T > >
24 static void composePreservingCSLikeGrammar(ext::ostream& output, const T& grammar);
25};
26
27template < class T, class TerminalSymbolType, class NonterminalSymbolType >
29 bool first;
30
31 output << " (" << std::endl;
32
33 output << "{";
34 first = false;
35 for(const auto& symbol : grammar.getNonterminalAlphabet() ) {
36 if(first)
37 output << ", ";
38 else
39 first = true;
41 }
42 output << "}," << std::endl;
43 output << "{";
44 first = false;
45 for(const auto& symbol : grammar.getTerminalAlphabet() ) {
46 if(first)
47 output << ", ";
48 else
49 first = true;
51 }
52 output << "}," << std::endl;
53 output << "{ ";
54 first = true;
55 auto rawRules = grammar::RawRules::getRawRules ( grammar );
56 for(const auto& rule : rawRules ) {
57 if(first)
58 first = false;
59 else
60 output << "," << std::endl << " ";
62 output << " ->";
63 bool innerFirst = true;
64 for(const auto& rhs : rule.second) {
65 if(innerFirst)
66 innerFirst = false;
67 else
68 output << " |";
69 for(const auto& symbol : rhs) {
70 output << " ";
72 }
73 }
74 }
75 output << "}," << std::endl;
77 output << ")" << std::endl;
78}
79
80template < class T, class SymbolType >
82 bool first;
83
84 output << " (" << std::endl;
85
86 output << "{";
87 first = false;
88 for(const auto& symbol : grammar.getNonterminalAlphabet() ) {
89 if(first)
90 output << ", ";
91 else
92 first = true;
94 }
95 output << "}," << std::endl;
96 output << "{";
97 first = false;
98 for(const auto& symbol : grammar.getTerminalAlphabet() ) {
99 if(first)
100 output << ", ";
101 else
102 first = true;
104 }
105 output << "}," << std::endl;
106 output << "{";
107 first = true;
108 for(const auto& rule : grammar.getRules() ) {
109 if(first)
110 first = false;
111 else
112 output << "," << std::endl << " ";
113 for(const auto& symbol : rule.first) {
114 output << " ";
116 }
117 output << " ->";
118 bool innerFirst = true;
119 for(const auto& rhs : rule.second) {
120 if(innerFirst)
121 innerFirst = false;
122 else
123 output << " |";
124 for(const auto& symbol : rhs) {
125 output << " ";
127 }
128 }
129 }
130 output << "}," << std::endl;
131 core::stringApi < SymbolType >::compose(output, grammar.getInitialSymbol());
132 output << ")" << std::endl;
133}
134
135template < class T, class SymbolType >
137 bool first;
138
139 output << " (" << std::endl;
140
141 output << "{";
142 first = false;
143 for(const auto& symbol : grammar.getNonterminalAlphabet() ) {
144 if(first)
145 output << ", ";
146 else
147 first = true;
149 }
150 output << "}," << std::endl;
151 output << "{";
152 first = false;
153 for(const auto& symbol : grammar.getTerminalAlphabet() ) {
154 if(first)
155 output << ", ";
156 else
157 first = true;
159 }
160 output << "}," << std::endl;
161 output << "{";
162 first = true;
163 for(const auto& rule : grammar.getRules() ) {
164 if(first)
165 first = false;
166 else
167 output << "," << std::endl << " ";
168 for(const auto& symbol : std::get<0>(rule.first)) {
169 output << " ";
171 }
172 output << " | ";
173 core::stringApi < SymbolType >::compose(output, std::get<1>(rule.first));
174 output << " |";
175 for(const auto& symbol : std::get<2>(rule.first)) {
176 output << " ";
178 }
179 output << " ->";
180 bool innerFirst = true;
181 for(const auto& rhs : rule.second) {
182 if(innerFirst)
183 innerFirst = false;
184 else
185 output << " |";
186 for(const auto& symbol : rhs) {
187 output << " ";
189 }
190 }
191 }
192 output << "}," << std::endl;
193 core::stringApi < SymbolType >::compose(output, grammar.getInitialSymbol());
194 output << ")" << std::endl;
195}
196
197} /* namespace grammar */
198
Definition: ostream.h:14
Definition: GrammarToStringComposerCommon.h:17
static void composeCSLikeGrammar(ext::ostream &output, const T &grammar)
Definition: GrammarToStringComposerCommon.h:81
static void composePreservingCSLikeGrammar(ext::ostream &output, const T &grammar)
Definition: GrammarToStringComposerCommon.h:136
static void composeCFLikeGrammar(ext::ostream &output, const T &grammar)
Definition: GrammarToStringComposerCommon.h:28
static ext::map< NonterminalSymbolType, ext::set< ext::vector< ext::variant< TerminalSymbolType, NonterminalSymbolType > > > > getRawRules(const LG< TerminalSymbolType, NonterminalSymbolType > &grammar)
Definition: RawRules.h:92
Definition: ToAutomaton.h:24
Definition: stringApi.hpp:26