From fa31264605b6d76a170b27933a722074c45ceda3 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Tue, 25 Sep 2018 11:39:04 +0200 Subject: [PATCH] update string parsing to changes in grammar templating --- alib2str/src/grammar/string/ContextFree/CFG.h | 22 ++++++------- alib2str/src/grammar/string/ContextFree/CNF.h | 32 ++++++++++++------- .../string/ContextFree/EpsilonFreeCFG.h | 32 ++++++++++++------- alib2str/src/grammar/string/ContextFree/GNF.h | 12 +++++-- alib2str/src/grammar/string/ContextFree/LG.h | 22 ++++++------- alib2str/src/grammar/string/Regular/LeftLG.h | 22 ++++++------- alib2str/src/grammar/string/Regular/LeftRG.h | 22 ++++++------- alib2str/src/grammar/string/Regular/RightLG.h | 22 ++++++------- alib2str/test-src/grammar/GrammarTest.cpp | 10 +++--- 9 files changed, 111 insertions(+), 85 deletions(-) diff --git a/alib2str/src/grammar/string/ContextFree/CFG.h b/alib2str/src/grammar/string/ContextFree/CFG.h index 248c56ccf6..9be8e54fe3 100644 --- a/alib2str/src/grammar/string/ContextFree/CFG.h +++ b/alib2str/src/grammar/string/ContextFree/CFG.h @@ -18,32 +18,32 @@ namespace core { -template<class SymbolType > -struct stringApi < grammar::CFG < SymbolType > > { - static grammar::CFG < SymbolType > parse ( std::istream & input ); +template < class TerminalSymbolType, class NonterminalSymbolType > +struct stringApi < grammar::CFG < TerminalSymbolType, NonterminalSymbolType > > { + static grammar::CFG < TerminalSymbolType, NonterminalSymbolType > parse ( std::istream & input ); static bool first ( std::istream & input ); - static void compose ( std::ostream & output, const grammar::CFG < SymbolType > & grammar ); + static void compose ( std::ostream & output, const grammar::CFG < TerminalSymbolType, NonterminalSymbolType > & grammar ); }; -template<class SymbolType > -grammar::CFG < SymbolType > stringApi < grammar::CFG < SymbolType > >::parse ( std::istream & input ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +grammar::CFG < TerminalSymbolType, NonterminalSymbolType > stringApi < grammar::CFG < TerminalSymbolType, NonterminalSymbolType > >::parse ( std::istream & input ) { grammar::GrammarFromStringLexer::Token token = grammar::GrammarFromStringLexer::next(input); if(token.type != grammar::GrammarFromStringLexer::TokenType::CFG) throw exception::CommonException("Unrecognised CFG token."); - return grammar::GrammarFromStringParserCommon::parseCFLikeGrammar < grammar::CFG < SymbolType > > ( input ); + return grammar::GrammarFromStringParserCommon::parseCFLikeGrammar < grammar::CFG < TerminalSymbolType, NonterminalSymbolType > > ( input ); } -template<class SymbolType > -bool stringApi < grammar::CFG < SymbolType > >::first ( std::istream & input ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +bool stringApi < grammar::CFG < TerminalSymbolType, NonterminalSymbolType > >::first ( std::istream & input ) { grammar::GrammarFromStringLexer::Token token = grammar::GrammarFromStringLexer::next ( input ); bool res = token.type == grammar::GrammarFromStringLexer::TokenType::CFG; grammar::GrammarFromStringLexer::putback ( input, token ); return res; } -template<class SymbolType > -void stringApi < grammar::CFG < SymbolType > >::compose ( std::ostream & output, const grammar::CFG < SymbolType > & grammar ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +void stringApi < grammar::CFG < TerminalSymbolType, NonterminalSymbolType > >::compose ( std::ostream & output, const grammar::CFG < TerminalSymbolType, NonterminalSymbolType > & grammar ) { output << "CFG"; grammar::GrammarToStringComposerCommon::composeCFLikeGrammar ( output, grammar ); } diff --git a/alib2str/src/grammar/string/ContextFree/CNF.h b/alib2str/src/grammar/string/ContextFree/CNF.h index 5558e89ce9..ff5ed7bb1b 100644 --- a/alib2str/src/grammar/string/ContextFree/CNF.h +++ b/alib2str/src/grammar/string/ContextFree/CNF.h @@ -16,36 +16,44 @@ #include <grammar/string/common/GrammarFromStringParserCommon.h> #include <grammar/string/common/GrammarToStringComposerCommon.h> +#include <grammar/properties/IsFITDefinition.h> +#include <grammar/simplify/MakeFITDefinition.h> + namespace core { -template<class SymbolType > -struct stringApi < grammar::CNF < SymbolType > > { - static grammar::CNF < SymbolType > parse ( std::istream & input ); +template < class TerminalSymbolType, class NonterminalSymbolType > +struct stringApi < grammar::CNF < TerminalSymbolType, NonterminalSymbolType > > { + static grammar::CNF < TerminalSymbolType, NonterminalSymbolType > parse ( std::istream & input ); static bool first ( std::istream & input ); - static void compose ( std::ostream & output, const grammar::CNF < SymbolType > & grammar ); + static void compose ( std::ostream & output, const grammar::CNF < TerminalSymbolType, NonterminalSymbolType > & grammar ); }; -template<class SymbolType > -grammar::CNF < SymbolType > stringApi < grammar::CNF < SymbolType > >::parse ( std::istream & input ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +grammar::CNF < TerminalSymbolType, NonterminalSymbolType > stringApi < grammar::CNF < TerminalSymbolType, NonterminalSymbolType > >::parse ( std::istream & input ) { grammar::GrammarFromStringLexer::Token token = grammar::GrammarFromStringLexer::next(input); if(token.type != grammar::GrammarFromStringLexer::TokenType::CNF) throw exception::CommonException("Unrecognised CNF token."); - return grammar::GrammarFromStringParserCommon::parseCFLikeGrammar < grammar::CNF < SymbolType > > ( input ); + grammar::CNF < TerminalSymbolType, NonterminalSymbolType > grammar = grammar::GrammarFromStringParserCommon::parseCFLikeGrammar < grammar::CNF < TerminalSymbolType, NonterminalSymbolType > > ( input ); + + if ( ! grammar::properties::IsFITDefinition::isFITDefinition ( grammar ) ) + throw exception::CommonException("Init on RHS when generate eps"); + + return grammar; } -template<class SymbolType > -bool stringApi < grammar::CNF < SymbolType > >::first ( std::istream & input ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +bool stringApi < grammar::CNF < TerminalSymbolType, NonterminalSymbolType > >::first ( std::istream & input ) { grammar::GrammarFromStringLexer::Token token = grammar::GrammarFromStringLexer::next ( input ); bool res = token.type == grammar::GrammarFromStringLexer::TokenType::CNF; grammar::GrammarFromStringLexer::putback ( input, token ); return res; } -template<class SymbolType > -void stringApi < grammar::CNF < SymbolType > >::compose ( std::ostream & output, const grammar::CNF < SymbolType > & grammar ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +void stringApi < grammar::CNF < TerminalSymbolType, NonterminalSymbolType > >::compose ( std::ostream & output, const grammar::CNF < TerminalSymbolType, NonterminalSymbolType > & grammar ) { output << "CNF"; - grammar::GrammarToStringComposerCommon::composeCFLikeGrammar ( output, grammar ); + grammar::GrammarToStringComposerCommon::composeCFLikeGrammar ( output, grammar::simplify::MakeFITDefinition::makeFITDefinition ( grammar ) ); } } /* namespace core */ diff --git a/alib2str/src/grammar/string/ContextFree/EpsilonFreeCFG.h b/alib2str/src/grammar/string/ContextFree/EpsilonFreeCFG.h index 77b271cffe..cddda5b88c 100644 --- a/alib2str/src/grammar/string/ContextFree/EpsilonFreeCFG.h +++ b/alib2str/src/grammar/string/ContextFree/EpsilonFreeCFG.h @@ -16,36 +16,44 @@ #include <grammar/string/common/GrammarFromStringParserCommon.h> #include <grammar/string/common/GrammarToStringComposerCommon.h> +#include <grammar/properties/IsFITDefinition.h> +#include <grammar/simplify/MakeFITDefinition.h> + namespace core { -template<class SymbolType > -struct stringApi < grammar::EpsilonFreeCFG < SymbolType > > { - static grammar::EpsilonFreeCFG < SymbolType > parse ( std::istream & input ); +template < class TerminalSymbolType, class NonterminalSymbolType > +struct stringApi < grammar::EpsilonFreeCFG < TerminalSymbolType, NonterminalSymbolType > > { + static grammar::EpsilonFreeCFG < TerminalSymbolType, NonterminalSymbolType > parse ( std::istream & input ); static bool first ( std::istream & input ); - static void compose ( std::ostream & output, const grammar::EpsilonFreeCFG < SymbolType > & grammar ); + static void compose ( std::ostream & output, const grammar::EpsilonFreeCFG < TerminalSymbolType, NonterminalSymbolType > & grammar ); }; -template<class SymbolType > -grammar::EpsilonFreeCFG < SymbolType > stringApi < grammar::EpsilonFreeCFG < SymbolType > >::parse ( std::istream & input ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +grammar::EpsilonFreeCFG < TerminalSymbolType, NonterminalSymbolType > stringApi < grammar::EpsilonFreeCFG < TerminalSymbolType, NonterminalSymbolType > >::parse ( std::istream & input ) { grammar::GrammarFromStringLexer::Token token = grammar::GrammarFromStringLexer::next(input); if(token.type != grammar::GrammarFromStringLexer::TokenType::EPSILON_FREE_CFG) throw exception::CommonException("Unrecognised EpsilonFreeCFG token."); - return grammar::GrammarFromStringParserCommon::parseCFLikeGrammar < grammar::EpsilonFreeCFG < SymbolType > > ( input ); + grammar::EpsilonFreeCFG < TerminalSymbolType, NonterminalSymbolType > grammar = grammar::GrammarFromStringParserCommon::parseCFLikeGrammar < grammar::EpsilonFreeCFG < TerminalSymbolType, NonterminalSymbolType > > ( input ); + + if ( ! grammar::properties::IsFITDefinition::isFITDefinition ( grammar ) ) + throw exception::CommonException("Init on RHS when generate eps"); + + return grammar; } -template<class SymbolType > -bool stringApi < grammar::EpsilonFreeCFG < SymbolType > >::first ( std::istream & input ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +bool stringApi < grammar::EpsilonFreeCFG < TerminalSymbolType, NonterminalSymbolType > >::first ( std::istream & input ) { grammar::GrammarFromStringLexer::Token token = grammar::GrammarFromStringLexer::next ( input ); bool res = token.type == grammar::GrammarFromStringLexer::TokenType::EPSILON_FREE_CFG; grammar::GrammarFromStringLexer::putback ( input, token ); return res; } -template<class SymbolType > -void stringApi < grammar::EpsilonFreeCFG < SymbolType > >::compose ( std::ostream & output, const grammar::EpsilonFreeCFG < SymbolType > & grammar ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +void stringApi < grammar::EpsilonFreeCFG < TerminalSymbolType, NonterminalSymbolType > >::compose ( std::ostream & output, const grammar::EpsilonFreeCFG < TerminalSymbolType, NonterminalSymbolType > & grammar ) { output << "EPSILON_FREE_CFG"; - grammar::GrammarToStringComposerCommon::composeCFLikeGrammar ( output, grammar ); + grammar::GrammarToStringComposerCommon::composeCFLikeGrammar ( output, grammar::simplify::MakeFITDefinition::makeFITDefinition ( grammar ) ); } } /* namespace core */ diff --git a/alib2str/src/grammar/string/ContextFree/GNF.h b/alib2str/src/grammar/string/ContextFree/GNF.h index 0edae27f6d..6d9a883a5f 100644 --- a/alib2str/src/grammar/string/ContextFree/GNF.h +++ b/alib2str/src/grammar/string/ContextFree/GNF.h @@ -16,6 +16,9 @@ #include <grammar/string/common/GrammarFromStringParserCommon.h> #include <grammar/string/common/GrammarToStringComposerCommon.h> +#include <grammar/properties/IsFITDefinition.h> +#include <grammar/simplify/MakeFITDefinition.h> + namespace core { template < class TerminalSymbolType, class NonterminalSymbolType > @@ -31,7 +34,12 @@ grammar::GNF < TerminalSymbolType, NonterminalSymbolType > stringApi < grammar:: if(token.type != grammar::GrammarFromStringLexer::TokenType::GNF) throw exception::CommonException("Unrecognised GNF token."); - return grammar::GrammarFromStringParserCommon::parseCFLikeGrammar < grammar::GNF < TerminalSymbolType, NonterminalSymbolType > > ( input ); + grammar::GNF < TerminalSymbolType, NonterminalSymbolType > grammar = grammar::GrammarFromStringParserCommon::parseCFLikeGrammar < grammar::GNF < TerminalSymbolType, NonterminalSymbolType > > ( input ); + + if ( ! grammar::properties::IsFITDefinition::isFITDefinition ( grammar ) ) + throw exception::CommonException("Init on RHS when generate eps"); + + return grammar; } template < class TerminalSymbolType, class NonterminalSymbolType > @@ -45,7 +53,7 @@ bool stringApi < grammar::GNF < TerminalSymbolType, NonterminalSymbolType > >::f template < class TerminalSymbolType, class NonterminalSymbolType > void stringApi < grammar::GNF < TerminalSymbolType, NonterminalSymbolType > >::compose ( std::ostream & output, const grammar::GNF < TerminalSymbolType, NonterminalSymbolType > & grammar ) { output << "GNF"; - grammar::GrammarToStringComposerCommon::composeCFLikeGrammar ( output, grammar ); + grammar::GrammarToStringComposerCommon::composeCFLikeGrammar ( output, grammar::simplify::MakeFITDefinition::makeFITDefinition ( grammar ) ); } } /* namespace core */ diff --git a/alib2str/src/grammar/string/ContextFree/LG.h b/alib2str/src/grammar/string/ContextFree/LG.h index dfe2c9cd42..acd8ab36c7 100644 --- a/alib2str/src/grammar/string/ContextFree/LG.h +++ b/alib2str/src/grammar/string/ContextFree/LG.h @@ -18,32 +18,32 @@ namespace core { -template<class SymbolType > -struct stringApi < grammar::LG < SymbolType > > { - static grammar::LG < SymbolType > parse ( std::istream & input ); +template < class TerminalSymbolType, class NonterminalSymbolType > +struct stringApi < grammar::LG < TerminalSymbolType, NonterminalSymbolType > > { + static grammar::LG < TerminalSymbolType, NonterminalSymbolType > parse ( std::istream & input ); static bool first ( std::istream & input ); - static void compose ( std::ostream & output, const grammar::LG < SymbolType > & grammar ); + static void compose ( std::ostream & output, const grammar::LG < TerminalSymbolType, NonterminalSymbolType > & grammar ); }; -template<class SymbolType > -grammar::LG < SymbolType > stringApi < grammar::LG < SymbolType > >::parse ( std::istream & input ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +grammar::LG < TerminalSymbolType, NonterminalSymbolType > stringApi < grammar::LG < TerminalSymbolType, NonterminalSymbolType > >::parse ( std::istream & input ) { grammar::GrammarFromStringLexer::Token token = grammar::GrammarFromStringLexer::next(input); if(token.type != grammar::GrammarFromStringLexer::TokenType::LG) throw exception::CommonException("Unrecognised LG token."); - return grammar::GrammarFromStringParserCommon::parseCFLikeGrammar < grammar::LG < SymbolType > > ( input ); + return grammar::GrammarFromStringParserCommon::parseCFLikeGrammar < grammar::LG < TerminalSymbolType, NonterminalSymbolType > > ( input ); } -template<class SymbolType > -bool stringApi < grammar::LG < SymbolType > >::first ( std::istream & input ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +bool stringApi < grammar::LG < TerminalSymbolType, NonterminalSymbolType > >::first ( std::istream & input ) { grammar::GrammarFromStringLexer::Token token = grammar::GrammarFromStringLexer::next ( input ); bool res = token.type == grammar::GrammarFromStringLexer::TokenType::LG; grammar::GrammarFromStringLexer::putback ( input, token ); return res; } -template<class SymbolType > -void stringApi < grammar::LG < SymbolType > >::compose ( std::ostream & output, const grammar::LG < SymbolType > & grammar ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +void stringApi < grammar::LG < TerminalSymbolType, NonterminalSymbolType > >::compose ( std::ostream & output, const grammar::LG < TerminalSymbolType, NonterminalSymbolType > & grammar ) { output << "LG"; grammar::GrammarToStringComposerCommon::composeCFLikeGrammar ( output, grammar ); } diff --git a/alib2str/src/grammar/string/Regular/LeftLG.h b/alib2str/src/grammar/string/Regular/LeftLG.h index 564a29e59e..7389ca6d9a 100644 --- a/alib2str/src/grammar/string/Regular/LeftLG.h +++ b/alib2str/src/grammar/string/Regular/LeftLG.h @@ -18,32 +18,32 @@ namespace core { -template<class SymbolType > -struct stringApi < grammar::LeftLG < SymbolType > > { - static grammar::LeftLG < SymbolType > parse ( std::istream & input ); +template < class TerminalSymbolType, class NonterminalSymbolType > +struct stringApi < grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > > { + static grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > parse ( std::istream & input ); static bool first ( std::istream & input ); - static void compose ( std::ostream & output, const grammar::LeftLG < SymbolType > & grammar ); + static void compose ( std::ostream & output, const grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > & grammar ); }; -template<class SymbolType > -grammar::LeftLG < SymbolType > stringApi < grammar::LeftLG < SymbolType > >::parse ( std::istream & input ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > stringApi < grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > >::parse ( std::istream & input ) { grammar::GrammarFromStringLexer::Token token = grammar::GrammarFromStringLexer::next(input); if(token.type != grammar::GrammarFromStringLexer::TokenType::LEFT_LG) throw exception::CommonException("Unrecognised LeftLG token."); - return grammar::GrammarFromStringParserCommon::parseCFLikeGrammar < grammar::LeftLG < SymbolType > > ( input ); + return grammar::GrammarFromStringParserCommon::parseCFLikeGrammar < grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > > ( input ); } -template<class SymbolType > -bool stringApi < grammar::LeftLG < SymbolType > >::first ( std::istream & input ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +bool stringApi < grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > >::first ( std::istream & input ) { grammar::GrammarFromStringLexer::Token token = grammar::GrammarFromStringLexer::next ( input ); bool res = token.type == grammar::GrammarFromStringLexer::TokenType::LEFT_LG; grammar::GrammarFromStringLexer::putback ( input, token ); return res; } -template<class SymbolType > -void stringApi < grammar::LeftLG < SymbolType > >::compose ( std::ostream & output, const grammar::LeftLG < SymbolType > & grammar ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +void stringApi < grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > >::compose ( std::ostream & output, const grammar::LeftLG < TerminalSymbolType, NonterminalSymbolType > & grammar ) { output << "LEFT_LG"; grammar::GrammarToStringComposerCommon::composeCFLikeGrammar ( output, grammar ); } diff --git a/alib2str/src/grammar/string/Regular/LeftRG.h b/alib2str/src/grammar/string/Regular/LeftRG.h index 796082ead7..d86a65baa7 100644 --- a/alib2str/src/grammar/string/Regular/LeftRG.h +++ b/alib2str/src/grammar/string/Regular/LeftRG.h @@ -21,20 +21,20 @@ namespace core { -template<class SymbolType > -struct stringApi < grammar::LeftRG < SymbolType > > { - static grammar::LeftRG < SymbolType > parse ( std::istream & input ); +template < class TerminalSymbolType, class NonterminalSymbolType > +struct stringApi < grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > > { + static grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > parse ( std::istream & input ); static bool first ( std::istream & input ); - static void compose ( std::ostream & output, const grammar::LeftRG < SymbolType > & grammar ); + static void compose ( std::ostream & output, const grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > & grammar ); }; -template<class SymbolType > -grammar::LeftRG < SymbolType > stringApi < grammar::LeftRG < SymbolType > >::parse ( std::istream & input ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > stringApi < grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > >::parse ( std::istream & input ) { grammar::GrammarFromStringLexer::Token token = grammar::GrammarFromStringLexer::next(input); if(token.type != grammar::GrammarFromStringLexer::TokenType::LEFT_RG) throw exception::CommonException("Unrecognised LeftRG token."); - grammar::LeftRG < SymbolType > grammar = grammar::GrammarFromStringParserCommon::parseCFLikeGrammar < grammar::LeftRG < SymbolType > > ( input ); + grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > grammar = grammar::GrammarFromStringParserCommon::parseCFLikeGrammar < grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > > ( input ); if ( ! grammar::properties::IsFITDefinition::isFITDefinition ( grammar ) ) throw exception::CommonException("Init on RHS when generate eps"); @@ -42,16 +42,16 @@ grammar::LeftRG < SymbolType > stringApi < grammar::LeftRG < SymbolType > >::par return grammar; } -template<class SymbolType > -bool stringApi < grammar::LeftRG < SymbolType > >::first ( std::istream & input ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +bool stringApi < grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > >::first ( std::istream & input ) { grammar::GrammarFromStringLexer::Token token = grammar::GrammarFromStringLexer::next ( input ); bool res = token.type == grammar::GrammarFromStringLexer::TokenType::LEFT_RG; grammar::GrammarFromStringLexer::putback ( input, token ); return res; } -template<class SymbolType > -void stringApi < grammar::LeftRG < SymbolType > >::compose ( std::ostream & output, const grammar::LeftRG < SymbolType > & grammar ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +void stringApi < grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > >::compose ( std::ostream & output, const grammar::LeftRG < TerminalSymbolType, NonterminalSymbolType > & grammar ) { output << "LEFT_RG"; grammar::GrammarToStringComposerCommon::composeCFLikeGrammar ( output, grammar::simplify::MakeFITDefinition::makeFITDefinition ( grammar ) ); } diff --git a/alib2str/src/grammar/string/Regular/RightLG.h b/alib2str/src/grammar/string/Regular/RightLG.h index 5927778c9b..fad9926235 100644 --- a/alib2str/src/grammar/string/Regular/RightLG.h +++ b/alib2str/src/grammar/string/Regular/RightLG.h @@ -18,32 +18,32 @@ namespace core { -template<class SymbolType > -struct stringApi < grammar::RightLG < SymbolType > > { - static grammar::RightLG < SymbolType > parse ( std::istream & input ); +template < class TerminalSymbolType, class NonterminalSymbolType > +struct stringApi < grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > > { + static grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > parse ( std::istream & input ); static bool first ( std::istream & input ); - static void compose ( std::ostream & output, const grammar::RightLG < SymbolType > & grammar ); + static void compose ( std::ostream & output, const grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > & grammar ); }; -template<class SymbolType > -grammar::RightLG < SymbolType > stringApi < grammar::RightLG < SymbolType > >::parse ( std::istream & input ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > stringApi < grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > >::parse ( std::istream & input ) { grammar::GrammarFromStringLexer::Token token = grammar::GrammarFromStringLexer::next(input); if(token.type != grammar::GrammarFromStringLexer::TokenType::RIGHT_LG) throw exception::CommonException("Unrecognised RightLG token."); - return grammar::GrammarFromStringParserCommon::parseCFLikeGrammar < grammar::RightLG < SymbolType > > ( input ); + return grammar::GrammarFromStringParserCommon::parseCFLikeGrammar < grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > > ( input ); } -template<class SymbolType > -bool stringApi < grammar::RightLG < SymbolType > >::first ( std::istream & input ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +bool stringApi < grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > >::first ( std::istream & input ) { grammar::GrammarFromStringLexer::Token token = grammar::GrammarFromStringLexer::next ( input ); bool res = token.type == grammar::GrammarFromStringLexer::TokenType::RIGHT_LG; grammar::GrammarFromStringLexer::putback ( input, token ); return res; } -template<class SymbolType > -void stringApi < grammar::RightLG < SymbolType > >::compose ( std::ostream & output, const grammar::RightLG < SymbolType > & grammar ) { +template < class TerminalSymbolType, class NonterminalSymbolType > +void stringApi < grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > >::compose ( std::ostream & output, const grammar::RightLG < TerminalSymbolType, NonterminalSymbolType > & grammar ) { output << "RIGHT_LG"; grammar::GrammarToStringComposerCommon::composeCFLikeGrammar ( output, grammar ); } diff --git a/alib2str/test-src/grammar/GrammarTest.cpp b/alib2str/test-src/grammar/GrammarTest.cpp index 29d4f9efb2..d546b6ebb3 100644 --- a/alib2str/test-src/grammar/GrammarTest.cpp +++ b/alib2str/test-src/grammar/GrammarTest.cpp @@ -39,19 +39,21 @@ void GrammarTest::tearDown() { void GrammarTest::stringParserTest() { { std::string input1 = "CNF (\n" - "{A, B, S},\n" + "{A, B, R, S},\n" "{a, b},\n" "{ A -> A A | a,\n" " B -> B B | b,\n" - " S -> | B S | S A},\n" + " R -> B R | R A,\n" + " S -> | B R | R A},\n" "S)\n"; grammar::Grammar grammar1 = factory::StringDataFactory::fromString (input1); std::string input2 = "CNF (\n" - "{A, B, S},\n" + "{A, B, R, S},\n" "{a, b},\n" "{ A -> A A | a,\n" " B -> B B | b,\n" - " S -> #E | B S | S A},\n" + " R -> B R | R A,\n" + " S -> #E | B R | R A},\n" "S)\n"; grammar::Grammar grammar2 = factory::StringDataFactory::fromString (input2); -- GitLab