Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
RightRG.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
30#include <grammar/AddRawRule.h>
31
33
34namespace core {
35
36template < class TerminalSymbolType, class NonterminalSymbolType >
37struct xmlApi < grammar::RightRG < TerminalSymbolType, NonterminalSymbolType > > {
45 static std::string xmlTagName() {
46 return "RightRG";
47 }
48
56 static bool first ( const ext::deque < sax::Token >::const_iterator & input ) {
58 }
59
68
76
84
92};
93
94template < class TerminalSymbolType, class NonterminalSymbolType >
97
98 ext::set < NonterminalSymbolType > nonterminalAlphabet = grammar::GrammarFromXMLParser::parseNonterminalAlphabet < NonterminalSymbolType > ( input );
99 ext::set < TerminalSymbolType > terminalAlphabet = grammar::GrammarFromXMLParser::parseTerminalAlphabet < TerminalSymbolType > ( input );
100 NonterminalSymbolType initialSymbol = grammar::GrammarFromXMLParser::parseInitialSymbol < NonterminalSymbolType > ( input );
101
102 grammar::RightRG < TerminalSymbolType, NonterminalSymbolType > grammar ( std::move ( nonterminalAlphabet ), std::move ( terminalAlphabet ), std::move ( initialSymbol ) );
103
105
106 bool generatesEpsilon = grammar::GrammarFromXMLParser::parseGeneratesEpsilon ( input );
107
108 grammar.setGeneratesEpsilon ( generatesEpsilon );
109
111 return grammar;
112}
113
114template < class TerminalSymbolType, class NonterminalSymbolType >
116 NonterminalSymbolType lhs = grammar::GrammarFromXMLParser::parseRuleSingleSymbolLHS < NonterminalSymbolType > ( input );
117 ext::vector < ext::variant < TerminalSymbolType, NonterminalSymbolType > > rhs = grammar::GrammarFromXMLParser::parseRuleRHS < ext::variant < TerminalSymbolType, NonterminalSymbolType > > ( input );
118
119 grammar::AddRawRule::addRawRule ( grammar, std::move ( lhs ), std::move ( rhs ) );
120}
121
122template < class TerminalSymbolType, class NonterminalSymbolType >
124 out.emplace_back ( xmlTagName ( ), sax::Token::TokenType::START_ELEMENT );
125
126 grammar::GrammarToXMLComposer::composeNonterminalAlphabet ( out, grammar.getNonterminalAlphabet ( ) );
129 composeRules ( out, grammar );
131
132 out.emplace_back ( xmlTagName ( ), sax::Token::TokenType::END_ELEMENT );
133}
134
135template < class TerminalSymbolType, class NonterminalSymbolType >
137 out.emplace_back ( "rules", sax::Token::TokenType::START_ELEMENT );
138
139 for ( const auto & rule : grammar.getRules ( ) )
140
141 for ( const auto & rhs : rule.second ) {
142 out.emplace_back ( "rule", sax::Token::TokenType::START_ELEMENT );
143
146
147 out.emplace_back ( "rule", sax::Token::TokenType::END_ELEMENT );
148 }
149
150 out.emplace_back ( "rules", sax::Token::TokenType::END_ELEMENT );
151}
152
153} /* namespace core */
154
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 bool addRawRule(LG< TerminalSymbolType, NonterminalSymbolType > &grammar, NonterminalSymbolType leftHandSide, ext::vector< ext::variant< TerminalSymbolType, NonterminalSymbolType > > rightHandSide)
Definition: AddRawRule.h:92
static void parseRules(ext::deque< sax::Token >::iterator &input, T &grammar)
Definition: GrammarFromXMLParser.h:157
static bool parseGeneratesEpsilon(ext::deque< sax::Token >::iterator &input)
Definition: GrammarFromXMLParser.h:31
static void composeNonterminalAlphabet(ext::deque< sax::Token > &out, const ext::set< SymbolType > &symbols)
Definition: GrammarToXMLComposer.h:66
static void composeTerminalAlphabet(ext::deque< sax::Token > &out, const ext::set< SymbolType > &symbols)
Definition: GrammarToXMLComposer.h:75
static void composeRuleSingleSymbolLHS(ext::deque< sax::Token > &out, const SymbolType &symbol)
Definition: GrammarToXMLComposer.h:127
static void composeInitialSymbol(ext::deque< sax::Token > &out, const SymbolType &symbol)
Definition: GrammarToXMLComposer.h:84
static void composeRuleOneOrTwoSymbolsRHS(ext::deque< sax::Token > &out, const ext::variant< T, ext::pair< R, S > > &symbols)
Definition: GrammarToXMLComposer.h:146
static void composeGeneratesEpsilon(ext::deque< sax::Token > &out, bool generatesEpsilon)
Definition: GrammarToXMLComposer.h:31
Right regular grammar in Chomsky hierarchy or type 3 in Chomsky hierarchy. Generates regular language...
Definition: RightRG.h:70
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
Definition: normalize.hpp:10
Definition: ToAutomaton.h:24
static std::string xmlTagName()
The XML tag name of class.
Definition: RightRG.h:45
static bool first(const ext::deque< sax::Token >::const_iterator &input)
Tests whether the token stream starts with this type.
Definition: RightRG.h:56
Definition: xmlApi.hpp:27