Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
GrammarFromStringParserCommon.h
Go to the documentation of this file.
1
6#pragma once
7
8#include <alib/set>
9#include <alib/vector>
10#include <alib/map>
11#include <alib/tuple>
12
14
15#include <grammar/Grammar.h>
16
17#include <core/stringApi.hpp>
18
19#include <grammar/AddRawRule.h>
20
21namespace grammar {
22
24 template < class SymbolType >
25 static ext::set < SymbolType > parseSet ( ext::istream & input );
26
27 template < class TerminalSymbolType, class NonterminalSymbolType >
29
30 template < class SymbolType >
32
33 template < class SymbolType >
35
36
37public:
38 template < class T, class TerminalSymbolType = typename grammar::TerminalSymbolTypeOfGrammar < T >, class NonterminalSymbolType = typename grammar::NonterminalSymbolTypeOfGrammar < T > >
39 static T parseCFLikeGrammar ( ext::istream & input );
40
41 template < class T, class SymbolType = typename grammar::TerminalSymbolTypeOfGrammar < T > >
42 static T parseCSLikeGrammar ( ext::istream & input );
43
44 template < class T, class SymbolType = typename grammar::TerminalSymbolTypeOfGrammar < T > >
45 static T parsePreservingCSLikeGrammar ( ext::istream & input );
46};
47
48template < class SymbolType >
49ext::set<SymbolType> GrammarFromStringParserCommon::parseSet (ext::istream& input) {
51
52 grammar::GrammarFromStringLexer::Token token = grammar::GrammarFromStringLexer::next(input);
54 throw exception::CommonException("Expected SET_BEGIN token.");
55 }
56
60 while(true) {
62 res.insert(symbol);
63
66 break;
67 }
69 throw exception::CommonException("Expected SET_END or COMMA token");
70 }
71 }
72 }
73
75 throw exception::CommonException("Expected SET_END token");
76 }
77
78 return res;
79}
80
81template < class TerminalSymbolType, class NonterminalSymbolType >
84
85 grammar::GrammarFromStringLexer::Token token = grammar::GrammarFromStringLexer::next(input);
87 throw exception::CommonException("Expected SET_BEGIN token.");
88 }
89
93 while(true) {
94 NonterminalSymbolType lhs = core::stringApi < NonterminalSymbolType >::parse ( input );
95
98 throw exception::CommonException("Expected MAPS_TO token.");
99 }
100
101 while(true) {
103
110 } else while(true) {
112
118 break;
119 }
120 }
121 }
122 result[lhs].insert(rhs);
124 break;
125 }
127 throw exception::CommonException("Expected SEPARATOR, SETEND or COMMA token");
128 }
129 }
130
132 break;
133 }
135 throw exception::CommonException("Expected SET_END or COMMA token");
136 }
137 }
138 }
139
141 throw exception::CommonException("Expected SET_END token");
142 }
143 return result;
144}
145
146template< class T, class TerminalSymbolType, class NonterminalSymbolType >
148 grammar::GrammarFromStringLexer::Token token = grammar::GrammarFromStringLexer::next(input);
150 throw exception::CommonException("Unrecognised Tuple begin token.");
151 }
152
153 ext::set < NonterminalSymbolType > nonterminals = parseSet < NonterminalSymbolType > (input);
154
157 throw exception::CommonException("Unrecognised Comma token.");
158 }
159
160 ext::set < TerminalSymbolType > terminals = parseSet < TerminalSymbolType > (input);
161
164 throw exception::CommonException("Unrecognised Comma token.");
165 }
166
167 ext::map < NonterminalSymbolType, ext::set < ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > > > rules = parseCFLikeRules < TerminalSymbolType, NonterminalSymbolType > ( input );
168
171 throw exception::CommonException("Unrecognised Comma token.");
172 }
173
174 NonterminalSymbolType initialSymbol = core::stringApi < NonterminalSymbolType >::parse ( input );
175
178 throw exception::CommonException("Unrecognised Tuple end token.");
179 }
180
181 T grammar(nonterminals, terminals, initialSymbol);
182 for(const auto& rule : rules) {
184 grammar::AddRawRule::addRawRule ( grammar, rule.first, rhs );
185 }
186 }
187 return grammar;
188}
189
190template < class SymbolType >
191ext::map<ext::vector<SymbolType>, ext::set<ext::vector<SymbolType>>> GrammarFromStringParserCommon::parseCSLikeRules (ext::istream& input) {
193
194 grammar::GrammarFromStringLexer::Token token = grammar::GrammarFromStringLexer::next(input);
196 throw exception::CommonException("Expected SET_BEGIN token.");
197 }
198
202 while(true) {
204 while(true) {
205 lhs.push_back(core::stringApi<SymbolType>::parse(input));
208 break;
209 }
211 }
212
214 throw exception::CommonException("Expected MAPS_TO token.");
215 }
216
217 while(true) {
219
223
224 rhs.push_back(core::stringApi<SymbolType>::parse(input));
227 break;
228 }
229 }
230 result[lhs].insert(rhs);
232 break;
233 }
235 throw exception::CommonException("Expected SEPARATOR, SETEND or COMMA token");
236 }
237 }
238
240 break;
241 }
243 throw exception::CommonException("Expected SET_END or COMMA token");
244 }
245 }
246 }
247
249 throw exception::CommonException("Expected SET_END token");
250 }
251 return result;
252}
253
254template < class T, class SymbolType >
256 grammar::GrammarFromStringLexer::Token token = grammar::GrammarFromStringLexer::next(input);
258 throw exception::CommonException("Unrecognised Tuple begin token.");
259 }
260
261 ext::set<SymbolType> nonterminals = parseSet < SymbolType > (input);
262
265 throw exception::CommonException("Unrecognised Comma token.");
266 }
267
268 ext::set<SymbolType> terminals = parseSet < SymbolType > (input);
269
272 throw exception::CommonException("Unrecognised Comma token.");
273 }
274
275 ext::map<ext::vector<SymbolType>, ext::set<ext::vector<SymbolType>>> rules = parseCSLikeRules < SymbolType > (input);
276
279 throw exception::CommonException("Unrecognised Comma token.");
280 }
281
282 SymbolType initialSymbol = core::stringApi<SymbolType>::parse(input);
283
286 throw exception::CommonException("Unrecognised Tuple end token.");
287 }
288
289 T grammar(nonterminals, terminals, initialSymbol);
290 for(const auto& rule : rules) {
291 for(const auto& ruleRHS : rule.second) {
292 grammar.addRule(rule.first, ruleRHS);
293 }
294 }
295 return grammar;
296}
297
298template < class SymbolType >
299ext::map<ext::tuple<ext::vector<SymbolType>, SymbolType, ext::vector<SymbolType>>, ext::set<ext::vector<SymbolType>>> GrammarFromStringParserCommon::parsePreservingCSLikeRules (ext::istream& input) {
301
302 grammar::GrammarFromStringLexer::Token token = grammar::GrammarFromStringLexer::next(input);
304 throw exception::CommonException("Expected SET_BEGIN token.");
305 }
306
310 while(true) {
313 if(token.type != grammar::GrammarFromStringLexer::TokenType::SEPARATOR) while(true) {
315 lContext.push_back(core::stringApi<SymbolType>::parse(input));
318 break;
319 }
320 }
321
323 throw exception::CommonException("Expected SEPARATOR token.");
324 }
325
327
330 throw exception::CommonException("Expected SEPARATOR token.");
331 }
332
335 if(token.type != grammar::GrammarFromStringLexer::TokenType::MAPS_TO) while(true) {
337 rContext.push_back(core::stringApi<SymbolType>::parse(input));
340 break;
341 }
342 }
343
345 throw exception::CommonException("Expected MAPS_TO token.");
346 }
347
349
350 while(true) {
352
356
357 rhs.push_back(core::stringApi<SymbolType>::parse(input));
360 break;
361 }
362 }
363 result[key].insert(rhs);
365 break;
366 }
368 throw exception::CommonException("Expected SEPARATOR, SETEND or COMMA token");
369 }
370 }
371
373 break;
374 }
376 throw exception::CommonException("Expected SET_END or COMMA token");
377 }
378 }
379 }
380
382 throw exception::CommonException("Expected SET_END token");
383 }
384 return result;
385}
386
387template < class T, class SymbolType >
389 grammar::GrammarFromStringLexer::Token token = grammar::GrammarFromStringLexer::next(input);
391 throw exception::CommonException("Unrecognised Tuple begin token.");
392 }
393
394 ext::set<SymbolType> nonterminals = parseSet < SymbolType > (input);
395
398 throw exception::CommonException("Unrecognised Comma token.");
399 }
400
401 ext::set<SymbolType> terminals = parseSet < SymbolType > (input);
402
405 throw exception::CommonException("Unrecognised Comma token.");
406 }
407
408 ext::map<ext::tuple<ext::vector<SymbolType>, SymbolType, ext::vector<SymbolType>>, ext::set<ext::vector<SymbolType>>> rules = parsePreservingCSLikeRules < SymbolType > (input);
409
412 throw exception::CommonException("Unrecognised Comma token.");
413 }
414
415 SymbolType initialSymbol = core::stringApi<SymbolType>::parse(input);
416
419 throw exception::CommonException("Unrecognised Tuple end token.");
420 }
421
422 T grammar(nonterminals, terminals, initialSymbol);
423 for(const auto& rule : rules) {
424 for(const auto& ruleRHS : rule.second) {
425 grammar.addRule(std::get<0>(rule.first), std::get<1>(rule.first), std::get<2>(rule.first), ruleRHS);
426 }
427 }
428 return grammar;
429}
430
431} /* namespace grammar */
432
Basic exception from which all other exceptions are derived.
Definition: CommonException.h:21
static void putback(ext::istream &input, const Token &token)
Definition: lexer.hpp:61
Definition: istream.h:32
Class extending the map class from the standard library. Original reason is to allow printing of the ...
Definition: map.hpp:48
Definition: set.hpp:44
Class extending the tuple class from the standard library. Original reason is to allow printing of th...
Definition: tuple.hpp:42
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
static bool addRawRule(LG< TerminalSymbolType, NonterminalSymbolType > &grammar, NonterminalSymbolType leftHandSide, ext::vector< ext::variant< TerminalSymbolType, NonterminalSymbolType > > rightHandSide)
Definition: AddRawRule.h:92
static Token next(ext::istream &input)
Definition: GrammarFromStringLexer.cpp:10
Definition: GrammarFromStringParserCommon.h:23
static T parsePreservingCSLikeGrammar(ext::istream &input)
Definition: GrammarFromStringParserCommon.h:388
static T parseCSLikeGrammar(ext::istream &input)
Definition: GrammarFromStringParserCommon.h:255
static T parseCFLikeGrammar(ext::istream &input)
Definition: GrammarFromStringParserCommon.h:147
return grammar
Definition: ToGrammarLeftRG.h:99
typename T::SymbolType SymbolType
Definition: ReachableStates.h:176
return res
Definition: MinimizeByPartitioning.h:145
for(const StateType &state :fsm.getStates()) renamingData.insert(std Rename::RenamedAutomaton< T > result(renamingData.at(fsm.getInitialState()))
Definition: Rename.h:253
constexpr auto make_tuple(Elements &&... args)
Helper of extended tuple construction. The tuple is constructed from values pack, types are deduced.
Definition: tuple.hpp:203
Definition: ToAutomaton.h:24
Definition: stringApi.hpp:26