Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
UnrestrictedGrammar.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#include "../common/GrammarFromXMLParser.h"
28#include "../common/GrammarToXMLComposer.h"
29
30namespace core {
31
32template < class SymbolType >
33struct xmlApi < grammar::UnrestrictedGrammar < SymbolType > > {
41 static std::string xmlTagName() {
42 return "UnrestrictedGrammar";
43 }
44
52 static bool first ( const ext::deque < sax::Token >::const_iterator & input ) {
54 }
55
64
72
80
87 static void composeRules ( ext::deque < sax::Token > & out, const grammar::UnrestrictedGrammar < SymbolType > & grammar );
88};
89
90template < class SymbolType >
93
94 ext::set < SymbolType > nonterminalAlphabet = grammar::GrammarFromXMLParser::parseNonterminalAlphabet < SymbolType > ( input );
95 ext::set < SymbolType > terminalAlphabet = grammar::GrammarFromXMLParser::parseTerminalAlphabet < SymbolType > ( input );
96 SymbolType initialSymbol = grammar::GrammarFromXMLParser::parseInitialSymbol < SymbolType > ( input );
97
98 grammar::UnrestrictedGrammar < SymbolType > grammar ( std::move ( initialSymbol ) );
99
100 grammar.setNonterminalAlphabet ( std::move ( nonterminalAlphabet ) );
101 grammar.setTerminalAlphabet ( std::move ( terminalAlphabet ) );
102
104
106 return grammar;
107}
108
109template < class SymbolType >
111 ext::vector < SymbolType > lhs = grammar::GrammarFromXMLParser::parseRuleLHS < SymbolType > ( input );
112 ext::vector < SymbolType > rhs = grammar::GrammarFromXMLParser::parseRuleRHS < SymbolType > ( input );
113
114 grammar.addRule ( std::move ( lhs ), std::move ( rhs ) );
115}
116
117template < class SymbolType >
119 out.emplace_back ( xmlTagName ( ), sax::Token::TokenType::START_ELEMENT );
120
121 grammar::GrammarToXMLComposer::composeNonterminalAlphabet ( out, grammar.getNonterminalAlphabet ( ) );
124 composeRules ( out, grammar );
125
126 out.emplace_back ( xmlTagName ( ), sax::Token::TokenType::END_ELEMENT );
127}
128
129template < class SymbolType >
131 out.emplace_back ( "rules", sax::Token::TokenType::START_ELEMENT );
132
133 for ( const auto & rule : grammar.getRules ( ) )
134
135 for ( const auto & rhs : rule.second ) {
136 out.emplace_back ( "rule", sax::Token::TokenType::START_ELEMENT );
137
140
141 out.emplace_back ( "rule", sax::Token::TokenType::END_ELEMENT );
142 }
143
144 out.emplace_back ( "rules", sax::Token::TokenType::END_ELEMENT );
145}
146
147} /* namespace core */
148
Class extending the deque class from the standard library. Original reason is to allow printing of th...
Definition: deque.hpp:44
Definition: set.hpp:44
Class extending the vector class from the standard library. Original reason is to allow printing of t...
Definition: vector.hpp:45
static void parseRules(ext::deque< sax::Token >::iterator &input, T &grammar)
Definition: GrammarFromXMLParser.h:157
static void composeNonterminalAlphabet(ext::deque< sax::Token > &out, const ext::set< SymbolType > &symbols)
Definition: GrammarToXMLComposer.h:66
static void composeRuleLHS(ext::deque< sax::Token > &out, const ext::vector< SymbolType > &symbols)
Definition: GrammarToXMLComposer.h:103
static void composeTerminalAlphabet(ext::deque< sax::Token > &out, const ext::set< SymbolType > &symbols)
Definition: GrammarToXMLComposer.h:75
static void composeInitialSymbol(ext::deque< sax::Token > &out, const SymbolType &symbol)
Definition: GrammarToXMLComposer.h:84
static void composeRuleRHS(ext::deque< sax::Token > &out, const ext::vector< SymbolType > &symbols)
Definition: GrammarToXMLComposer.h:134
Unrestricted grammar. Type 0 in Chomsky hierarchy. Generates recursively enumerable languages.
Definition: UnrestrictedGrammar.h:64
static void popToken(ext::deque< Token >::iterator &input, Token::TokenType type, const std::string &data)
Definition: FromXMLParserHelper.cpp:39
static bool isToken(ext::deque< Token >::const_iterator input, Token::TokenType type, const std::string &data)
Definition: FromXMLParserHelper.cpp:29
return grammar
Definition: ToGrammarLeftRG.h:99
typename T::SymbolType SymbolType
Definition: ReachableStates.h:176
Definition: normalize.hpp:10
Definition: ToAutomaton.h:24
static std::string xmlTagName()
The XML tag name of class.
Definition: UnrestrictedGrammar.h:41
static bool first(const ext::deque< sax::Token >::const_iterator &input)
Tests whether the token stream starts with this type.
Definition: UnrestrictedGrammar.h:52
Definition: xmlApi.hpp:27