From cef22bb2f89a4cae7aa7bd6473e710c28418bd00 Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Thu, 22 Dec 2016 16:22:24 +0100 Subject: [PATCH] Default types in most of grammar experiment algos --- .../grammar/parsing/CornerSubstitution.cpp | 8 +- .../src/grammar/parsing/CornerSubstitution.h | 2 +- .../parsing/DeterministicLL1Grammar.cpp | 26 +-- .../parsing/DeterministicLL1ParseTable.cpp | 8 +- .../parsing/DeterministicLL1ParseTable.h | 2 +- .../grammar/parsing/ExtractRightContext.cpp | 12 +- .../src/grammar/parsing/ExtractRightContext.h | 2 +- .../src/grammar/parsing/First.cpp | 38 ++-- .../src/grammar/parsing/First.h | 14 +- .../src/grammar/parsing/Follow.cpp | 28 +-- .../src/grammar/parsing/Follow.h | 12 +- .../parsing/HandleFirstFirstConflict.cpp | 4 +- .../parsing/HandleFirstFirstConflict.h | 2 +- .../parsing/HandleFirstFollowConflict.cpp | 22 +-- .../parsing/HandleFirstFollowConflict.h | 2 +- .../src/grammar/parsing/LL1ParseTable.cpp | 40 ++-- .../src/grammar/parsing/LL1ParseTable.h | 6 +- .../src/grammar/parsing/LR0Parser.cpp | 32 +-- .../src/grammar/parsing/LR0Parser.h | 2 +- .../src/grammar/parsing/LRParser.cpp | 14 +- .../src/grammar/parsing/LRParser.h | 4 +- .../src/grammar/parsing/LeftFactorize.cpp | 14 +- .../src/grammar/parsing/LeftFactorize.h | 2 +- .../src/grammar/parsing/SLR1ParseTable.cpp | 28 +-- .../src/grammar/parsing/common/Substitute.cpp | 6 +- .../src/grammar/parsing/common/Substitute.h | 2 +- .../grammar/parsing/CornerSubstitution.cpp | 82 ++++---- .../grammar/parsing/ExtractRightContext.cpp | 72 +++---- .../test-src/grammar/parsing/FirstTest.cpp | 182 +++++++++--------- .../test-src/grammar/parsing/FollowTest.cpp | 120 ++++++------ .../parsing/HandleFirstFirstConflict.cpp | 120 ++++++------ .../grammar/parsing/LL1ParseTable.cpp | 48 ++--- .../test-src/grammar/parsing/LR0Parser.cpp | 6 +- .../test-src/grammar/parsing/LR0Parser.h | 18 +- .../test-src/grammar/parsing/LRParser.cpp | 10 +- .../test-src/grammar/parsing/LRParser.h | 18 +- .../grammar/parsing/LeftFactorize.cpp | 114 +++++------ .../grammar/parsing/SLR1ParseTable.cpp | 24 +-- .../test-src/grammar/parsing/SLR1ParseTable.h | 18 +- .../src/grammar/parsing/LRParserTypes.h | 10 +- 40 files changed, 587 insertions(+), 587 deletions(-) diff --git a/alib2algo_experimental/src/grammar/parsing/CornerSubstitution.cpp b/alib2algo_experimental/src/grammar/parsing/CornerSubstitution.cpp index eaefdb8c66..989d64d665 100644 --- a/alib2algo_experimental/src/grammar/parsing/CornerSubstitution.cpp +++ b/alib2algo_experimental/src/grammar/parsing/CornerSubstitution.cpp @@ -15,16 +15,16 @@ namespace grammar { namespace parsing { -void CornerSubstitution::cornerSubstitution ( grammar::CFG < > & grammar, const alphabet::Symbol & terminal, const alphabet::Symbol & nonterminal ) { +void CornerSubstitution::cornerSubstitution ( grammar::CFG < > & grammar, const DefaultSymbolType & terminal, const DefaultSymbolType & nonterminal ) { grammar::CFG < > res ( grammar.getInitialSymbol ( ) ); res.setNonterminalAlphabet ( grammar.getNonterminalAlphabet ( ) ); res.setTerminalAlphabet ( grammar.getTerminalAlphabet ( ) ); - for ( const std::pair < const alphabet::Symbol, std::set < std::vector < alphabet::Symbol > > > & rule : grammar.getRules ( ) ) { - const alphabet::Symbol & lhs = rule.first; + for ( const std::pair < const DefaultSymbolType, std::set < std::vector < DefaultSymbolType > > > & rule : grammar.getRules ( ) ) { + const DefaultSymbolType & lhs = rule.first; - for ( const std::vector < alphabet::Symbol > & rhs : rule.second ) { + for ( const std::vector < DefaultSymbolType > & rhs : rule.second ) { if ( ( lhs == nonterminal ) && ( rhs.size ( ) > 0 ) && ( grammar.getNonterminalAlphabet ( ).count ( rhs[0] ) ) && First::first ( grammar, rhs ).count ( terminal ) ) Substitute::substitute ( grammar, res, lhs, rhs, rhs.begin ( ) ); else diff --git a/alib2algo_experimental/src/grammar/parsing/CornerSubstitution.h b/alib2algo_experimental/src/grammar/parsing/CornerSubstitution.h index beaf08a20b..8d0edd36b4 100644 --- a/alib2algo_experimental/src/grammar/parsing/CornerSubstitution.h +++ b/alib2algo_experimental/src/grammar/parsing/CornerSubstitution.h @@ -17,7 +17,7 @@ namespace parsing { class CornerSubstitution { public: - static void cornerSubstitution ( grammar::CFG < > & grammar, const alphabet::Symbol & terminal, const alphabet::Symbol & nonterminal ); + static void cornerSubstitution ( grammar::CFG < > & grammar, const DefaultSymbolType & terminal, const DefaultSymbolType & nonterminal ); }; diff --git a/alib2algo_experimental/src/grammar/parsing/DeterministicLL1Grammar.cpp b/alib2algo_experimental/src/grammar/parsing/DeterministicLL1Grammar.cpp index 0f40c34fb0..686e3de703 100644 --- a/alib2algo_experimental/src/grammar/parsing/DeterministicLL1Grammar.cpp +++ b/alib2algo_experimental/src/grammar/parsing/DeterministicLL1Grammar.cpp @@ -22,7 +22,7 @@ namespace parsing { grammar::CFG < > DeterministicLL1Grammar::convert ( const grammar::CFG < > & param ) { - if ( std::any_of ( param.getNonterminalAlphabet ( ).begin ( ), param.getNonterminalAlphabet ( ).end ( ), [&] ( const alphabet::Symbol & nonterminal ) { + if ( std::any_of ( param.getNonterminalAlphabet ( ).begin ( ), param.getNonterminalAlphabet ( ).end ( ), [&] ( const DefaultSymbolType & nonterminal ) { return grammar::properties::RecursiveNonterminal::isNonterminalRecursive ( param, nonterminal ); } ) ) throw exception::CommonException ( "Grammar contains left recursion" ); @@ -30,23 +30,23 @@ grammar::CFG < > DeterministicLL1Grammar::convert ( const grammar::CFG < > & par grammar::CFG < > grammar = param; while ( true ) { - std::map < std::pair < std::variant < alphabet::Symbol, string::Epsilon < > >, alphabet::Symbol >, std::set < std::vector < alphabet::Symbol > > > parseTable = LL1ParseTable::parseTable ( grammar ); + std::map < std::pair < std::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, std::set < std::vector < DefaultSymbolType > > > parseTable = LL1ParseTable::parseTable ( grammar ); bool deterministic = true; - for ( const std::pair < const std::pair < std::variant < alphabet::Symbol, string::Epsilon < > >, alphabet::Symbol >, std::set < std::vector < alphabet::Symbol > > > & elem : parseTable ) + for ( const std::pair < const std::pair < std::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, std::set < std::vector < DefaultSymbolType > > > & elem : parseTable ) if ( elem.second.size ( ) > 1 ) if ( elem.first.first.is < string::Epsilon < > > ( ) ) throw exception::CommonException ( "Cant handle conflict in epsilon" ); - for ( const std::pair < const std::pair < std::variant < alphabet::Symbol, string::Epsilon < > >, alphabet::Symbol >, std::set < std::vector < alphabet::Symbol > > > & elem : parseTable ) { + for ( const std::pair < const std::pair < std::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, std::set < std::vector < DefaultSymbolType > > > & elem : parseTable ) { if ( elem.first.first.is < string::Epsilon < > > ( ) ) continue; - const alphabet::Symbol & terminal = elem.first.first.get < alphabet::Symbol > ( ); - const alphabet::Symbol & nonterminal = elem.first.second; - const std::set < std::vector < alphabet::Symbol > > & rhsds = elem.second; + const DefaultSymbolType & terminal = elem.first.first.get < DefaultSymbolType > ( ); + const DefaultSymbolType & nonterminal = elem.first.second; + const std::set < std::vector < DefaultSymbolType > > & rhsds = elem.second; - if ( ( elem.second.size ( ) > 1 ) && std::all_of ( rhsds.begin ( ), rhsds.end ( ), [] ( const std::vector < alphabet::Symbol > & rhs ) { + if ( ( elem.second.size ( ) > 1 ) && std::all_of ( rhsds.begin ( ), rhsds.end ( ), [] ( const std::vector < DefaultSymbolType > & rhs ) { return rhs.size ( ) > 0; } ) ) { HandleFirstFirstConflict::handleFirstFirstConflict ( grammar, terminal, nonterminal, rhsds ); @@ -57,14 +57,14 @@ grammar::CFG < > DeterministicLL1Grammar::convert ( const grammar::CFG < > & par if ( !deterministic ) continue; - for ( const std::pair < const std::pair < std::variant < alphabet::Symbol, string::Epsilon < > >, alphabet::Symbol >, std::set < std::vector < alphabet::Symbol > > > & elem : parseTable ) { + for ( const std::pair < const std::pair < std::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, std::set < std::vector < DefaultSymbolType > > > & elem : parseTable ) { if ( elem.first.first.is < string::Epsilon < > > ( ) ) continue; - const alphabet::Symbol & terminal = elem.first.first.get < alphabet::Symbol > ( ); - const alphabet::Symbol & nonterminal = elem.first.second; - const std::set < std::vector < alphabet::Symbol > > & rhsds = elem.second; + const DefaultSymbolType & terminal = elem.first.first.get < DefaultSymbolType > ( ); + const DefaultSymbolType & nonterminal = elem.first.second; + const std::set < std::vector < DefaultSymbolType > > & rhsds = elem.second; - if ( ( elem.second.size ( ) > 1 ) && std::any_of ( rhsds.begin ( ), rhsds.end ( ), [] ( const std::vector < alphabet::Symbol > & rhs ) { + if ( ( elem.second.size ( ) > 1 ) && std::any_of ( rhsds.begin ( ), rhsds.end ( ), [] ( const std::vector < DefaultSymbolType > & rhs ) { return rhs.size ( ) == 0; } ) ) { HandleFirstFollowConflict::handleFirstFollowConflict ( grammar, terminal, nonterminal, rhsds ); diff --git a/alib2algo_experimental/src/grammar/parsing/DeterministicLL1ParseTable.cpp b/alib2algo_experimental/src/grammar/parsing/DeterministicLL1ParseTable.cpp index 681fd9a60f..63f6e1bcc1 100644 --- a/alib2algo_experimental/src/grammar/parsing/DeterministicLL1ParseTable.cpp +++ b/alib2algo_experimental/src/grammar/parsing/DeterministicLL1ParseTable.cpp @@ -13,15 +13,15 @@ namespace grammar { namespace parsing { -std::map < std::pair < std::variant < alphabet::Symbol, string::Epsilon < > >, alphabet::Symbol >, std::vector < alphabet::Symbol > > DeterministicLL1ParseTable::parseTable ( const std::map < std::pair < std::variant < alphabet::Symbol, string::Epsilon < > >, alphabet::Symbol >, std::set < std::vector < alphabet::Symbol > > > & parseTable ) { +std::map < std::pair < std::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, std::vector < DefaultSymbolType > > DeterministicLL1ParseTable::parseTable ( const std::map < std::pair < std::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, std::set < std::vector < DefaultSymbolType > > > & parseTable ) { - for ( const std::pair < const std::pair < std::variant < alphabet::Symbol, string::Epsilon < > >, alphabet::Symbol >, std::set < std::vector < alphabet::Symbol > > > & elem : parseTable ) + for ( const std::pair < const std::pair < std::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, std::set < std::vector < DefaultSymbolType > > > & elem : parseTable ) if ( elem.second.size ( ) > 1 ) throw exception::CommonException ( "Cant handle conflict in epsilon" ); - std::map < std::pair < std::variant < alphabet::Symbol, string::Epsilon < > >, alphabet::Symbol >, std::vector < alphabet::Symbol > > res; + std::map < std::pair < std::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, std::vector < DefaultSymbolType > > res; - for ( const std::pair < const std::pair < std::variant < alphabet::Symbol, string::Epsilon < > >, alphabet::Symbol >, std::set < std::vector < alphabet::Symbol > > > & elem : parseTable ) + for ( const std::pair < const std::pair < std::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, std::set < std::vector < DefaultSymbolType > > > & elem : parseTable ) if ( elem.second.size ( ) == 1 ) res.insert ( std::make_pair ( elem.first, * elem.second.begin ( ) ) ); diff --git a/alib2algo_experimental/src/grammar/parsing/DeterministicLL1ParseTable.h b/alib2algo_experimental/src/grammar/parsing/DeterministicLL1ParseTable.h index b5bae8714e..9597e5a504 100644 --- a/alib2algo_experimental/src/grammar/parsing/DeterministicLL1ParseTable.h +++ b/alib2algo_experimental/src/grammar/parsing/DeterministicLL1ParseTable.h @@ -21,7 +21,7 @@ namespace parsing { class DeterministicLL1ParseTable { public: - static std::map < std::pair < std::variant < alphabet::Symbol, string::Epsilon < > >, alphabet::Symbol >, std::vector < alphabet::Symbol > > parseTable ( const std::map < std::pair < std::variant < alphabet::Symbol, string::Epsilon < > >, alphabet::Symbol >, std::set < std::vector < alphabet::Symbol > > > & parseTable ); + static std::map < std::pair < std::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, std::vector < DefaultSymbolType > > parseTable ( const std::map < std::pair < std::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, std::set < std::vector < DefaultSymbolType > > > & parseTable ); }; } /* namespace parsing */ diff --git a/alib2algo_experimental/src/grammar/parsing/ExtractRightContext.cpp b/alib2algo_experimental/src/grammar/parsing/ExtractRightContext.cpp index ad9b200cf4..9ea9912804 100644 --- a/alib2algo_experimental/src/grammar/parsing/ExtractRightContext.cpp +++ b/alib2algo_experimental/src/grammar/parsing/ExtractRightContext.cpp @@ -14,20 +14,20 @@ namespace grammar { namespace parsing { -void ExtractRightContext::extractRightContext ( grammar::CFG < > & grammar, const alphabet::Symbol & terminal, const std::set < alphabet::Symbol > & nonterminals ) { +void ExtractRightContext::extractRightContext ( grammar::CFG < > & grammar, const DefaultSymbolType & terminal, const std::set < DefaultSymbolType > & nonterminals ) { grammar::CFG < > res ( grammar.getInitialSymbol ( ) ); res.setNonterminalAlphabet ( grammar.getNonterminalAlphabet ( ) ); res.setTerminalAlphabet ( grammar.getTerminalAlphabet ( ) ); - for ( const std::pair < const alphabet::Symbol, std::set < std::vector < alphabet::Symbol > > > & rule : grammar.getRules ( ) ) { - const alphabet::Symbol & lhs = rule.first; + for ( const std::pair < const DefaultSymbolType, std::set < std::vector < DefaultSymbolType > > > & rule : grammar.getRules ( ) ) { + const DefaultSymbolType & lhs = rule.first; - for ( const std::vector < alphabet::Symbol > & rhs : rule.second ) { + for ( const std::vector < DefaultSymbolType > & rhs : rule.second ) { bool substitued = false; if(rhs.size() > 0) - for ( std::vector < alphabet::Symbol >::const_iterator iter = rhs.begin ( ); iter + 1 != rhs.end ( ); ++iter ) - if ( nonterminals.count ( * iter ) && grammar.getNonterminalAlphabet ( ).count ( * ( iter + 1 ) ) && First::first ( grammar, std::vector < alphabet::Symbol > ( iter + 1, rhs.end ( ) ) ).count ( terminal ) ) { + for ( std::vector < DefaultSymbolType >::const_iterator iter = rhs.begin ( ); iter + 1 != rhs.end ( ); ++iter ) + if ( nonterminals.count ( * iter ) && grammar.getNonterminalAlphabet ( ).count ( * ( iter + 1 ) ) && First::first ( grammar, std::vector < DefaultSymbolType > ( iter + 1, rhs.end ( ) ) ).count ( terminal ) ) { Substitute::substitute ( grammar, res, lhs, rhs, iter + 1 ); substitued = true; break; diff --git a/alib2algo_experimental/src/grammar/parsing/ExtractRightContext.h b/alib2algo_experimental/src/grammar/parsing/ExtractRightContext.h index b40accc853..8fd1b54c2d 100644 --- a/alib2algo_experimental/src/grammar/parsing/ExtractRightContext.h +++ b/alib2algo_experimental/src/grammar/parsing/ExtractRightContext.h @@ -18,7 +18,7 @@ namespace parsing { class ExtractRightContext { public: - static void extractRightContext ( grammar::CFG < > & grammar, const alphabet::Symbol & terminal, const std::set < alphabet::Symbol > & nonterminals ); + static void extractRightContext ( grammar::CFG < > & grammar, const DefaultSymbolType & terminal, const std::set < DefaultSymbolType > & nonterminals ); }; diff --git a/alib2algo_experimental/src/grammar/parsing/First.cpp b/alib2algo_experimental/src/grammar/parsing/First.cpp index 21c36bb9f1..9e736b686e 100644 --- a/alib2algo_experimental/src/grammar/parsing/First.cpp +++ b/alib2algo_experimental/src/grammar/parsing/First.cpp @@ -22,7 +22,7 @@ namespace grammar { namespace parsing { -std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > First::first ( const std::set < alphabet::Symbol > & terminals, const std::set < alphabet::Symbol > & nonterminals, const std::map < alphabet::Symbol, std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > > & firstOfNonterminal, const std::vector < alphabet::Symbol > & rhs ) { +std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > First::first ( const std::set < DefaultSymbolType > & terminals, const std::set < DefaultSymbolType > & nonterminals, const std::map < DefaultSymbolType, std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > > & firstOfNonterminal, const std::vector < DefaultSymbolType > & rhs ) { // 1. FIRST(\varepsilon) = { \varepsilon } if ( rhs.size ( ) == 0 ) { return { string::Epsilon < >::EPSILON }; @@ -40,10 +40,10 @@ std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > First::first // 5. FIRST(A \alpha) = (first(A) - \varepsilon) \cup FIRST(\alpha) if A \in N and \varepsilon \in first(A) else if ( nonterminals.count ( rhs[0] ) && firstOfNonterminal.find ( rhs[0] )->second.count ( string::Epsilon < >::EPSILON ) ) { - std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > res = firstOfNonterminal.find ( rhs[0] )->second; + std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > res = firstOfNonterminal.find ( rhs[0] )->second; res.erase ( string::Epsilon < >::EPSILON ); - std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > next = first ( terminals, nonterminals, firstOfNonterminal, std::vector < alphabet::Symbol > ( rhs.begin ( ) + 1, rhs.end ( ) ) ); + std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > next = first ( terminals, nonterminals, firstOfNonterminal, std::vector < DefaultSymbolType > ( rhs.begin ( ) + 1, rhs.end ( ) ) ); res.insert ( next.begin ( ), next.end ( ) ); return res; } else { @@ -51,7 +51,7 @@ std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > First::first } } -std::map < alphabet::Symbol, std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > > First::first ( const std::set < alphabet::Symbol > & terminals, const std::set < alphabet::Symbol > & nonterminals, const std::map < alphabet::Symbol, std::set < std::vector < alphabet::Symbol > > > & rules ) { +std::map < DefaultSymbolType, std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > > First::first ( const std::set < DefaultSymbolType > & terminals, const std::set < DefaultSymbolType > & nonterminals, const std::map < DefaultSymbolType, std::set < std::vector < DefaultSymbolType > > > & rules ) { /* * * 1. foreach A \in N: first(A) = \emptyset @@ -60,17 +60,17 @@ std::map < alphabet::Symbol, std::set < std::variant < alphabet::Symbol, string: * 3. repeat step 2 if at least one set first(A) has changed * */ - std::map < alphabet::Symbol, std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > > firstOfNonterminal1; + std::map < DefaultSymbolType, std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > > firstOfNonterminal1; - for ( const alphabet::Symbol & nonterminal : nonterminals ) + for ( const DefaultSymbolType & nonterminal : nonterminals ) firstOfNonterminal1[nonterminal]; - std::map < alphabet::Symbol, std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > > firstOfNonterminal2 = firstOfNonterminal1; + std::map < DefaultSymbolType, std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > > firstOfNonterminal2 = firstOfNonterminal1; do { - for ( const std::pair < const alphabet::Symbol, std::set < std::vector < alphabet::Symbol > > > & rule : rules ) - for ( const std::vector < alphabet::Symbol > & rhs : rule.second ) { - std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > newFirst = first ( terminals, nonterminals, firstOfNonterminal1, rhs ); + for ( const std::pair < const DefaultSymbolType, std::set < std::vector < DefaultSymbolType > > > & rule : rules ) + for ( const std::vector < DefaultSymbolType > & rhs : rule.second ) { + std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > newFirst = first ( terminals, nonterminals, firstOfNonterminal1, rhs ); firstOfNonterminal2[rule.first].insert ( newFirst.begin ( ), newFirst.end ( ) ); } @@ -85,21 +85,21 @@ std::map < alphabet::Symbol, std::set < std::variant < alphabet::Symbol, string: } template < class T > -std::map < std::vector < alphabet::Symbol >, std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > > First::first ( const T & grammar ) { - std::map < alphabet::Symbol, std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > > firstNt = first ( grammar.getTerminalAlphabet ( ), grammar.getNonterminalAlphabet ( ), grammar.getRawRules ( ) ); +std::map < std::vector < DefaultSymbolType >, std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > > First::first ( const T & grammar ) { + std::map < DefaultSymbolType, std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > > firstNt = first ( grammar.getTerminalAlphabet ( ), grammar.getNonterminalAlphabet ( ), grammar.getRawRules ( ) ); - std::map < std::vector < alphabet::Symbol >, std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > > res; + std::map < std::vector < DefaultSymbolType >, std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > > res; - for ( const std::pair < const alphabet::Symbol, std::set < std::vector < alphabet::Symbol > > > & rule : grammar.getRawRules ( ) ) - for ( const std::vector < alphabet::Symbol > & rhs : rule.second ) + for ( const std::pair < const DefaultSymbolType, std::set < std::vector < DefaultSymbolType > > > & rule : grammar.getRawRules ( ) ) + for ( const std::vector < DefaultSymbolType > & rhs : rule.second ) res.insert ( make_pair ( rhs, first ( grammar.getTerminalAlphabet ( ), grammar.getNonterminalAlphabet ( ), firstNt, rhs ) ) ); return res; } template < class T > -std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > First::first ( const T & grammar, const std::vector < alphabet::Symbol > & rhs ) { - std::map < alphabet::Symbol, std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > > firstNt = first ( grammar.getTerminalAlphabet ( ), grammar.getNonterminalAlphabet ( ), grammar.getRawRules ( ) ); +std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > First::first ( const T & grammar, const std::vector < DefaultSymbolType > & rhs ) { + std::map < DefaultSymbolType, std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > > firstNt = first ( grammar.getTerminalAlphabet ( ), grammar.getNonterminalAlphabet ( ), grammar.getRawRules ( ) ); return first ( grammar.getTerminalAlphabet ( ), grammar.getNonterminalAlphabet ( ), firstNt, rhs ); } @@ -114,7 +114,7 @@ auto FirstLeftRG = FirstBase1::RegistratorWrapper < FirstResult1, grammar::LeftR auto FirstRightLG = FirstBase1::RegistratorWrapper < FirstResult1, grammar::RightLG < > > ( First::first ); auto FirstRightRG = FirstBase1::RegistratorWrapper < FirstResult1, grammar::RightRG < > > ( First::first ); -std::map < std::vector < alphabet::Symbol >, std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > > First::first ( const grammar::Grammar & grammar ) { +std::map < std::vector < DefaultSymbolType >, std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > > First::first ( const grammar::Grammar & grammar ) { return FirstBase1::dispatch ( grammar.getData ( ) ); } @@ -128,7 +128,7 @@ auto FirstLeftRG2 = FirstBase2::RegistratorWrapper < FirstResult2, grammar::Left auto FirstRightLG2 = FirstBase2::RegistratorWrapper < FirstResult2, grammar::RightLG < > > ( First::first ); auto FirstRightRG2 = FirstBase2::RegistratorWrapper < FirstResult2, grammar::RightRG < > > ( First::first ); -std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > First::first ( const grammar::Grammar & grammar, const std::vector < alphabet::Symbol > & rhs ) { +std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > First::first ( const grammar::Grammar & grammar, const std::vector < DefaultSymbolType > & rhs ) { return FirstBase2::dispatch ( grammar.getData ( ), rhs ); } diff --git a/alib2algo_experimental/src/grammar/parsing/First.h b/alib2algo_experimental/src/grammar/parsing/First.h index 5aa27880fe..b2809205c6 100644 --- a/alib2algo_experimental/src/grammar/parsing/First.h +++ b/alib2algo_experimental/src/grammar/parsing/First.h @@ -22,27 +22,27 @@ namespace parsing { class First; -typedef std::map < std::vector < alphabet::Symbol >, std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > > FirstResult1; +typedef std::map < std::vector < DefaultSymbolType >, std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > > FirstResult1; typedef std::SingleDispatch < First, FirstResult1, const grammar::GrammarBase & > FirstBase1; -typedef std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > FirstResult2; -typedef std::SingleDispatch < First, FirstResult2, const grammar::GrammarBase &, const std::vector < alphabet::Symbol > & > FirstBase2; +typedef std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > FirstResult2; +typedef std::SingleDispatch < First, FirstResult2, const grammar::GrammarBase &, const std::vector < DefaultSymbolType > & > FirstBase2; class First : public FirstBase1, public FirstBase2 { - static std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > first ( const std::set < alphabet::Symbol > & terminals, const std::set < alphabet::Symbol > & nonterminals, const std::map < alphabet::Symbol, std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > > & firstOfNonterminal, const std::vector < alphabet::Symbol > & rhs ); + static std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > first ( const std::set < DefaultSymbolType > & terminals, const std::set < DefaultSymbolType > & nonterminals, const std::map < DefaultSymbolType, std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > > & firstOfNonterminal, const std::vector < DefaultSymbolType > & rhs ); - static std::map < alphabet::Symbol, std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > > first ( const std::set < alphabet::Symbol > & terminals, const std::set < alphabet::Symbol > & nonterminals, const std::map < alphabet::Symbol, std::set < std::vector < alphabet::Symbol > > > & rules ); + static std::map < DefaultSymbolType, std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > > first ( const std::set < DefaultSymbolType > & terminals, const std::set < DefaultSymbolType > & nonterminals, const std::map < DefaultSymbolType, std::set < std::vector < DefaultSymbolType > > > & rules ); public: template < class T > static FirstResult1 first ( const T & grammar ); template < class T > - static FirstResult2 first ( const T & grammar, const std::vector < alphabet::Symbol > & rhs ); + static FirstResult2 first ( const T & grammar, const std::vector < DefaultSymbolType > & rhs ); static FirstResult1 first ( const grammar::Grammar & grammar ); - static FirstResult2 first ( const grammar::Grammar & grammar, const std::vector < alphabet::Symbol > & rhs ); + static FirstResult2 first ( const grammar::Grammar & grammar, const std::vector < DefaultSymbolType > & rhs ); }; diff --git a/alib2algo_experimental/src/grammar/parsing/Follow.cpp b/alib2algo_experimental/src/grammar/parsing/Follow.cpp index c5e301f47d..a75ecc831e 100644 --- a/alib2algo_experimental/src/grammar/parsing/Follow.cpp +++ b/alib2algo_experimental/src/grammar/parsing/Follow.cpp @@ -27,18 +27,18 @@ namespace grammar { namespace parsing { template < class T > -void Follow::follow ( const T & grammar, std::map < alphabet::Symbol, std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > > & followSet ) { - for ( const std::pair < const alphabet::Symbol, std::set < std::vector < alphabet::Symbol > > > & rule : grammar.getRawRules ( ) ) { - const alphabet::Symbol & X = rule.first; +void Follow::follow ( const T & grammar, std::map < DefaultSymbolType, std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > > & followSet ) { + for ( const std::pair < const DefaultSymbolType, std::set < std::vector < DefaultSymbolType > > > & rule : grammar.getRawRules ( ) ) { + const DefaultSymbolType & X = rule.first; - for ( const std::vector < alphabet::Symbol > & rhs : rule.second ) + for ( const std::vector < DefaultSymbolType > & rhs : rule.second ) // every nt in rhs is Y - for ( std::vector < alphabet::Symbol >::const_iterator it = rhs.begin ( ); it != rhs.end ( ); it++ ) { - const alphabet::Symbol & Y = * it; + for ( std::vector < DefaultSymbolType >::const_iterator it = rhs.begin ( ); it != rhs.end ( ); it++ ) { + const DefaultSymbolType & Y = * it; if ( !grammar.getNonterminalAlphabet ( ).count ( Y ) ) continue; - std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > firstBeta = First::first ( grammar, std::vector < alphabet::Symbol > ( std::next ( it ), rhs.end ( ) ) ); + std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > firstBeta = First::first ( grammar, std::vector < DefaultSymbolType > ( std::next ( it ), rhs.end ( ) ) ); if ( firstBeta.count ( string::Epsilon < >::EPSILON ) ) { firstBeta.erase ( string::Epsilon < >::EPSILON ); @@ -52,7 +52,7 @@ void Follow::follow ( const T & grammar, std::map < alphabet::Symbol, std::set < } template < class T > -std::map < alphabet::Symbol, std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > > Follow::follow ( const T & grammar ) { +std::map < DefaultSymbolType, std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > > Follow::follow ( const T & grammar ) { /* * 1. Follow(S) = { \varepsilon } * Follow(A) = {} forall A \in N, A \neq S @@ -64,14 +64,14 @@ std::map < alphabet::Symbol, std::set < std::variant < alphabet::Symbol, string: * 3. goto 2 if any follow set was changed in prev step. */ - std::map < alphabet::Symbol, std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > > followSet1; + std::map < DefaultSymbolType, std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > > followSet1; - for ( const alphabet::Symbol & symb : grammar.getNonterminalAlphabet ( ) ) + for ( const DefaultSymbolType & symb : grammar.getNonterminalAlphabet ( ) ) followSet1[symb]; followSet1[grammar.getInitialSymbol ( )] = { string::Epsilon < >::EPSILON }; - std::map < alphabet::Symbol, std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > > followSet2 = followSet1; + std::map < DefaultSymbolType, std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > > followSet2 = followSet1; do { follow ( grammar, followSet2 ); @@ -85,7 +85,7 @@ std::map < alphabet::Symbol, std::set < std::variant < alphabet::Symbol, string: } template < class T > -std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > Follow::follow ( const T & grammar, const alphabet::Symbol & nt ) { +std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > Follow::follow ( const T & grammar, const DefaultSymbolType & nt ) { if ( !grammar.getNonterminalAlphabet ( ).count ( nt ) ) throw exception::CommonException ( "Follow: Given symbol is not nonterminal." ); @@ -102,7 +102,7 @@ auto FollowLeftRG = FollowBase1::RegistratorWrapper < FollowResult1, grammar::Le auto FollowRightLG = FollowBase1::RegistratorWrapper < FollowResult1, grammar::RightLG < > > ( Follow::follow ); auto FollowRightRG = FollowBase1::RegistratorWrapper < FollowResult1, grammar::RightRG < > > ( Follow::follow ); -std::map < alphabet::Symbol, std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > > Follow::follow ( const grammar::Grammar & grammar ) { +std::map < DefaultSymbolType, std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > > Follow::follow ( const grammar::Grammar & grammar ) { return FollowBase1::dispatch ( grammar.getData ( ) ); } @@ -116,7 +116,7 @@ auto FollowLeftRG2 = FollowBase2::RegistratorWrapper < FollowResult2, grammar::L auto FollowRightLG2 = FollowBase2::RegistratorWrapper < FollowResult2, grammar::RightLG < > > ( Follow::follow ); auto FollowRightRG2 = FollowBase2::RegistratorWrapper < FollowResult2, grammar::RightRG < > > ( Follow::follow ); -std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > Follow::follow ( const grammar::Grammar & grammar, const alphabet::Symbol & nt ) { +std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > Follow::follow ( const grammar::Grammar & grammar, const DefaultSymbolType & nt ) { return FollowBase2::dispatch ( grammar.getData ( ), nt ); } diff --git a/alib2algo_experimental/src/grammar/parsing/Follow.h b/alib2algo_experimental/src/grammar/parsing/Follow.h index e67676524f..52aeb93ad9 100644 --- a/alib2algo_experimental/src/grammar/parsing/Follow.h +++ b/alib2algo_experimental/src/grammar/parsing/Follow.h @@ -22,15 +22,15 @@ namespace parsing { class Follow; -typedef std::map < alphabet::Symbol, std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > > FollowResult1; +typedef std::map < DefaultSymbolType, std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > > FollowResult1; typedef std::SingleDispatch < Follow, FollowResult1, const grammar::GrammarBase & > FollowBase1; -typedef std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > FollowResult2; -typedef std::SingleDispatch < Follow, FollowResult2, const grammar::GrammarBase &, const alphabet::Symbol & > FollowBase2; +typedef std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > FollowResult2; +typedef std::SingleDispatch < Follow, FollowResult2, const grammar::GrammarBase &, const DefaultSymbolType & > FollowBase2; class Follow : public FollowBase1, public FollowBase2 { template < class T > - static void follow ( const T & grammar, std::map < alphabet::Symbol, std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > > & followSet ); + static void follow ( const T & grammar, std::map < DefaultSymbolType, std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > > & followSet ); static Follow & getInstance ( ) { static Follow res; @@ -41,13 +41,13 @@ class Follow : public FollowBase1, public FollowBase2 { public: static FollowResult1 follow ( const grammar::Grammar & grammar ); - static FollowResult2 follow ( const grammar::Grammar & grammar, const alphabet::Symbol & nt ); + static FollowResult2 follow ( const grammar::Grammar & grammar, const DefaultSymbolType & nt ); template < class T > static FollowResult1 follow ( const T & grammar ); template < class T > - static FollowResult2 follow ( const T & grammar, const alphabet::Symbol & nt ); + static FollowResult2 follow ( const T & grammar, const DefaultSymbolType & nt ); static FollowBase1 & getInstance1 ( ) { return getInstance ( ); diff --git a/alib2algo_experimental/src/grammar/parsing/HandleFirstFirstConflict.cpp b/alib2algo_experimental/src/grammar/parsing/HandleFirstFirstConflict.cpp index 6da06bdfdc..6dce3107db 100644 --- a/alib2algo_experimental/src/grammar/parsing/HandleFirstFirstConflict.cpp +++ b/alib2algo_experimental/src/grammar/parsing/HandleFirstFirstConflict.cpp @@ -16,8 +16,8 @@ namespace grammar { namespace parsing { -void HandleFirstFirstConflict::handleFirstFirstConflict ( grammar::CFG < > & grammar, const alphabet::Symbol & terminal, const alphabet::Symbol & nonterminal, const std::set < std::vector < alphabet::Symbol > > & rhsds ) { - for ( const std::vector < alphabet::Symbol > & rhs : rhsds ) +void HandleFirstFirstConflict::handleFirstFirstConflict ( grammar::CFG < > & grammar, const DefaultSymbolType & terminal, const DefaultSymbolType & nonterminal, const std::set < std::vector < DefaultSymbolType > > & rhsds ) { + for ( const std::vector < DefaultSymbolType > & rhs : rhsds ) if ( ( rhs.size ( ) > 0 ) && grammar.getNonterminalAlphabet ( ).count ( rhs[0] ) && First::first ( grammar, rhs ).count ( terminal ) ) { CornerSubstitution::cornerSubstitution ( grammar, terminal, nonterminal ); return; diff --git a/alib2algo_experimental/src/grammar/parsing/HandleFirstFirstConflict.h b/alib2algo_experimental/src/grammar/parsing/HandleFirstFirstConflict.h index f37c358008..03e0fb2dac 100644 --- a/alib2algo_experimental/src/grammar/parsing/HandleFirstFirstConflict.h +++ b/alib2algo_experimental/src/grammar/parsing/HandleFirstFirstConflict.h @@ -18,7 +18,7 @@ namespace parsing { class HandleFirstFirstConflict { public: - static void handleFirstFirstConflict ( grammar::CFG < > & grammar, const alphabet::Symbol & terminal, const alphabet::Symbol & nonterminal, const std::set < std::vector < alphabet::Symbol > > & rhsds ); + static void handleFirstFirstConflict ( grammar::CFG < > & grammar, const DefaultSymbolType & terminal, const DefaultSymbolType & nonterminal, const std::set < std::vector < DefaultSymbolType > > & rhsds ); }; } /* namespace parsing */ diff --git a/alib2algo_experimental/src/grammar/parsing/HandleFirstFollowConflict.cpp b/alib2algo_experimental/src/grammar/parsing/HandleFirstFollowConflict.cpp index a24084ed2d..83fcddc302 100644 --- a/alib2algo_experimental/src/grammar/parsing/HandleFirstFollowConflict.cpp +++ b/alib2algo_experimental/src/grammar/parsing/HandleFirstFollowConflict.cpp @@ -18,22 +18,22 @@ namespace grammar { namespace parsing { -void HandleFirstFollowConflict::handleFirstFollowConflict ( grammar::CFG < > & grammar, const alphabet::Symbol & terminal, const alphabet::Symbol & nonterminal, const std::set < std::vector < alphabet::Symbol > > & /* rhsds */ ) { - std::set < alphabet::Symbol > nullableNonterminals = properties::NullableNonterminals::getNullableNonterminals ( grammar ); +void HandleFirstFollowConflict::handleFirstFollowConflict ( grammar::CFG < > & grammar, const DefaultSymbolType & terminal, const DefaultSymbolType & nonterminal, const std::set < std::vector < DefaultSymbolType > > & /* rhsds */ ) { + std::set < DefaultSymbolType > nullableNonterminals = properties::NullableNonterminals::getNullableNonterminals ( grammar ); - std::set < alphabet::Symbol > symbolsEndingWithNonterminal = { nonterminal }; + std::set < DefaultSymbolType > symbolsEndingWithNonterminal = { nonterminal }; // find all nonterminals X of the form X -> \alpha N \beta where \alpha is (NuT)*, N is in symbolsEndingWithNonterminal and \beta is nullable // A -> aC | \eps; B -> \eps; X -> AB; Y -> Xa // conflict is in A but algorithm needs to find a set {A, X} since the resolution is in creation [Xa] -> ABa | a while ( true ) { - std::set < alphabet::Symbol > symbolsEndingWithNonterminalOld = symbolsEndingWithNonterminal; + std::set < DefaultSymbolType > symbolsEndingWithNonterminalOld = symbolsEndingWithNonterminal; - for ( const std::pair < const alphabet::Symbol, std::set < std::vector < alphabet::Symbol > > > & rule : grammar.getRules ( ) ) { - const alphabet::Symbol & lhs = rule.first; + for ( const std::pair < const DefaultSymbolType, std::set < std::vector < DefaultSymbolType > > > & rule : grammar.getRules ( ) ) { + const DefaultSymbolType & lhs = rule.first; if ( Follow::follow ( grammar, lhs ).count ( terminal ) ) - for ( const std::vector < alphabet::Symbol > & rhs : rule.second ) + for ( const std::vector < DefaultSymbolType > & rhs : rule.second ) for ( unsigned i = rhs.size ( ); i > 0; i-- ) { if ( symbolsEndingWithNonterminal.count ( rhs[i - 1] ) ) symbolsEndingWithNonterminal.insert ( lhs ); @@ -47,11 +47,11 @@ void HandleFirstFollowConflict::handleFirstFollowConflict ( grammar::CFG < > & g } // find whether all occurrences of a symbol in symbolsEndingWithNonterminal are followed by terminal symbol (the paremeter) - for ( const std::pair < const alphabet::Symbol, std::set < std::vector < alphabet::Symbol > > > & rule : grammar.getRules ( ) ) - for ( const std::vector < alphabet::Symbol > & rhs : rule.second ) { + for ( const std::pair < const DefaultSymbolType, std::set < std::vector < DefaultSymbolType > > > & rule : grammar.getRules ( ) ) + for ( const std::vector < DefaultSymbolType > & rhs : rule.second ) { if ( rhs.size ( ) > 0 ) - for ( std::vector < alphabet::Symbol >::const_iterator iter = rhs.begin ( ); iter + 1 != rhs.end ( ); ++iter ) - if ( symbolsEndingWithNonterminal.count ( * iter ) && grammar.getNonterminalAlphabet ( ).count ( * ( iter + 1 ) ) && First::first ( grammar, std::vector < alphabet::Symbol > ( iter + 1, rhs.end ( ) ) ).count ( terminal ) ) { + for ( std::vector < DefaultSymbolType >::const_iterator iter = rhs.begin ( ); iter + 1 != rhs.end ( ); ++iter ) + if ( symbolsEndingWithNonterminal.count ( * iter ) && grammar.getNonterminalAlphabet ( ).count ( * ( iter + 1 ) ) && First::first ( grammar, std::vector < DefaultSymbolType > ( iter + 1, rhs.end ( ) ) ).count ( terminal ) ) { ExtractRightContext::extractRightContext ( grammar, terminal, symbolsEndingWithNonterminal ); return; } diff --git a/alib2algo_experimental/src/grammar/parsing/HandleFirstFollowConflict.h b/alib2algo_experimental/src/grammar/parsing/HandleFirstFollowConflict.h index 7ad19b9d7d..bcf00c9d99 100644 --- a/alib2algo_experimental/src/grammar/parsing/HandleFirstFollowConflict.h +++ b/alib2algo_experimental/src/grammar/parsing/HandleFirstFollowConflict.h @@ -19,7 +19,7 @@ namespace parsing { class HandleFirstFollowConflict { public: - static void handleFirstFollowConflict ( grammar::CFG < > & grammar, const alphabet::Symbol & terminal, const alphabet::Symbol & nonterminal, const std::set < std::vector < alphabet::Symbol > > & /* rhsds */ ); + static void handleFirstFollowConflict ( grammar::CFG < > & grammar, const DefaultSymbolType & terminal, const DefaultSymbolType & nonterminal, const std::set < std::vector < DefaultSymbolType > > & /* rhsds */ ); }; } /* namespace parsing */ diff --git a/alib2algo_experimental/src/grammar/parsing/LL1ParseTable.cpp b/alib2algo_experimental/src/grammar/parsing/LL1ParseTable.cpp index 302b844162..6d3e45f867 100644 --- a/alib2algo_experimental/src/grammar/parsing/LL1ParseTable.cpp +++ b/alib2algo_experimental/src/grammar/parsing/LL1ParseTable.cpp @@ -24,24 +24,24 @@ namespace grammar { namespace parsing { template < class T > -std::map < std::pair < std::variant < alphabet::Symbol, string::Epsilon < > >, alphabet::Symbol >, std::set < std::vector < alphabet::Symbol > > > LL1ParseTable::parseTable ( const T & grammar ) { - std::map < std::pair < std::variant < alphabet::Symbol, string::Epsilon < > >, alphabet::Symbol >, std::set < std::vector < alphabet::Symbol > > > res; +std::map < std::pair < std::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, std::set < std::vector < DefaultSymbolType > > > LL1ParseTable::parseTable ( const T & grammar ) { + std::map < std::pair < std::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, std::set < std::vector < DefaultSymbolType > > > res; - std::map < std::vector < alphabet::Symbol >, std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > > first = First::first ( grammar ); - std::map < alphabet::Symbol, std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > > follow = Follow::follow ( grammar ); + std::map < std::vector < DefaultSymbolType >, std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > > first = First::first ( grammar ); + std::map < DefaultSymbolType, std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > > follow = Follow::follow ( grammar ); - for ( const std::pair < const alphabet::Symbol, std::set < std::vector < alphabet::Symbol > > > & transition : grammar.getRawRules ( ) ) { - const alphabet::Symbol & lhs = transition.first; + for ( const std::pair < const DefaultSymbolType, std::set < std::vector < DefaultSymbolType > > > & transition : grammar.getRawRules ( ) ) { + const DefaultSymbolType & lhs = transition.first; - for ( const std::vector < alphabet::Symbol > & rhs : transition.second ) { - for ( const std::variant < alphabet::Symbol, string::Epsilon < > > & firstElem : first[rhs] ) { + for ( const std::vector < DefaultSymbolType > & rhs : transition.second ) { + for ( const std::variant < DefaultSymbolType, string::Epsilon < > > & firstElem : first[rhs] ) { if ( firstElem.is < string::Epsilon < > > ( ) ) continue; res[std::make_pair ( firstElem, lhs )].insert ( rhs ); } if ( first[rhs].count ( string::Epsilon < >::EPSILON ) ) - for ( const std::variant < alphabet::Symbol, string::Epsilon < > > & followElem : follow[lhs] ) + for ( const std::variant < DefaultSymbolType, string::Epsilon < > > & followElem : follow[lhs] ) res[std::make_pair ( followElem, lhs )].insert ( rhs ); } @@ -50,17 +50,17 @@ std::map < std::pair < std::variant < alphabet::Symbol, string::Epsilon < > >, a return res; } -auto LL1ParseTableCFG = LL1ParseTable::RegistratorWrapper < std::map < std::pair < std::variant < alphabet::Symbol, string::Epsilon < > >, alphabet::Symbol >, std::set < std::vector < alphabet::Symbol > > >, grammar::CFG < > > ( LL1ParseTable::parseTable ); -auto LL1ParseTableEpsilonFreeCFG = LL1ParseTable::RegistratorWrapper < std::map < std::pair < std::variant < alphabet::Symbol, string::Epsilon < > >, alphabet::Symbol >, std::set < std::vector < alphabet::Symbol > > >, grammar::EpsilonFreeCFG < > > ( LL1ParseTable::parseTable ); -auto LL1ParseTableGNF = LL1ParseTable::RegistratorWrapper < std::map < std::pair < std::variant < alphabet::Symbol, string::Epsilon < > >, alphabet::Symbol >, std::set < std::vector < alphabet::Symbol > > >, grammar::GNF < > > ( LL1ParseTable::parseTable ); -auto LL1ParseTableCNF = LL1ParseTable::RegistratorWrapper < std::map < std::pair < std::variant < alphabet::Symbol, string::Epsilon < > >, alphabet::Symbol >, std::set < std::vector < alphabet::Symbol > > >, grammar::CNF < > > ( LL1ParseTable::parseTable ); -auto LL1ParseTableLG = LL1ParseTable::RegistratorWrapper < std::map < std::pair < std::variant < alphabet::Symbol, string::Epsilon < > >, alphabet::Symbol >, std::set < std::vector < alphabet::Symbol > > >, grammar::LG < > > ( LL1ParseTable::parseTable ); -auto LL1ParseTableLeftLG = LL1ParseTable::RegistratorWrapper < std::map < std::pair < std::variant < alphabet::Symbol, string::Epsilon < > >, alphabet::Symbol >, std::set < std::vector < alphabet::Symbol > > >, grammar::LeftLG < > > ( LL1ParseTable::parseTable ); -auto LL1ParseTableLeftRG = LL1ParseTable::RegistratorWrapper < std::map < std::pair < std::variant < alphabet::Symbol, string::Epsilon < > >, alphabet::Symbol >, std::set < std::vector < alphabet::Symbol > > >, grammar::LeftRG < > > ( LL1ParseTable::parseTable ); -auto LL1ParseTableRightLG = LL1ParseTable::RegistratorWrapper < std::map < std::pair < std::variant < alphabet::Symbol, string::Epsilon < > >, alphabet::Symbol >, std::set < std::vector < alphabet::Symbol > > >, grammar::RightLG < > > ( LL1ParseTable::parseTable ); -auto LL1ParseTableRightRG = LL1ParseTable::RegistratorWrapper < std::map < std::pair < std::variant < alphabet::Symbol, string::Epsilon < > >, alphabet::Symbol >, std::set < std::vector < alphabet::Symbol > > >, grammar::RightRG < > > ( LL1ParseTable::parseTable ); - -std::map < std::pair < std::variant < alphabet::Symbol, string::Epsilon < > >, alphabet::Symbol >, std::set < std::vector < alphabet::Symbol > > > LL1ParseTable::parseTable ( const grammar::Grammar & grammar ) { +auto LL1ParseTableCFG = LL1ParseTable::RegistratorWrapper < std::map < std::pair < std::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, std::set < std::vector < DefaultSymbolType > > >, grammar::CFG < > > ( LL1ParseTable::parseTable ); +auto LL1ParseTableEpsilonFreeCFG = LL1ParseTable::RegistratorWrapper < std::map < std::pair < std::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, std::set < std::vector < DefaultSymbolType > > >, grammar::EpsilonFreeCFG < > > ( LL1ParseTable::parseTable ); +auto LL1ParseTableGNF = LL1ParseTable::RegistratorWrapper < std::map < std::pair < std::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, std::set < std::vector < DefaultSymbolType > > >, grammar::GNF < > > ( LL1ParseTable::parseTable ); +auto LL1ParseTableCNF = LL1ParseTable::RegistratorWrapper < std::map < std::pair < std::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, std::set < std::vector < DefaultSymbolType > > >, grammar::CNF < > > ( LL1ParseTable::parseTable ); +auto LL1ParseTableLG = LL1ParseTable::RegistratorWrapper < std::map < std::pair < std::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, std::set < std::vector < DefaultSymbolType > > >, grammar::LG < > > ( LL1ParseTable::parseTable ); +auto LL1ParseTableLeftLG = LL1ParseTable::RegistratorWrapper < std::map < std::pair < std::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, std::set < std::vector < DefaultSymbolType > > >, grammar::LeftLG < > > ( LL1ParseTable::parseTable ); +auto LL1ParseTableLeftRG = LL1ParseTable::RegistratorWrapper < std::map < std::pair < std::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, std::set < std::vector < DefaultSymbolType > > >, grammar::LeftRG < > > ( LL1ParseTable::parseTable ); +auto LL1ParseTableRightLG = LL1ParseTable::RegistratorWrapper < std::map < std::pair < std::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, std::set < std::vector < DefaultSymbolType > > >, grammar::RightLG < > > ( LL1ParseTable::parseTable ); +auto LL1ParseTableRightRG = LL1ParseTable::RegistratorWrapper < std::map < std::pair < std::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, std::set < std::vector < DefaultSymbolType > > >, grammar::RightRG < > > ( LL1ParseTable::parseTable ); + +std::map < std::pair < std::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, std::set < std::vector < DefaultSymbolType > > > LL1ParseTable::parseTable ( const grammar::Grammar & grammar ) { return dispatch ( grammar.getData ( ) ); } diff --git a/alib2algo_experimental/src/grammar/parsing/LL1ParseTable.h b/alib2algo_experimental/src/grammar/parsing/LL1ParseTable.h index 572743df3d..65595d12fb 100644 --- a/alib2algo_experimental/src/grammar/parsing/LL1ParseTable.h +++ b/alib2algo_experimental/src/grammar/parsing/LL1ParseTable.h @@ -21,12 +21,12 @@ namespace grammar { namespace parsing { -class LL1ParseTable : public std::SingleDispatch < LL1ParseTable, std::map < std::pair < std::variant < alphabet::Symbol, string::Epsilon < > >, alphabet::Symbol >, std::set < std::vector < alphabet::Symbol > > >, const grammar::GrammarBase & > { +class LL1ParseTable : public std::SingleDispatch < LL1ParseTable, std::map < std::pair < std::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, std::set < std::vector < DefaultSymbolType > > >, const grammar::GrammarBase & > { public: template < class T > - static std::map < std::pair < std::variant < alphabet::Symbol, string::Epsilon < > >, alphabet::Symbol >, std::set < std::vector < alphabet::Symbol > > > parseTable ( const T & grammar ); + static std::map < std::pair < std::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, std::set < std::vector < DefaultSymbolType > > > parseTable ( const T & grammar ); - static std::map < std::pair < std::variant < alphabet::Symbol, string::Epsilon < > >, alphabet::Symbol >, std::set < std::vector < alphabet::Symbol > > > parseTable ( const grammar::Grammar & grammar ); + static std::map < std::pair < std::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, std::set < std::vector < DefaultSymbolType > > > parseTable ( const grammar::Grammar & grammar ); }; diff --git a/alib2algo_experimental/src/grammar/parsing/LR0Parser.cpp b/alib2algo_experimental/src/grammar/parsing/LR0Parser.cpp index 5e6388fb40..27e66a4e30 100644 --- a/alib2algo_experimental/src/grammar/parsing/LR0Parser.cpp +++ b/alib2algo_experimental/src/grammar/parsing/LR0Parser.cpp @@ -23,18 +23,18 @@ LR0Items LR0Parser::getClosure ( LR0Items items, grammar::CFG < > originalGramma changed = false; for ( const LR0Items::value_type & symbolItems : items ) { - for ( const std::pair < unsigned, std::vector < alphabet::Symbol > > & item : symbolItems.second ) { + for ( const std::pair < unsigned, std::vector < DefaultSymbolType > > & item : symbolItems.second ) { unsigned position = item.first; - std::vector < alphabet::Symbol > rightHandSide = item.second; + std::vector < DefaultSymbolType > rightHandSide = item.second; if ( position == rightHandSide.size ( ) ) continue; - std::map < alphabet::Symbol, std::set < std::vector < alphabet::Symbol > > >::const_iterator rulesIterator = originalGrammar.getRules ( ) . find(rightHandSide[position]); + std::map < DefaultSymbolType, std::set < std::vector < DefaultSymbolType > > >::const_iterator rulesIterator = originalGrammar.getRules ( ) . find(rightHandSide[position]); if ( rulesIterator == originalGrammar.getRules ( ) . end ( ) ) continue; - for ( const std::vector < alphabet::Symbol > & rule : rulesIterator->second ) { + for ( const std::vector < DefaultSymbolType > & rule : rulesIterator->second ) { if (items[rightHandSide[position]].find ( { 0, rule } ) == items[rightHandSide[position]].end ( ) ) { changed = true; items[rightHandSide[position]].insert ( { 0, rule } ); @@ -47,13 +47,13 @@ LR0Items LR0Parser::getClosure ( LR0Items items, grammar::CFG < > originalGramma return items; } -LR0Items LR0Parser::getNextStateItems ( LR0Items items, alphabet::Symbol symbol, grammar::CFG < > originalGrammar ) { +LR0Items LR0Parser::getNextStateItems ( LR0Items items, DefaultSymbolType symbol, grammar::CFG < > originalGrammar ) { LR0Items transitionItems; for ( const LR0Items::value_type & symbolItems : items ) { - alphabet::Symbol leftHandSide = symbolItems.first; - for ( const std::pair < unsigned, std::vector < alphabet::Symbol > > & item : symbolItems.second) { + DefaultSymbolType leftHandSide = symbolItems.first; + for ( const std::pair < unsigned, std::vector < DefaultSymbolType > > & item : symbolItems.second) { unsigned position = item.first; - std::vector < alphabet::Symbol > rightHandSide = item.second; + std::vector < DefaultSymbolType > rightHandSide = item.second; if ( position == rightHandSide.size ( ) ) { continue; @@ -70,32 +70,32 @@ LR0Items LR0Parser::getNextStateItems ( LR0Items items, alphabet::Symbol symbol, automaton::DFA<> LR0Parser::getAutomaton ( grammar::CFG < > originalGrammar ) { grammar::CFG < > augmentedGrammar = LRParser::getAugmentedGrammar ( originalGrammar ); - alphabet::Symbol initialSymbol = augmentedGrammar.getInitialSymbol ( ); - const std::map < alphabet::Symbol, std::set < std::vector < alphabet::Symbol > > > & rules = augmentedGrammar.getRules ( ); + DefaultSymbolType initialSymbol = augmentedGrammar.getInitialSymbol ( ); + const std::map < DefaultSymbolType, std::set < std::vector < DefaultSymbolType > > > & rules = augmentedGrammar.getRules ( ); LR0Items initialItems; initialItems[initialSymbol].insert ( { 0, *rules.find ( initialSymbol ) -> second.begin ( ) } ); - label::Label initialState ( label::LR0ItemsLabel ( getClosure ( initialItems, augmentedGrammar ) ) ); + DefaultStateType initialState ( label::LR0ItemsLabel ( getClosure ( initialItems, augmentedGrammar ) ) ); automaton::DFA<> lr0Automaton ( initialState ); lr0Automaton.addInputSymbols ( augmentedGrammar.getNonterminalAlphabet ( ) ); lr0Automaton.addInputSymbols ( augmentedGrammar.getTerminalAlphabet ( ) ); - std::queue < label::Label > itemsToProcess; + std::queue < DefaultStateType > itemsToProcess; itemsToProcess.push ( initialState ); while ( !itemsToProcess.empty ( ) ) { - label::Label currentState = itemsToProcess.front ( ); + DefaultStateType currentState = itemsToProcess.front ( ); label::LR0ItemsLabel & currentLabel = static_cast < label::LR0ItemsLabel & > ( currentState . getData ( ) ); itemsToProcess.pop ( ); - for ( const alphabet::Symbol & symbol : lr0Automaton.getInputAlphabet ( ) ) { + for ( const DefaultSymbolType & symbol : lr0Automaton.getInputAlphabet ( ) ) { LR0Items nextStateItems = getNextStateItems ( currentLabel.getItems ( ), symbol, augmentedGrammar ); if ( !nextStateItems.empty ( ) ) { label::LR0ItemsLabel nextStateLabel ( nextStateItems ); - label::Label nextState ( nextStateLabel ); - std::set < label::Label > ::iterator stateIterator = lr0Automaton.getStates ( ) . find ( nextState ); + DefaultStateType nextState ( nextStateLabel ); + std::set < DefaultStateType > ::iterator stateIterator = lr0Automaton.getStates ( ) . find ( nextState ); if ( stateIterator == lr0Automaton.getStates ( ) . end ( ) ) { lr0Automaton.addState ( nextState ); lr0Automaton.addTransition ( currentState, symbol, nextState ); diff --git a/alib2algo_experimental/src/grammar/parsing/LR0Parser.h b/alib2algo_experimental/src/grammar/parsing/LR0Parser.h index 7fd2015d0a..8ab87ba3f3 100644 --- a/alib2algo_experimental/src/grammar/parsing/LR0Parser.h +++ b/alib2algo_experimental/src/grammar/parsing/LR0Parser.h @@ -20,7 +20,7 @@ class LR0Parser { public: static LR0Items getClosure ( LR0Items items, grammar::CFG < > originalGrammar ); - static LR0Items getNextStateItems ( LR0Items items, alphabet::Symbol symbol, grammar::CFG < > originalGrammar ); + static LR0Items getNextStateItems ( LR0Items items, DefaultSymbolType symbol, grammar::CFG < > originalGrammar ); static automaton::DFA<> getAutomaton ( grammar::CFG < > originalGrammar ); }; diff --git a/alib2algo_experimental/src/grammar/parsing/LRParser.cpp b/alib2algo_experimental/src/grammar/parsing/LRParser.cpp index 7f2e02fd94..1b44dd8250 100644 --- a/alib2algo_experimental/src/grammar/parsing/LRParser.cpp +++ b/alib2algo_experimental/src/grammar/parsing/LRParser.cpp @@ -15,12 +15,12 @@ namespace grammar { namespace parsing { -alphabet::Symbol LRParser::getEndOfInputSymbol ( grammar::CFG < > originalGrammar ) { - return common::createUnique ( alphabet::Symbol ( alphabet::EndSymbol ( ) ), originalGrammar.getTerminalAlphabet ( ), originalGrammar.getNonterminalAlphabet ( ) ); +DefaultSymbolType LRParser::getEndOfInputSymbol ( grammar::CFG < > originalGrammar ) { + return common::createUnique ( DefaultSymbolType ( alphabet::EndSymbol ( ) ), originalGrammar.getTerminalAlphabet ( ), originalGrammar.getNonterminalAlphabet ( ) ); } grammar::CFG < > LRParser::getAugmentedGrammar ( grammar::CFG < > originalGrammar ) { - alphabet::Symbol initialSymbol = common::createUnique ( originalGrammar.getInitialSymbol(), originalGrammar.getTerminalAlphabet ( ), originalGrammar.getNonterminalAlphabet ( ) ); + DefaultSymbolType initialSymbol = common::createUnique ( originalGrammar.getInitialSymbol(), originalGrammar.getTerminalAlphabet ( ), originalGrammar.getNonterminalAlphabet ( ) ); originalGrammar.addNonterminalSymbol ( initialSymbol ); originalGrammar.addRule ( initialSymbol, { originalGrammar.getInitialSymbol ( ) } ); @@ -29,8 +29,8 @@ grammar::CFG < > LRParser::getAugmentedGrammar ( grammar::CFG < > originalGramma return originalGrammar; } -bool LRParser::parse ( LRActionTable actionTable, LRGotoTable gotoTable, label::Label initialState, std::vector < alphabet::Symbol > input ) { - std::stack < label::Label > states; +bool LRParser::parse ( LRActionTable actionTable, LRGotoTable gotoTable, DefaultStateType initialState, std::vector < DefaultSymbolType > input ) { + std::stack < DefaultStateType > states; states.push ( initialState ); unsigned currentPosition = 0; @@ -41,13 +41,13 @@ bool LRParser::parse ( LRActionTable actionTable, LRGotoTable gotoTable, label:: switch ( actionIterator->second.first ) { case LRAction::Shift: - states.push ( actionIterator->second.second.get < label::Label > ( ) ); + states.push ( actionIterator->second.second.get < DefaultStateType > ( ) ); ++currentPosition; break; case LRAction::Reduce: { - std::pair < alphabet::Symbol, std::vector < alphabet::Symbol > > reduceBy = actionIterator->second.second.get < std::pair < alphabet::Symbol, std::vector < alphabet::Symbol > > > ( ); + std::pair < DefaultSymbolType, std::vector < DefaultSymbolType > > reduceBy = actionIterator->second.second.get < std::pair < DefaultSymbolType, std::vector < DefaultSymbolType > > > ( ); for ( unsigned i = 0; i < reduceBy.second.size ( ); ++i ) { states.pop ( ); } diff --git a/alib2algo_experimental/src/grammar/parsing/LRParser.h b/alib2algo_experimental/src/grammar/parsing/LRParser.h index 50677f4e3a..6de9331dd0 100644 --- a/alib2algo_experimental/src/grammar/parsing/LRParser.h +++ b/alib2algo_experimental/src/grammar/parsing/LRParser.h @@ -20,11 +20,11 @@ namespace parsing { class LRParser { public: - static alphabet::Symbol getEndOfInputSymbol ( grammar::CFG < > originalGrammar ); + static DefaultSymbolType getEndOfInputSymbol ( grammar::CFG < > originalGrammar ); static grammar::CFG < > getAugmentedGrammar ( grammar::CFG < > originalGrammar ); - static bool parse ( LRActionTable actionTable, LRGotoTable gotoTable, label::Label initialState, std::vector < alphabet::Symbol > input ); + static bool parse ( LRActionTable actionTable, LRGotoTable gotoTable, DefaultStateType initialState, std::vector < DefaultSymbolType > input ); }; } /* namespace parsing */ diff --git a/alib2algo_experimental/src/grammar/parsing/LeftFactorize.cpp b/alib2algo_experimental/src/grammar/parsing/LeftFactorize.cpp index 0016f1a2ef..5c262e537c 100644 --- a/alib2algo_experimental/src/grammar/parsing/LeftFactorize.cpp +++ b/alib2algo_experimental/src/grammar/parsing/LeftFactorize.cpp @@ -14,22 +14,22 @@ namespace grammar { namespace parsing { -void LeftFactorize::leftFactorize ( grammar::CFG < > & grammar, const alphabet::Symbol & terminal, const alphabet::Symbol & nonterminal ) { +void LeftFactorize::leftFactorize ( grammar::CFG < > & grammar, const DefaultSymbolType & terminal, const DefaultSymbolType & nonterminal ) { grammar::CFG < > res ( grammar.getInitialSymbol ( ) ); res.setNonterminalAlphabet ( grammar.getNonterminalAlphabet ( ) ); res.setTerminalAlphabet ( grammar.getTerminalAlphabet ( ) ); - alphabet::Symbol primed = common::createUnique ( nonterminal, grammar.getTerminalAlphabet ( ), grammar.getNonterminalAlphabet ( ) ); + DefaultSymbolType primed = common::createUnique ( nonterminal, grammar.getTerminalAlphabet ( ), grammar.getNonterminalAlphabet ( ) ); res.addNonterminalSymbol ( primed ); - for ( const std::pair < const alphabet::Symbol, std::set < std::vector < alphabet::Symbol > > > & rule : grammar.getRules ( ) ) { - const alphabet::Symbol & lhs = rule.first; + for ( const std::pair < const DefaultSymbolType, std::set < std::vector < DefaultSymbolType > > > & rule : grammar.getRules ( ) ) { + const DefaultSymbolType & lhs = rule.first; - for ( const std::vector < alphabet::Symbol > & rhs : rule.second ) { + for ( const std::vector < DefaultSymbolType > & rhs : rule.second ) { if ( ( lhs == nonterminal ) && ( rhs.size ( ) > 0 ) && ( rhs[0] == terminal ) ) { - res.addRule ( lhs, std::vector < alphabet::Symbol > { terminal, primed } ); - res.addRule ( primed, std::vector < alphabet::Symbol > ( rhs.begin ( ) + 1, rhs.end ( ) ) ); + res.addRule ( lhs, std::vector < DefaultSymbolType > { terminal, primed } ); + res.addRule ( primed, std::vector < DefaultSymbolType > ( rhs.begin ( ) + 1, rhs.end ( ) ) ); } else { res.addRule ( lhs, rhs ); } diff --git a/alib2algo_experimental/src/grammar/parsing/LeftFactorize.h b/alib2algo_experimental/src/grammar/parsing/LeftFactorize.h index 00e4a5dda4..e13f3db71e 100644 --- a/alib2algo_experimental/src/grammar/parsing/LeftFactorize.h +++ b/alib2algo_experimental/src/grammar/parsing/LeftFactorize.h @@ -18,7 +18,7 @@ namespace parsing { class LeftFactorize { public: - static void leftFactorize ( grammar::CFG < > & grammar, const alphabet::Symbol & terminal, const alphabet::Symbol & nonterminal ); + static void leftFactorize ( grammar::CFG < > & grammar, const DefaultSymbolType & terminal, const DefaultSymbolType & nonterminal ); }; diff --git a/alib2algo_experimental/src/grammar/parsing/SLR1ParseTable.cpp b/alib2algo_experimental/src/grammar/parsing/SLR1ParseTable.cpp index 96dedf6018..da6665e6aa 100644 --- a/alib2algo_experimental/src/grammar/parsing/SLR1ParseTable.cpp +++ b/alib2algo_experimental/src/grammar/parsing/SLR1ParseTable.cpp @@ -29,14 +29,14 @@ LRActionTable SLR1ParseTable::getActionTable ( grammar::CFG < > originalGrammar LRActionTable actionTable; grammar::CFG < > augmentedGrammar = LRParser::getAugmentedGrammar ( originalGrammar ); automaton::DFA<> parsingAutomaton = LR0Parser::getAutomaton ( originalGrammar ); - for ( const label::Label & state : parsingAutomaton.getStates ( ) ) { + for ( const DefaultStateType & state : parsingAutomaton.getStates ( ) ) { LR0Items items = static_cast < const label::LR0ItemsLabel & > ( state . getData ( ) ) . getItems ( ); - std::map < std::pair < label::Label, alphabet::Symbol >, label::Label > transitionsFromCurrentState = parsingAutomaton.getTransitionsFromState ( state ); + std::map < std::pair < DefaultStateType, DefaultSymbolType >, DefaultStateType > transitionsFromCurrentState = parsingAutomaton.getTransitionsFromState ( state ); for ( const LR0Items::value_type & nonterminalItems : items ) { - alphabet::Symbol leftHandSide = nonterminalItems.first; - for ( const std::pair < unsigned, std::vector < alphabet::Symbol > > & item : nonterminalItems.second ) { + DefaultSymbolType leftHandSide = nonterminalItems.first; + for ( const std::pair < unsigned, std::vector < DefaultSymbolType > > & item : nonterminalItems.second ) { unsigned position = item.first; - std::vector < alphabet::Symbol > rightHandSide = item.second; + std::vector < DefaultSymbolType > rightHandSide = item.second; if ( position == rightHandSide.size ( ) ) { if ( leftHandSide == augmentedGrammar.getInitialSymbol ( ) ) { @@ -45,10 +45,10 @@ LRActionTable SLR1ParseTable::getActionTable ( grammar::CFG < > originalGrammar } grammar::parsing::FollowResult2 followSet = grammar::parsing::Follow::follow ( augmentedGrammar, leftHandSide ); - std::pair < alphabet::Symbol, std::vector < alphabet::Symbol > > currentRule = { leftHandSide, rightHandSide }; - for ( const std::variant < alphabet::Symbol, string::Epsilon < > > & followSymbol : followSet ) { - if ( followSymbol.is < alphabet::Symbol > ( ) ) { - insertToActionTable(actionTable, { state, followSymbol.get < alphabet::Symbol > ( ) }, { LRAction::Reduce, currentRule } ); + std::pair < DefaultSymbolType, std::vector < DefaultSymbolType > > currentRule = { leftHandSide, rightHandSide }; + for ( const std::variant < DefaultSymbolType, string::Epsilon < > > & followSymbol : followSet ) { + if ( followSymbol.is < DefaultSymbolType > ( ) ) { + insertToActionTable(actionTable, { state, followSymbol.get < DefaultSymbolType > ( ) }, { LRAction::Reduce, currentRule } ); } else { insertToActionTable(actionTable, { state, LRParser::getEndOfInputSymbol(augmentedGrammar) }, { LRAction::Reduce, currentRule } ); } @@ -56,7 +56,7 @@ LRActionTable SLR1ParseTable::getActionTable ( grammar::CFG < > originalGrammar continue; } - alphabet::Symbol currentSymbol = rightHandSide[position]; + DefaultSymbolType currentSymbol = rightHandSide[position]; if ( originalGrammar.getTerminalAlphabet ( ) . find ( currentSymbol ) != originalGrammar.getTerminalAlphabet ( ) . end ( ) ) { insertToActionTable(actionTable, { state, currentSymbol }, { LRAction::Shift, transitionsFromCurrentState.find ( { state, currentSymbol } )->second } ); } @@ -71,10 +71,10 @@ LRGotoTable SLR1ParseTable::getGotoTable ( grammar::CFG < > originalGrammar ) { LRGotoTable gotoTable; grammar::CFG < > augmentedGrammar = LRParser::getAugmentedGrammar ( originalGrammar ); automaton::DFA<> parsingAutomaton = LR0Parser::getAutomaton ( originalGrammar ); - for ( const label::Label & state : parsingAutomaton.getStates ( ) ) { - std::map < std::pair < label::Label, alphabet::Symbol >, label::Label > transitionsFromCurrentState = parsingAutomaton.getTransitionsFromState ( state ); - for ( const alphabet::Symbol & nonterminal : augmentedGrammar.getNonterminalAlphabet ( ) ) { - std::map < std::pair<label::Label, alphabet::Symbol >, label::Label >::iterator transitionIterator = transitionsFromCurrentState.find ( { state, nonterminal } ); + for ( const DefaultStateType & state : parsingAutomaton.getStates ( ) ) { + std::map < std::pair < DefaultStateType, DefaultSymbolType >, DefaultStateType > transitionsFromCurrentState = parsingAutomaton.getTransitionsFromState ( state ); + for ( const DefaultSymbolType & nonterminal : augmentedGrammar.getNonterminalAlphabet ( ) ) { + std::map < std::pair<DefaultStateType, DefaultSymbolType >, DefaultStateType >::iterator transitionIterator = transitionsFromCurrentState.find ( { state, nonterminal } ); if ( transitionIterator != transitionsFromCurrentState.end ( ) ) { gotoTable.insert ( { { state, nonterminal }, transitionIterator->second } ); } diff --git a/alib2algo_experimental/src/grammar/parsing/common/Substitute.cpp b/alib2algo_experimental/src/grammar/parsing/common/Substitute.cpp index b10d0da37c..4d823fbb70 100644 --- a/alib2algo_experimental/src/grammar/parsing/common/Substitute.cpp +++ b/alib2algo_experimental/src/grammar/parsing/common/Substitute.cpp @@ -12,13 +12,13 @@ namespace grammar { namespace parsing { -void Substitute::substitute ( const grammar::CFG < > & orig, grammar::CFG < > & res, const alphabet::Symbol & origLHS, const std::vector < alphabet::Symbol > & origRHS, std::vector < alphabet::Symbol >::const_iterator nonterminal ) { +void Substitute::substitute ( const grammar::CFG < > & orig, grammar::CFG < > & res, const DefaultSymbolType & origLHS, const std::vector < DefaultSymbolType > & origRHS, std::vector < DefaultSymbolType >::const_iterator nonterminal ) { auto iter = orig.getRules ( ).find ( * nonterminal ); if ( iter == orig.getRules ( ).end ( ) ) return; - for ( const std::vector < alphabet::Symbol > & rhs : iter->second ) { - std::vector < alphabet::Symbol > newRHS ( origRHS.begin ( ), nonterminal ); + for ( const std::vector < DefaultSymbolType > & rhs : iter->second ) { + std::vector < DefaultSymbolType > newRHS ( origRHS.begin ( ), nonterminal ); newRHS.insert ( newRHS.end ( ), rhs.begin ( ), rhs.end ( ) ); newRHS.insert ( newRHS.end ( ), nonterminal + 1, origRHS.end ( ) ); res.addRule ( origLHS, newRHS ); diff --git a/alib2algo_experimental/src/grammar/parsing/common/Substitute.h b/alib2algo_experimental/src/grammar/parsing/common/Substitute.h index d9246ed1cb..2167ece97b 100644 --- a/alib2algo_experimental/src/grammar/parsing/common/Substitute.h +++ b/alib2algo_experimental/src/grammar/parsing/common/Substitute.h @@ -19,7 +19,7 @@ namespace parsing { class Substitute { public: - static void substitute ( const grammar::CFG < > & orig, grammar::CFG < > & res, const alphabet::Symbol & origLHS, const std::vector < alphabet::Symbol > & origRHS, std::vector < alphabet::Symbol >::const_iterator nonterminal ); + static void substitute ( const grammar::CFG < > & orig, grammar::CFG < > & res, const DefaultSymbolType & origLHS, const std::vector < DefaultSymbolType > & origRHS, std::vector < DefaultSymbolType >::const_iterator nonterminal ); }; diff --git a/alib2algo_experimental/test-src/grammar/parsing/CornerSubstitution.cpp b/alib2algo_experimental/test-src/grammar/parsing/CornerSubstitution.cpp index 7fd41b02d6..b2859cd0a4 100644 --- a/alib2algo_experimental/test-src/grammar/parsing/CornerSubstitution.cpp +++ b/alib2algo_experimental/test-src/grammar/parsing/CornerSubstitution.cpp @@ -13,14 +13,14 @@ void CornerSubstitution::tearDown ( ) { } void CornerSubstitution::testCornerSubstitution ( ) { - alphabet::Symbol A = alphabet::Symbol ( "A" ); - alphabet::Symbol B = alphabet::Symbol ( "B" ); - alphabet::Symbol C = alphabet::Symbol ( "C" ); + DefaultSymbolType A = DefaultSymbolType ( "A" ); + DefaultSymbolType B = DefaultSymbolType ( "B" ); + DefaultSymbolType C = DefaultSymbolType ( "C" ); - alphabet::Symbol a = alphabet::Symbol ( 'a' ); - alphabet::Symbol b = alphabet::Symbol ( 'b' ); - alphabet::Symbol c = alphabet::Symbol ( 'c' ); - alphabet::Symbol d = alphabet::Symbol ( 'd' ); + DefaultSymbolType a = DefaultSymbolType ( 'a' ); + DefaultSymbolType b = DefaultSymbolType ( 'b' ); + DefaultSymbolType c = DefaultSymbolType ( 'c' ); + DefaultSymbolType d = DefaultSymbolType ( 'd' ); grammar::CFG < > grammar ( A ); @@ -28,12 +28,12 @@ void CornerSubstitution::testCornerSubstitution ( ) { grammar.setNonterminalAlphabet ( { A, B, C } ); grammar.setInitialSymbol ( A ); - grammar.addRule ( A, std::vector < alphabet::Symbol > { a, B } ); - grammar.addRule ( A, std::vector < alphabet::Symbol > { C, B } ); - grammar.addRule ( B, std::vector < alphabet::Symbol > { c, B } ); - grammar.addRule ( B, std::vector < alphabet::Symbol > { d } ); - grammar.addRule ( C, std::vector < alphabet::Symbol > { a, C } ); - grammar.addRule ( C, std::vector < alphabet::Symbol > { b, B } ); + grammar.addRule ( A, std::vector < DefaultSymbolType > { a, B } ); + grammar.addRule ( A, std::vector < DefaultSymbolType > { C, B } ); + grammar.addRule ( B, std::vector < DefaultSymbolType > { c, B } ); + grammar.addRule ( B, std::vector < DefaultSymbolType > { d } ); + grammar.addRule ( C, std::vector < DefaultSymbolType > { a, C } ); + grammar.addRule ( C, std::vector < DefaultSymbolType > { b, B } ); grammar::CFG < > res = grammar; grammar::parsing::CornerSubstitution::cornerSubstitution ( res, a, A ); @@ -44,13 +44,13 @@ void CornerSubstitution::testCornerSubstitution ( ) { comp.setNonterminalAlphabet ( { A, B, C } ); comp.setInitialSymbol ( A ); - comp.addRule ( A, std::vector < alphabet::Symbol > { a, B } ); - comp.addRule ( A, std::vector < alphabet::Symbol > { a, C, B } ); - comp.addRule ( A, std::vector < alphabet::Symbol > { b, B, B } ); - comp.addRule ( B, std::vector < alphabet::Symbol > { c, B } ); - comp.addRule ( B, std::vector < alphabet::Symbol > { d } ); - comp.addRule ( C, std::vector < alphabet::Symbol > { a, C } ); - comp.addRule ( C, std::vector < alphabet::Symbol > { b, B } ); + comp.addRule ( A, std::vector < DefaultSymbolType > { a, B } ); + comp.addRule ( A, std::vector < DefaultSymbolType > { a, C, B } ); + comp.addRule ( A, std::vector < DefaultSymbolType > { b, B, B } ); + comp.addRule ( B, std::vector < DefaultSymbolType > { c, B } ); + comp.addRule ( B, std::vector < DefaultSymbolType > { d } ); + comp.addRule ( C, std::vector < DefaultSymbolType > { a, C } ); + comp.addRule ( C, std::vector < DefaultSymbolType > { b, B } ); // std::cout << res << std::endl << comp << std::endl; @@ -58,14 +58,14 @@ void CornerSubstitution::testCornerSubstitution ( ) { } void CornerSubstitution::testCornerSubstitution2 ( ) { - alphabet::Symbol A = alphabet::Symbol ( "A" ); - alphabet::Symbol B = alphabet::Symbol ( "B" ); - alphabet::Symbol C = alphabet::Symbol ( "C" ); + DefaultSymbolType A = DefaultSymbolType ( "A" ); + DefaultSymbolType B = DefaultSymbolType ( "B" ); + DefaultSymbolType C = DefaultSymbolType ( "C" ); - alphabet::Symbol a = alphabet::Symbol ( 'a' ); - alphabet::Symbol b = alphabet::Symbol ( 'b' ); - alphabet::Symbol c = alphabet::Symbol ( 'c' ); - alphabet::Symbol d = alphabet::Symbol ( 'd' ); + DefaultSymbolType a = DefaultSymbolType ( 'a' ); + DefaultSymbolType b = DefaultSymbolType ( 'b' ); + DefaultSymbolType c = DefaultSymbolType ( 'c' ); + DefaultSymbolType d = DefaultSymbolType ( 'd' ); grammar::CFG < > grammar ( A ); @@ -73,12 +73,12 @@ void CornerSubstitution::testCornerSubstitution2 ( ) { grammar.setNonterminalAlphabet ( { A, B, C } ); grammar.setInitialSymbol ( A ); - grammar.addRule ( A, std::vector < alphabet::Symbol > { a, B } ); - grammar.addRule ( A, std::vector < alphabet::Symbol > { C, B } ); - grammar.addRule ( B, std::vector < alphabet::Symbol > { c, B } ); - grammar.addRule ( B, std::vector < alphabet::Symbol > { a } ); - grammar.addRule ( C, std::vector < alphabet::Symbol > { a, C } ); - grammar.addRule ( C, std::vector < alphabet::Symbol > { B, b } ); + grammar.addRule ( A, std::vector < DefaultSymbolType > { a, B } ); + grammar.addRule ( A, std::vector < DefaultSymbolType > { C, B } ); + grammar.addRule ( B, std::vector < DefaultSymbolType > { c, B } ); + grammar.addRule ( B, std::vector < DefaultSymbolType > { a } ); + grammar.addRule ( C, std::vector < DefaultSymbolType > { a, C } ); + grammar.addRule ( C, std::vector < DefaultSymbolType > { B, b } ); grammar::CFG < > res = grammar; grammar::parsing::CornerSubstitution::cornerSubstitution ( res, a, A ); @@ -90,14 +90,14 @@ void CornerSubstitution::testCornerSubstitution2 ( ) { comp.setNonterminalAlphabet ( { A, B, C } ); comp.setInitialSymbol ( A ); - comp.addRule ( A, std::vector < alphabet::Symbol > { a, B } ); - comp.addRule ( A, std::vector < alphabet::Symbol > { a, C, B } ); - comp.addRule ( A, std::vector < alphabet::Symbol > { a, b, B } ); - comp.addRule ( A, std::vector < alphabet::Symbol > { c, B, b, B } ); - comp.addRule ( B, std::vector < alphabet::Symbol > { c, B } ); - comp.addRule ( B, std::vector < alphabet::Symbol > { a } ); - comp.addRule ( C, std::vector < alphabet::Symbol > { a, C } ); - comp.addRule ( C, std::vector < alphabet::Symbol > { B, b } ); + comp.addRule ( A, std::vector < DefaultSymbolType > { a, B } ); + comp.addRule ( A, std::vector < DefaultSymbolType > { a, C, B } ); + comp.addRule ( A, std::vector < DefaultSymbolType > { a, b, B } ); + comp.addRule ( A, std::vector < DefaultSymbolType > { c, B, b, B } ); + comp.addRule ( B, std::vector < DefaultSymbolType > { c, B } ); + comp.addRule ( B, std::vector < DefaultSymbolType > { a } ); + comp.addRule ( C, std::vector < DefaultSymbolType > { a, C } ); + comp.addRule ( C, std::vector < DefaultSymbolType > { B, b } ); // std::cout << res << std::endl << comp << std::endl; diff --git a/alib2algo_experimental/test-src/grammar/parsing/ExtractRightContext.cpp b/alib2algo_experimental/test-src/grammar/parsing/ExtractRightContext.cpp index 0b6958272e..37f8f3615a 100644 --- a/alib2algo_experimental/test-src/grammar/parsing/ExtractRightContext.cpp +++ b/alib2algo_experimental/test-src/grammar/parsing/ExtractRightContext.cpp @@ -13,13 +13,13 @@ void ExtractRightContext::tearDown ( ) { } void ExtractRightContext::testExtractRightContext ( ) { - alphabet::Symbol S = alphabet::Symbol ( "S" ); - alphabet::Symbol A = alphabet::Symbol ( "A" ); - alphabet::Symbol C = alphabet::Symbol ( "C" ); + DefaultSymbolType S = DefaultSymbolType ( "S" ); + DefaultSymbolType A = DefaultSymbolType ( "A" ); + DefaultSymbolType C = DefaultSymbolType ( "C" ); - alphabet::Symbol a = alphabet::Symbol ( 'a' ); - alphabet::Symbol b = alphabet::Symbol ( 'b' ); - alphabet::Symbol c = alphabet::Symbol ( 'c' ); + DefaultSymbolType a = DefaultSymbolType ( 'a' ); + DefaultSymbolType b = DefaultSymbolType ( 'b' ); + DefaultSymbolType c = DefaultSymbolType ( 'c' ); grammar::CFG < > grammar ( S ); @@ -27,10 +27,10 @@ void ExtractRightContext::testExtractRightContext ( ) { grammar.setNonterminalAlphabet ( { S, A, C } ); grammar.setInitialSymbol ( S ); - grammar.addRule ( S, std::vector < alphabet::Symbol > { b, c, A, C } ); - grammar.addRule ( S, std::vector < alphabet::Symbol > { b, b } ); - grammar.addRule ( A, std::vector < alphabet::Symbol > { } ); - grammar.addRule ( A, std::vector < alphabet::Symbol > { a, c, A, b } ); + grammar.addRule ( S, std::vector < DefaultSymbolType > { b, c, A, C } ); + grammar.addRule ( S, std::vector < DefaultSymbolType > { b, b } ); + grammar.addRule ( A, std::vector < DefaultSymbolType > { } ); + grammar.addRule ( A, std::vector < DefaultSymbolType > { a, c, A, b } ); grammar.addRule ( C, { a, b } ); grammar.addRule ( C, { c, C } ); @@ -43,13 +43,13 @@ void ExtractRightContext::testExtractRightContext ( ) { comp.setNonterminalAlphabet ( { S, A, C } ); comp.setInitialSymbol ( S ); - comp.addRule ( S, std::vector < alphabet::Symbol > { b, c, A, a, b } ); - comp.addRule ( S, std::vector < alphabet::Symbol > { b, c, A, c, C } ); - comp.addRule ( S, std::vector < alphabet::Symbol > { b, b } ); - comp.addRule ( A, std::vector < alphabet::Symbol > { } ); - comp.addRule ( A, std::vector < alphabet::Symbol > { a, c, A, b } ); - comp.addRule ( C, std::vector < alphabet::Symbol > { a, b } ); - comp.addRule ( C, std::vector < alphabet::Symbol > { c, C } ); + comp.addRule ( S, std::vector < DefaultSymbolType > { b, c, A, a, b } ); + comp.addRule ( S, std::vector < DefaultSymbolType > { b, c, A, c, C } ); + comp.addRule ( S, std::vector < DefaultSymbolType > { b, b } ); + comp.addRule ( A, std::vector < DefaultSymbolType > { } ); + comp.addRule ( A, std::vector < DefaultSymbolType > { a, c, A, b } ); + comp.addRule ( C, std::vector < DefaultSymbolType > { a, b } ); + comp.addRule ( C, std::vector < DefaultSymbolType > { c, C } ); // std::cout << res << std::endl << comp << std::endl; @@ -57,14 +57,14 @@ void ExtractRightContext::testExtractRightContext ( ) { } void ExtractRightContext::testExtractRightContext2 ( ) { - alphabet::Symbol S = alphabet::Symbol ( "S" ); - alphabet::Symbol X = alphabet::Symbol ( "X" ); - alphabet::Symbol A = alphabet::Symbol ( "A" ); - alphabet::Symbol C = alphabet::Symbol ( "C" ); + DefaultSymbolType S = DefaultSymbolType ( "S" ); + DefaultSymbolType X = DefaultSymbolType ( "X" ); + DefaultSymbolType A = DefaultSymbolType ( "A" ); + DefaultSymbolType C = DefaultSymbolType ( "C" ); - alphabet::Symbol a = alphabet::Symbol ( 'a' ); - alphabet::Symbol b = alphabet::Symbol ( 'b' ); - alphabet::Symbol c = alphabet::Symbol ( 'c' ); + DefaultSymbolType a = DefaultSymbolType ( 'a' ); + DefaultSymbolType b = DefaultSymbolType ( 'b' ); + DefaultSymbolType c = DefaultSymbolType ( 'c' ); grammar::CFG < > grammar ( S ); @@ -72,11 +72,11 @@ void ExtractRightContext::testExtractRightContext2 ( ) { grammar.setNonterminalAlphabet ( { S, X, A, C } ); grammar.setInitialSymbol ( S ); - grammar.addRule ( S, std::vector < alphabet::Symbol > { b, c, X, C } ); - grammar.addRule ( S, std::vector < alphabet::Symbol > { b, b } ); - grammar.addRule ( X, std::vector < alphabet::Symbol > { b, A } ); - grammar.addRule ( A, std::vector < alphabet::Symbol > { } ); - grammar.addRule ( A, std::vector < alphabet::Symbol > { a, c, A, b } ); + grammar.addRule ( S, std::vector < DefaultSymbolType > { b, c, X, C } ); + grammar.addRule ( S, std::vector < DefaultSymbolType > { b, b } ); + grammar.addRule ( X, std::vector < DefaultSymbolType > { b, A } ); + grammar.addRule ( A, std::vector < DefaultSymbolType > { } ); + grammar.addRule ( A, std::vector < DefaultSymbolType > { a, c, A, b } ); grammar.addRule ( C, { A, a, b } ); grammar.addRule ( C, { c, C } ); @@ -89,14 +89,14 @@ void ExtractRightContext::testExtractRightContext2 ( ) { comp.setNonterminalAlphabet ( { S, X, A, C } ); comp.setInitialSymbol ( S ); - comp.addRule ( S, std::vector < alphabet::Symbol > { b, c, X, A, a, b } ); - comp.addRule ( S, std::vector < alphabet::Symbol > { b, c, X, c, C } ); - comp.addRule ( S, std::vector < alphabet::Symbol > { b, b } ); + comp.addRule ( S, std::vector < DefaultSymbolType > { b, c, X, A, a, b } ); + comp.addRule ( S, std::vector < DefaultSymbolType > { b, c, X, c, C } ); + comp.addRule ( S, std::vector < DefaultSymbolType > { b, b } ); comp.addRule ( X, { b, A } ); - comp.addRule ( A, std::vector < alphabet::Symbol > { } ); - comp.addRule ( A, std::vector < alphabet::Symbol > { a, c, A, b } ); - comp.addRule ( C, std::vector < alphabet::Symbol > { A, a, b } ); - comp.addRule ( C, std::vector < alphabet::Symbol > { c, C } ); + comp.addRule ( A, std::vector < DefaultSymbolType > { } ); + comp.addRule ( A, std::vector < DefaultSymbolType > { a, c, A, b } ); + comp.addRule ( C, std::vector < DefaultSymbolType > { A, a, b } ); + comp.addRule ( C, std::vector < DefaultSymbolType > { c, C } ); // std::cout << res << std::endl << comp << std::endl; diff --git a/alib2algo_experimental/test-src/grammar/parsing/FirstTest.cpp b/alib2algo_experimental/test-src/grammar/parsing/FirstTest.cpp index 6d87cbc969..f0b21a96a6 100644 --- a/alib2algo_experimental/test-src/grammar/parsing/FirstTest.cpp +++ b/alib2algo_experimental/test-src/grammar/parsing/FirstTest.cpp @@ -15,26 +15,26 @@ void FirstTest::tearDown ( ) { void FirstTest::testFirst ( ) { { - alphabet::Symbol nE = alphabet::Symbol ( 'E' ); - alphabet::Symbol nT = alphabet::Symbol ( 'T' ); - alphabet::Symbol nF = alphabet::Symbol ( 'F' ); + DefaultSymbolType nE = DefaultSymbolType ( 'E' ); + DefaultSymbolType nT = DefaultSymbolType ( 'T' ); + DefaultSymbolType nF = DefaultSymbolType ( 'F' ); - alphabet::Symbol tP = alphabet::Symbol ( '+' ); - alphabet::Symbol tS = alphabet::Symbol ( '*' ); - alphabet::Symbol tL = alphabet::Symbol ( '(' ); - alphabet::Symbol tR = alphabet::Symbol ( ')' ); - alphabet::Symbol tA = alphabet::Symbol ( 'a' ); + DefaultSymbolType tP = DefaultSymbolType ( '+' ); + DefaultSymbolType tS = DefaultSymbolType ( '*' ); + DefaultSymbolType tL = DefaultSymbolType ( '(' ); + DefaultSymbolType tR = DefaultSymbolType ( ')' ); + DefaultSymbolType tA = DefaultSymbolType ( 'a' ); grammar::CFG < > grammar ( nE ); - grammar.setTerminalAlphabet ( std::set < alphabet::Symbol > { tP, tS, tL, tR, tA } ); - grammar.setNonterminalAlphabet ( std::set < alphabet::Symbol > { nE, nT, nF } ); + grammar.setTerminalAlphabet ( std::set < DefaultSymbolType > { tP, tS, tL, tR, tA } ); + grammar.setNonterminalAlphabet ( std::set < DefaultSymbolType > { nE, nT, nF } ); - std::vector < alphabet::Symbol > rhsE1 ( { nE, tP, nT } ); - std::vector < alphabet::Symbol > rhsE2 ( { nT } ); - std::vector < alphabet::Symbol > rhsT1 ( { nT, tS, nF } ); - std::vector < alphabet::Symbol > rhsT2 ( { nF } ); - std::vector < alphabet::Symbol > rhsF1 ( { tA } ); - std::vector < alphabet::Symbol > rhsF2 ( { tL, nE, tR } ); + std::vector < DefaultSymbolType > rhsE1 ( { nE, tP, nT } ); + std::vector < DefaultSymbolType > rhsE2 ( { nT } ); + std::vector < DefaultSymbolType > rhsT1 ( { nT, tS, nF } ); + std::vector < DefaultSymbolType > rhsT2 ( { nF } ); + std::vector < DefaultSymbolType > rhsF1 ( { tA } ); + std::vector < DefaultSymbolType > rhsF2 ( { tL, nE, tR } ); grammar.addRule ( nE, rhsE1 ); grammar.addRule ( nE, rhsE2 ); @@ -44,30 +44,30 @@ void FirstTest::testFirst ( ) { grammar.addRule ( nF, rhsF2 ); // -------------------------------------------------- - std::map < std::vector < alphabet::Symbol >, std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > > first; + std::map < std::vector < DefaultSymbolType >, std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > > first; - first[rhsE1] = std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > { + first[rhsE1] = std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > { tA, tL }; - first[rhsE2] = std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > { + first[rhsE2] = std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > { tA, tL }; - first[rhsT1] = std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > { + first[rhsT1] = std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > { tA, tL }; - first[rhsT2] = std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > { + first[rhsT2] = std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > { tA, tL }; - first[rhsF1] = std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > { + first[rhsF1] = std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > { tA }; - first[rhsF2] = std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > { + first[rhsF2] = std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > { tL }; // -------------------------------------------------- - std::map < std::vector < alphabet::Symbol >, std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > > firstAlgo; + std::map < std::vector < DefaultSymbolType >, std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > > firstAlgo; for ( const auto & rule : grammar.getRawRules ( ) ) for ( const auto & rhs : rule.second ) @@ -77,38 +77,38 @@ void FirstTest::testFirst ( ) { } { - alphabet::Symbol nS = alphabet::Symbol ( 'S' ); - alphabet::Symbol nA = alphabet::Symbol ( 'A' ); - alphabet::Symbol nB = alphabet::Symbol ( 'B' ); - alphabet::Symbol nC = alphabet::Symbol ( 'C' ); - alphabet::Symbol nD = alphabet::Symbol ( 'D' ); - alphabet::Symbol nE = alphabet::Symbol ( 'E' ); - alphabet::Symbol nF = alphabet::Symbol ( 'F' ); - - alphabet::Symbol tA = alphabet::Symbol ( 'a' ); - alphabet::Symbol tB = alphabet::Symbol ( 'b' ); - alphabet::Symbol tC = alphabet::Symbol ( 'c' ); - alphabet::Symbol tD = alphabet::Symbol ( 'd' ); - alphabet::Symbol tE = alphabet::Symbol ( 'e' ); + DefaultSymbolType nS = DefaultSymbolType ( 'S' ); + DefaultSymbolType nA = DefaultSymbolType ( 'A' ); + DefaultSymbolType nB = DefaultSymbolType ( 'B' ); + DefaultSymbolType nC = DefaultSymbolType ( 'C' ); + DefaultSymbolType nD = DefaultSymbolType ( 'D' ); + DefaultSymbolType nE = DefaultSymbolType ( 'E' ); + DefaultSymbolType nF = DefaultSymbolType ( 'F' ); + + DefaultSymbolType tA = DefaultSymbolType ( 'a' ); + DefaultSymbolType tB = DefaultSymbolType ( 'b' ); + DefaultSymbolType tC = DefaultSymbolType ( 'c' ); + DefaultSymbolType tD = DefaultSymbolType ( 'd' ); + DefaultSymbolType tE = DefaultSymbolType ( 'e' ); grammar::CFG < > grammar ( nS ); - grammar.setTerminalAlphabet ( std::set < alphabet::Symbol > { tA, tB, tC, tD, tE } ); - grammar.setNonterminalAlphabet ( std::set < alphabet::Symbol > { nS, nA, nB, nC, nD, nE, nF } ); - - std::vector < alphabet::Symbol > rhsS1 ( { nB, tD, nS } ); - std::vector < alphabet::Symbol > rhsS2 ( { tD, tD, nC } ); - std::vector < alphabet::Symbol > rhsS3 ( { tC, nA } ); - std::vector < alphabet::Symbol > rhsA1 ( { tA, tE, nE } ); - std::vector < alphabet::Symbol > rhsA2 ( { tB, tB, nE } ); - std::vector < alphabet::Symbol > rhsB1 ( { tA, nF } ); - std::vector < alphabet::Symbol > rhsB2 ( { tB, tB, nD } ); - std::vector < alphabet::Symbol > rhsC1 ( { tA, nB, tD } ); - std::vector < alphabet::Symbol > rhsC2 ( { tE, nA } ); - std::vector < alphabet::Symbol > rhsD1 ( { tC, tA, nF } ); - std::vector < alphabet::Symbol > rhsE1 ( { tC, tA, tE, nE } ); - std::vector < alphabet::Symbol > rhsE2 ( { } ); - std::vector < alphabet::Symbol > rhsF1 ( { tE, nD } ); - std::vector < alphabet::Symbol > rhsF2 ( { } ); + grammar.setTerminalAlphabet ( std::set < DefaultSymbolType > { tA, tB, tC, tD, tE } ); + grammar.setNonterminalAlphabet ( std::set < DefaultSymbolType > { nS, nA, nB, nC, nD, nE, nF } ); + + std::vector < DefaultSymbolType > rhsS1 ( { nB, tD, nS } ); + std::vector < DefaultSymbolType > rhsS2 ( { tD, tD, nC } ); + std::vector < DefaultSymbolType > rhsS3 ( { tC, nA } ); + std::vector < DefaultSymbolType > rhsA1 ( { tA, tE, nE } ); + std::vector < DefaultSymbolType > rhsA2 ( { tB, tB, nE } ); + std::vector < DefaultSymbolType > rhsB1 ( { tA, nF } ); + std::vector < DefaultSymbolType > rhsB2 ( { tB, tB, nD } ); + std::vector < DefaultSymbolType > rhsC1 ( { tA, nB, tD } ); + std::vector < DefaultSymbolType > rhsC2 ( { tE, nA } ); + std::vector < DefaultSymbolType > rhsD1 ( { tC, tA, nF } ); + std::vector < DefaultSymbolType > rhsE1 ( { tC, tA, tE, nE } ); + std::vector < DefaultSymbolType > rhsE2 ( { } ); + std::vector < DefaultSymbolType > rhsF1 ( { tE, nD } ); + std::vector < DefaultSymbolType > rhsF2 ( { } ); grammar.addRule ( nS, rhsS1 ); grammar.addRule ( nS, rhsS2 ); @@ -126,54 +126,54 @@ void FirstTest::testFirst ( ) { grammar.addRule ( nF, rhsF2 ); // -------------------------------------------------- - std::map < std::vector < alphabet::Symbol >, std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > > first; + std::map < std::vector < DefaultSymbolType >, std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > > first; - first[rhsS1] = std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > { + first[rhsS1] = std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > { tA, tB }; - first[rhsS2] = std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > { + first[rhsS2] = std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > { tD }; - first[rhsS3] = std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > { + first[rhsS3] = std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > { tC }; - first[rhsA1] = std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > { + first[rhsA1] = std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > { tA }; - first[rhsA2] = std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > { + first[rhsA2] = std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > { tB }; - first[rhsB1] = std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > { + first[rhsB1] = std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > { tA }; - first[rhsB2] = std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > { + first[rhsB2] = std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > { tB }; - first[rhsC1] = std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > { + first[rhsC1] = std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > { tA }; - first[rhsC2] = std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > { + first[rhsC2] = std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > { tE }; - first[rhsD1] = std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > { + first[rhsD1] = std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > { tC }; - first[rhsE1] = std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > { + first[rhsE1] = std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > { tC }; - first[rhsE2] = std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > { + first[rhsE2] = std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > { string::Epsilon < >::EPSILON }; - first[rhsF1] = std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > { + first[rhsF1] = std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > { tE }; - first[rhsF2] = std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > { + first[rhsF2] = std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > { string::Epsilon < >::EPSILON }; // -------------------------------------------------- - std::map < std::vector < alphabet::Symbol >, std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > > firstAlgo; + std::map < std::vector < DefaultSymbolType >, std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > > firstAlgo; for ( const auto & rule : grammar.getRawRules ( ) ) for ( const auto & rhs : rule.second ) @@ -184,9 +184,9 @@ void FirstTest::testFirst ( ) { } void FirstTest::testFirst2 ( ) { - alphabet::Symbol A = alphabet::Symbol ( 'A' ); - alphabet::Symbol c = alphabet::Symbol ( 'c' ); - alphabet::Symbol d = alphabet::Symbol ( 'd' ); + DefaultSymbolType A = DefaultSymbolType ( 'A' ); + DefaultSymbolType c = DefaultSymbolType ( 'c' ); + DefaultSymbolType d = DefaultSymbolType ( 'd' ); grammar::CFG < > grammar ( A ); @@ -194,22 +194,22 @@ void FirstTest::testFirst2 ( ) { grammar.setNonterminalAlphabet ( { { A } } ); grammar.setInitialSymbol ( A ); - grammar.addRule ( A, std::vector < alphabet::Symbol > { A, c } ); - grammar.addRule ( A, std::vector < alphabet::Symbol > { d } ); + grammar.addRule ( A, std::vector < DefaultSymbolType > { A, c } ); + grammar.addRule ( A, std::vector < DefaultSymbolType > { d } ); - std::map < std::vector < alphabet::Symbol >, std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > > res = { { { d }, { d } }, { { A, c }, { d } } }; + std::map < std::vector < DefaultSymbolType >, std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > > res = { { { d }, { d } }, { { A, c }, { d } } }; CPPUNIT_ASSERT ( res == grammar::parsing::First::first ( grammar ) ); } void FirstTest::testFirst3 ( ) { - alphabet::Symbol S = alphabet::Symbol ( 'S' ); - alphabet::Symbol A = alphabet::Symbol ( 'A' ); - alphabet::Symbol B = alphabet::Symbol ( 'B' ); - alphabet::Symbol a = alphabet::Symbol ( 'a' ); - alphabet::Symbol b = alphabet::Symbol ( 'b' ); - alphabet::Symbol c = alphabet::Symbol ( 'c' ); - alphabet::Symbol d = alphabet::Symbol ( 'd' ); - alphabet::Symbol f = alphabet::Symbol ( 'f' ); + DefaultSymbolType S = DefaultSymbolType ( 'S' ); + DefaultSymbolType A = DefaultSymbolType ( 'A' ); + DefaultSymbolType B = DefaultSymbolType ( 'B' ); + DefaultSymbolType a = DefaultSymbolType ( 'a' ); + DefaultSymbolType b = DefaultSymbolType ( 'b' ); + DefaultSymbolType c = DefaultSymbolType ( 'c' ); + DefaultSymbolType d = DefaultSymbolType ( 'd' ); + DefaultSymbolType f = DefaultSymbolType ( 'f' ); grammar::CFG < > grammar ( S ); @@ -217,14 +217,14 @@ void FirstTest::testFirst3 ( ) { grammar.setNonterminalAlphabet ( { { S, A, B } } ); grammar.setInitialSymbol ( S ); - grammar.addRule ( S, std::vector < alphabet::Symbol > { A, a } ); - grammar.addRule ( S, std::vector < alphabet::Symbol > { b, S } ); - grammar.addRule ( A, std::vector < alphabet::Symbol > { c, A, d } ); - grammar.addRule ( A, std::vector < alphabet::Symbol > { B } ); - grammar.addRule ( B, std::vector < alphabet::Symbol > { f, S } ); - grammar.addRule ( B, std::vector < alphabet::Symbol > { } ); + grammar.addRule ( S, std::vector < DefaultSymbolType > { A, a } ); + grammar.addRule ( S, std::vector < DefaultSymbolType > { b, S } ); + grammar.addRule ( A, std::vector < DefaultSymbolType > { c, A, d } ); + grammar.addRule ( A, std::vector < DefaultSymbolType > { B } ); + grammar.addRule ( B, std::vector < DefaultSymbolType > { f, S } ); + grammar.addRule ( B, std::vector < DefaultSymbolType > { } ); - std::map < std::vector < alphabet::Symbol >, std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > > res = + std::map < std::vector < DefaultSymbolType >, std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > > res = { { { A, a }, { c, f, a } }, { { b, S }, { b } }, { { c, A, d }, { c } }, { { B }, { f, string::Epsilon < >::EPSILON } }, { { f, S }, { f } }, { { }, { string::Epsilon < >::EPSILON } } }; diff --git a/alib2algo_experimental/test-src/grammar/parsing/FollowTest.cpp b/alib2algo_experimental/test-src/grammar/parsing/FollowTest.cpp index 5bb5c81fe4..d416735cba 100644 --- a/alib2algo_experimental/test-src/grammar/parsing/FollowTest.cpp +++ b/alib2algo_experimental/test-src/grammar/parsing/FollowTest.cpp @@ -15,26 +15,26 @@ void FollowTest::tearDown ( ) { void FollowTest::testFollow ( ) { { - alphabet::Symbol nE = alphabet::Symbol ( 'E' ); - alphabet::Symbol nT = alphabet::Symbol ( 'T' ); - alphabet::Symbol nF = alphabet::Symbol ( 'F' ); + DefaultSymbolType nE = DefaultSymbolType ( 'E' ); + DefaultSymbolType nT = DefaultSymbolType ( 'T' ); + DefaultSymbolType nF = DefaultSymbolType ( 'F' ); - alphabet::Symbol tP = alphabet::Symbol ( '+' ); - alphabet::Symbol tS = alphabet::Symbol ( '*' ); - alphabet::Symbol tL = alphabet::Symbol ( '(' ); - alphabet::Symbol tR = alphabet::Symbol ( ')' ); - alphabet::Symbol tA = alphabet::Symbol ( 'a' ); + DefaultSymbolType tP = DefaultSymbolType ( '+' ); + DefaultSymbolType tS = DefaultSymbolType ( '*' ); + DefaultSymbolType tL = DefaultSymbolType ( '(' ); + DefaultSymbolType tR = DefaultSymbolType ( ')' ); + DefaultSymbolType tA = DefaultSymbolType ( 'a' ); grammar::CFG < > grammar ( nE ); - grammar.setTerminalAlphabet ( std::set < alphabet::Symbol > { tP, tS, tL, tR, tA } ); - grammar.setNonterminalAlphabet ( std::set < alphabet::Symbol > { nE, nT, nF } ); + grammar.setTerminalAlphabet ( std::set < DefaultSymbolType > { tP, tS, tL, tR, tA } ); + grammar.setNonterminalAlphabet ( std::set < DefaultSymbolType > { nE, nT, nF } ); - std::vector < alphabet::Symbol > rhsE1 ( { nE, tP, nT } ); - std::vector < alphabet::Symbol > rhsE2 ( { nT } ); - std::vector < alphabet::Symbol > rhsT1 ( { nT, tS, nF } ); - std::vector < alphabet::Symbol > rhsT2 ( { nF } ); - std::vector < alphabet::Symbol > rhsF1 ( { tA } ); - std::vector < alphabet::Symbol > rhsF2 ( { tL, nE, tR } ); + std::vector < DefaultSymbolType > rhsE1 ( { nE, tP, nT } ); + std::vector < DefaultSymbolType > rhsE2 ( { nT } ); + std::vector < DefaultSymbolType > rhsT1 ( { nT, tS, nF } ); + std::vector < DefaultSymbolType > rhsT2 ( { nF } ); + std::vector < DefaultSymbolType > rhsF1 ( { tA } ); + std::vector < DefaultSymbolType > rhsF2 ( { tL, nE, tR } ); grammar.addRule ( nE, rhsE1 ); grammar.addRule ( nE, rhsE2 ); @@ -44,21 +44,21 @@ void FollowTest::testFollow ( ) { grammar.addRule ( nF, rhsF2 ); // -------------------------------------------------- - std::map < alphabet::Symbol, std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > > follow; + std::map < DefaultSymbolType, std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > > follow; - follow[nE] = std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > { + follow[nE] = std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > { string::Epsilon < >::EPSILON, tP, tR }; - follow[nT] = std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > { + follow[nT] = std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > { string::Epsilon < >::EPSILON, tP, tR, tS }; - follow[nF] = std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > { + follow[nF] = std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > { string::Epsilon < >::EPSILON, tP, tR, tS }; // -------------------------------------------------- - std::map < alphabet::Symbol, std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > > followAlgo; + std::map < DefaultSymbolType, std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > > followAlgo; for ( const auto & nt : grammar.getNonterminalAlphabet ( ) ) followAlgo[nt] = grammar::parsing::Follow::follow ( grammar, nt ); @@ -69,38 +69,38 @@ void FollowTest::testFollow ( ) { } { - alphabet::Symbol nS = alphabet::Symbol ( 'S' ); - alphabet::Symbol nA = alphabet::Symbol ( 'A' ); - alphabet::Symbol nB = alphabet::Symbol ( 'B' ); - alphabet::Symbol nC = alphabet::Symbol ( 'C' ); - alphabet::Symbol nD = alphabet::Symbol ( 'D' ); - alphabet::Symbol nE = alphabet::Symbol ( 'E' ); - alphabet::Symbol nF = alphabet::Symbol ( 'F' ); - - alphabet::Symbol tA = alphabet::Symbol ( 'a' ); - alphabet::Symbol tB = alphabet::Symbol ( 'b' ); - alphabet::Symbol tC = alphabet::Symbol ( 'c' ); - alphabet::Symbol tD = alphabet::Symbol ( 'd' ); - alphabet::Symbol tE = alphabet::Symbol ( 'e' ); + DefaultSymbolType nS = DefaultSymbolType ( 'S' ); + DefaultSymbolType nA = DefaultSymbolType ( 'A' ); + DefaultSymbolType nB = DefaultSymbolType ( 'B' ); + DefaultSymbolType nC = DefaultSymbolType ( 'C' ); + DefaultSymbolType nD = DefaultSymbolType ( 'D' ); + DefaultSymbolType nE = DefaultSymbolType ( 'E' ); + DefaultSymbolType nF = DefaultSymbolType ( 'F' ); + + DefaultSymbolType tA = DefaultSymbolType ( 'a' ); + DefaultSymbolType tB = DefaultSymbolType ( 'b' ); + DefaultSymbolType tC = DefaultSymbolType ( 'c' ); + DefaultSymbolType tD = DefaultSymbolType ( 'd' ); + DefaultSymbolType tE = DefaultSymbolType ( 'e' ); grammar::CFG < > grammar ( nS ); - grammar.setTerminalAlphabet ( std::set < alphabet::Symbol > { tA, tB, tC, tD, tE } ); - grammar.setNonterminalAlphabet ( std::set < alphabet::Symbol > { nS, nA, nB, nC, nD, nE, nF } ); - - std::vector < alphabet::Symbol > rhsS1 ( { nB, tD, nS } ); - std::vector < alphabet::Symbol > rhsS2 ( { tD, tD, nC } ); - std::vector < alphabet::Symbol > rhsS3 ( { tC, nA } ); - std::vector < alphabet::Symbol > rhsA1 ( { tA, tE, nE } ); - std::vector < alphabet::Symbol > rhsA2 ( { tB, tB, nE } ); - std::vector < alphabet::Symbol > rhsB1 ( { tA, nF } ); - std::vector < alphabet::Symbol > rhsB2 ( { tB, tB, nD } ); - std::vector < alphabet::Symbol > rhsC1 ( { tA, nB, tD } ); - std::vector < alphabet::Symbol > rhsC2 ( { tE, nA } ); - std::vector < alphabet::Symbol > rhsD1 ( { tC, tA, nF } ); - std::vector < alphabet::Symbol > rhsE1 ( { tC, tA, tE, nE } ); - std::vector < alphabet::Symbol > rhsE2 ( { } ); - std::vector < alphabet::Symbol > rhsF1 ( { tE, nD } ); - std::vector < alphabet::Symbol > rhsF2 ( { } ); + grammar.setTerminalAlphabet ( std::set < DefaultSymbolType > { tA, tB, tC, tD, tE } ); + grammar.setNonterminalAlphabet ( std::set < DefaultSymbolType > { nS, nA, nB, nC, nD, nE, nF } ); + + std::vector < DefaultSymbolType > rhsS1 ( { nB, tD, nS } ); + std::vector < DefaultSymbolType > rhsS2 ( { tD, tD, nC } ); + std::vector < DefaultSymbolType > rhsS3 ( { tC, nA } ); + std::vector < DefaultSymbolType > rhsA1 ( { tA, tE, nE } ); + std::vector < DefaultSymbolType > rhsA2 ( { tB, tB, nE } ); + std::vector < DefaultSymbolType > rhsB1 ( { tA, nF } ); + std::vector < DefaultSymbolType > rhsB2 ( { tB, tB, nD } ); + std::vector < DefaultSymbolType > rhsC1 ( { tA, nB, tD } ); + std::vector < DefaultSymbolType > rhsC2 ( { tE, nA } ); + std::vector < DefaultSymbolType > rhsD1 ( { tC, tA, nF } ); + std::vector < DefaultSymbolType > rhsE1 ( { tC, tA, tE, nE } ); + std::vector < DefaultSymbolType > rhsE2 ( { } ); + std::vector < DefaultSymbolType > rhsF1 ( { tE, nD } ); + std::vector < DefaultSymbolType > rhsF2 ( { } ); grammar.addRule ( nS, rhsS1 ); grammar.addRule ( nS, rhsS2 ); @@ -118,33 +118,33 @@ void FollowTest::testFollow ( ) { grammar.addRule ( nF, rhsF2 ); // -------------------------------------------------- - std::map < alphabet::Symbol, std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > > follow; + std::map < DefaultSymbolType, std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > > follow; - follow[nS] = std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > { + follow[nS] = std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > { string::Epsilon < >::EPSILON }; - follow[nA] = std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > { + follow[nA] = std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > { string::Epsilon < >::EPSILON }; - follow[nB] = std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > { + follow[nB] = std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > { tD }; - follow[nC] = std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > { + follow[nC] = std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > { string::Epsilon < >::EPSILON }; - follow[nD] = std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > { + follow[nD] = std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > { tD }; - follow[nE] = std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > { + follow[nE] = std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > { string::Epsilon < >::EPSILON }; - follow[nF] = std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > { + follow[nF] = std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > { tD }; // -------------------------------------------------- - std::map < alphabet::Symbol, std::set < std::variant < alphabet::Symbol, string::Epsilon < > > > > followAlgo; + std::map < DefaultSymbolType, std::set < std::variant < DefaultSymbolType, string::Epsilon < > > > > followAlgo; for ( const auto & nt : grammar.getNonterminalAlphabet ( ) ) followAlgo[nt] = grammar::parsing::Follow::follow ( grammar, nt ); diff --git a/alib2algo_experimental/test-src/grammar/parsing/HandleFirstFirstConflict.cpp b/alib2algo_experimental/test-src/grammar/parsing/HandleFirstFirstConflict.cpp index d45ab78f41..985f440458 100644 --- a/alib2algo_experimental/test-src/grammar/parsing/HandleFirstFirstConflict.cpp +++ b/alib2algo_experimental/test-src/grammar/parsing/HandleFirstFirstConflict.cpp @@ -13,15 +13,15 @@ void HandleFirstFirstConflict::tearDown ( ) { } void HandleFirstFirstConflict::testHandleFirstFirstConflict ( ) { - alphabet::Symbol A = alphabet::Symbol ( "A" ); - alphabet::Symbol Ap = alphabet::Symbol ( "A'" ); - alphabet::Symbol B = alphabet::Symbol ( "B" ); - alphabet::Symbol C = alphabet::Symbol ( "C" ); + DefaultSymbolType A = DefaultSymbolType ( "A" ); + DefaultSymbolType Ap = DefaultSymbolType ( "A'" ); + DefaultSymbolType B = DefaultSymbolType ( "B" ); + DefaultSymbolType C = DefaultSymbolType ( "C" ); - alphabet::Symbol a = alphabet::Symbol ( 'a' ); - alphabet::Symbol b = alphabet::Symbol ( 'b' ); - alphabet::Symbol c = alphabet::Symbol ( 'c' ); - alphabet::Symbol d = alphabet::Symbol ( 'd' ); + DefaultSymbolType a = DefaultSymbolType ( 'a' ); + DefaultSymbolType b = DefaultSymbolType ( 'b' ); + DefaultSymbolType c = DefaultSymbolType ( 'c' ); + DefaultSymbolType d = DefaultSymbolType ( 'd' ); grammar::CFG < > grammar ( A ); @@ -29,12 +29,12 @@ void HandleFirstFirstConflict::testHandleFirstFirstConflict ( ) { grammar.setNonterminalAlphabet ( { A, B, C } ); grammar.setInitialSymbol ( A ); - grammar.addRule ( A, std::vector < alphabet::Symbol > { a, B } ); - grammar.addRule ( A, std::vector < alphabet::Symbol > { C, B } ); - grammar.addRule ( B, std::vector < alphabet::Symbol > { c, B } ); - grammar.addRule ( B, std::vector < alphabet::Symbol > { d } ); - grammar.addRule ( C, std::vector < alphabet::Symbol > { a, C } ); - grammar.addRule ( C, std::vector < alphabet::Symbol > { b, B } ); + grammar.addRule ( A, std::vector < DefaultSymbolType > { a, B } ); + grammar.addRule ( A, std::vector < DefaultSymbolType > { C, B } ); + grammar.addRule ( B, std::vector < DefaultSymbolType > { c, B } ); + grammar.addRule ( B, std::vector < DefaultSymbolType > { d } ); + grammar.addRule ( C, std::vector < DefaultSymbolType > { a, C } ); + grammar.addRule ( C, std::vector < DefaultSymbolType > { b, B } ); grammar::CFG < > res = grammar; grammar::parsing::HandleFirstFirstConflict::handleFirstFirstConflict ( res, a, A, { {a, B }, {C ,B} } ); @@ -45,13 +45,13 @@ void HandleFirstFirstConflict::testHandleFirstFirstConflict ( ) { comp.setNonterminalAlphabet ( { A, B, C } ); comp.setInitialSymbol ( A ); - comp.addRule ( A, std::vector < alphabet::Symbol > { a, B } ); - comp.addRule ( A, std::vector < alphabet::Symbol > { a, C, B } ); - comp.addRule ( A, std::vector < alphabet::Symbol > { b, B, B } ); - comp.addRule ( B, std::vector < alphabet::Symbol > { c, B } ); - comp.addRule ( B, std::vector < alphabet::Symbol > { d } ); - comp.addRule ( C, std::vector < alphabet::Symbol > { a, C } ); - comp.addRule ( C, std::vector < alphabet::Symbol > { b, B } ); + comp.addRule ( A, std::vector < DefaultSymbolType > { a, B } ); + comp.addRule ( A, std::vector < DefaultSymbolType > { a, C, B } ); + comp.addRule ( A, std::vector < DefaultSymbolType > { b, B, B } ); + comp.addRule ( B, std::vector < DefaultSymbolType > { c, B } ); + comp.addRule ( B, std::vector < DefaultSymbolType > { d } ); + comp.addRule ( C, std::vector < DefaultSymbolType > { a, C } ); + comp.addRule ( C, std::vector < DefaultSymbolType > { b, B } ); // std::cout << res << std::endl << comp << std::endl; @@ -65,14 +65,14 @@ void HandleFirstFirstConflict::testHandleFirstFirstConflict ( ) { comp2.setNonterminalAlphabet ( { A, Ap, B, C } ); comp2.setInitialSymbol ( A ); - comp2.addRule ( A, std::vector < alphabet::Symbol > { a, Ap } ); - comp2.addRule ( A, std::vector < alphabet::Symbol > { b, B, B } ); - comp2.addRule ( Ap, std::vector < alphabet::Symbol > { B } ); - comp2.addRule ( Ap, std::vector < alphabet::Symbol > { C, B } ); - comp2.addRule ( B, std::vector < alphabet::Symbol > { c, B } ); - comp2.addRule ( B, std::vector < alphabet::Symbol > { d } ); - comp2.addRule ( C, std::vector < alphabet::Symbol > { a, C } ); - comp2.addRule ( C, std::vector < alphabet::Symbol > { b, B } ); + comp2.addRule ( A, std::vector < DefaultSymbolType > { a, Ap } ); + comp2.addRule ( A, std::vector < DefaultSymbolType > { b, B, B } ); + comp2.addRule ( Ap, std::vector < DefaultSymbolType > { B } ); + comp2.addRule ( Ap, std::vector < DefaultSymbolType > { C, B } ); + comp2.addRule ( B, std::vector < DefaultSymbolType > { c, B } ); + comp2.addRule ( B, std::vector < DefaultSymbolType > { d } ); + comp2.addRule ( C, std::vector < DefaultSymbolType > { a, C } ); + comp2.addRule ( C, std::vector < DefaultSymbolType > { b, B } ); // std::cout << res << std::endl << comp2 << std::endl; @@ -80,15 +80,15 @@ void HandleFirstFirstConflict::testHandleFirstFirstConflict ( ) { } void HandleFirstFirstConflict::testHandleFirstFirstConflict2 ( ) { - alphabet::Symbol A = alphabet::Symbol ( "A" ); - alphabet::Symbol Ap = alphabet::Symbol ( "A'" ); - alphabet::Symbol B = alphabet::Symbol ( "B" ); - alphabet::Symbol C = alphabet::Symbol ( "C" ); + DefaultSymbolType A = DefaultSymbolType ( "A" ); + DefaultSymbolType Ap = DefaultSymbolType ( "A'" ); + DefaultSymbolType B = DefaultSymbolType ( "B" ); + DefaultSymbolType C = DefaultSymbolType ( "C" ); - alphabet::Symbol a = alphabet::Symbol ( 'a' ); - alphabet::Symbol b = alphabet::Symbol ( 'b' ); - alphabet::Symbol c = alphabet::Symbol ( 'c' ); - alphabet::Symbol d = alphabet::Symbol ( 'd' ); + DefaultSymbolType a = DefaultSymbolType ( 'a' ); + DefaultSymbolType b = DefaultSymbolType ( 'b' ); + DefaultSymbolType c = DefaultSymbolType ( 'c' ); + DefaultSymbolType d = DefaultSymbolType ( 'd' ); grammar::CFG < > grammar ( A ); @@ -96,12 +96,12 @@ void HandleFirstFirstConflict::testHandleFirstFirstConflict2 ( ) { grammar.setNonterminalAlphabet ( { A, B, C } ); grammar.setInitialSymbol ( A ); - grammar.addRule ( A, std::vector < alphabet::Symbol > { a, B } ); - grammar.addRule ( A, std::vector < alphabet::Symbol > { C, B } ); - grammar.addRule ( B, std::vector < alphabet::Symbol > { c, B } ); - grammar.addRule ( B, std::vector < alphabet::Symbol > { a } ); - grammar.addRule ( C, std::vector < alphabet::Symbol > { a, C } ); - grammar.addRule ( C, std::vector < alphabet::Symbol > { B, b } ); + grammar.addRule ( A, std::vector < DefaultSymbolType > { a, B } ); + grammar.addRule ( A, std::vector < DefaultSymbolType > { C, B } ); + grammar.addRule ( B, std::vector < DefaultSymbolType > { c, B } ); + grammar.addRule ( B, std::vector < DefaultSymbolType > { a } ); + grammar.addRule ( C, std::vector < DefaultSymbolType > { a, C } ); + grammar.addRule ( C, std::vector < DefaultSymbolType > { B, b } ); grammar::CFG < > res = grammar; grammar::parsing::HandleFirstFirstConflict::handleFirstFirstConflict ( res, a, A, { {a, B}, {C, B}}); @@ -113,14 +113,14 @@ void HandleFirstFirstConflict::testHandleFirstFirstConflict2 ( ) { comp.setNonterminalAlphabet ( { A, B, C } ); comp.setInitialSymbol ( A ); - comp.addRule ( A, std::vector < alphabet::Symbol > { a, B } ); - comp.addRule ( A, std::vector < alphabet::Symbol > { a, C, B } ); - comp.addRule ( A, std::vector < alphabet::Symbol > { a, b, B } ); - comp.addRule ( A, std::vector < alphabet::Symbol > { c, B, b, B } ); - comp.addRule ( B, std::vector < alphabet::Symbol > { c, B } ); - comp.addRule ( B, std::vector < alphabet::Symbol > { a } ); - comp.addRule ( C, std::vector < alphabet::Symbol > { a, C } ); - comp.addRule ( C, std::vector < alphabet::Symbol > { B, b } ); + comp.addRule ( A, std::vector < DefaultSymbolType > { a, B } ); + comp.addRule ( A, std::vector < DefaultSymbolType > { a, C, B } ); + comp.addRule ( A, std::vector < DefaultSymbolType > { a, b, B } ); + comp.addRule ( A, std::vector < DefaultSymbolType > { c, B, b, B } ); + comp.addRule ( B, std::vector < DefaultSymbolType > { c, B } ); + comp.addRule ( B, std::vector < DefaultSymbolType > { a } ); + comp.addRule ( C, std::vector < DefaultSymbolType > { a, C } ); + comp.addRule ( C, std::vector < DefaultSymbolType > { B, b } ); // std::cout << res << std::endl << comp << std::endl; @@ -134,15 +134,15 @@ void HandleFirstFirstConflict::testHandleFirstFirstConflict2 ( ) { comp2.setNonterminalAlphabet ( { A, Ap, B, C } ); comp2.setInitialSymbol ( A ); - comp2.addRule ( A, std::vector < alphabet::Symbol > { a, Ap } ); - comp2.addRule ( A, std::vector < alphabet::Symbol > { c, B, b, B } ); - comp2.addRule ( Ap, std::vector < alphabet::Symbol > { B } ); - comp2.addRule ( Ap, std::vector < alphabet::Symbol > { C, B } ); - comp2.addRule ( Ap, std::vector < alphabet::Symbol > { b, B } ); - comp2.addRule ( B, std::vector < alphabet::Symbol > { c, B } ); - comp2.addRule ( B, std::vector < alphabet::Symbol > { a } ); - comp2.addRule ( C, std::vector < alphabet::Symbol > { a, C } ); - comp2.addRule ( C, std::vector < alphabet::Symbol > { B, b } ); + comp2.addRule ( A, std::vector < DefaultSymbolType > { a, Ap } ); + comp2.addRule ( A, std::vector < DefaultSymbolType > { c, B, b, B } ); + comp2.addRule ( Ap, std::vector < DefaultSymbolType > { B } ); + comp2.addRule ( Ap, std::vector < DefaultSymbolType > { C, B } ); + comp2.addRule ( Ap, std::vector < DefaultSymbolType > { b, B } ); + comp2.addRule ( B, std::vector < DefaultSymbolType > { c, B } ); + comp2.addRule ( B, std::vector < DefaultSymbolType > { a } ); + comp2.addRule ( C, std::vector < DefaultSymbolType > { a, C } ); + comp2.addRule ( C, std::vector < DefaultSymbolType > { B, b } ); // std::cout << res << std::endl << comp2 << std::endl; diff --git a/alib2algo_experimental/test-src/grammar/parsing/LL1ParseTable.cpp b/alib2algo_experimental/test-src/grammar/parsing/LL1ParseTable.cpp index cde797aabd..9080e3e7f7 100644 --- a/alib2algo_experimental/test-src/grammar/parsing/LL1ParseTable.cpp +++ b/alib2algo_experimental/test-src/grammar/parsing/LL1ParseTable.cpp @@ -15,30 +15,30 @@ void LL1ParseTable::tearDown ( ) { void LL1ParseTable::testLL1Table ( ) { { - alphabet::Symbol nE = alphabet::Symbol ( 'E' ); - alphabet::Symbol nEp = alphabet::Symbol ( "E'" ); - alphabet::Symbol nT = alphabet::Symbol ( 'T' ); - alphabet::Symbol nTp = alphabet::Symbol ( "T'" ); - alphabet::Symbol nF = alphabet::Symbol ( 'F' ); - - alphabet::Symbol tP = alphabet::Symbol ( '+' ); - alphabet::Symbol tS = alphabet::Symbol ( '*' ); - alphabet::Symbol tL = alphabet::Symbol ( '(' ); - alphabet::Symbol tR = alphabet::Symbol ( ')' ); - alphabet::Symbol tA = alphabet::Symbol ( 'a' ); + DefaultSymbolType nE = DefaultSymbolType ( 'E' ); + DefaultSymbolType nEp = DefaultSymbolType ( "E'" ); + DefaultSymbolType nT = DefaultSymbolType ( 'T' ); + DefaultSymbolType nTp = DefaultSymbolType ( "T'" ); + DefaultSymbolType nF = DefaultSymbolType ( 'F' ); + + DefaultSymbolType tP = DefaultSymbolType ( '+' ); + DefaultSymbolType tS = DefaultSymbolType ( '*' ); + DefaultSymbolType tL = DefaultSymbolType ( '(' ); + DefaultSymbolType tR = DefaultSymbolType ( ')' ); + DefaultSymbolType tA = DefaultSymbolType ( 'a' ); grammar::CFG < > grammar ( nE ); - grammar.setTerminalAlphabet ( std::set < alphabet::Symbol > { tP, tS, tL, tR, tA } ); - grammar.setNonterminalAlphabet ( std::set < alphabet::Symbol > { nE, nEp, nT, nTp, nF } ); - - std::vector < alphabet::Symbol > rhsE1 ( { nT, nEp } ); - std::vector < alphabet::Symbol > rhsEp1 ( { tP, nT, nEp } ); - std::vector < alphabet::Symbol > rhsEp2 ( { } ); - std::vector < alphabet::Symbol > rhsT1 ( { nF, nTp } ); - std::vector < alphabet::Symbol > rhsTp1 ( { tS, nF, nTp } ); - std::vector < alphabet::Symbol > rhsTp2 ( { } ); - std::vector < alphabet::Symbol > rhsF1 ( { tA } ); - std::vector < alphabet::Symbol > rhsF2 ( { tL, nE, tR } ); + grammar.setTerminalAlphabet ( std::set < DefaultSymbolType > { tP, tS, tL, tR, tA } ); + grammar.setNonterminalAlphabet ( std::set < DefaultSymbolType > { nE, nEp, nT, nTp, nF } ); + + std::vector < DefaultSymbolType > rhsE1 ( { nT, nEp } ); + std::vector < DefaultSymbolType > rhsEp1 ( { tP, nT, nEp } ); + std::vector < DefaultSymbolType > rhsEp2 ( { } ); + std::vector < DefaultSymbolType > rhsT1 ( { nF, nTp } ); + std::vector < DefaultSymbolType > rhsTp1 ( { tS, nF, nTp } ); + std::vector < DefaultSymbolType > rhsTp2 ( { } ); + std::vector < DefaultSymbolType > rhsF1 ( { tA } ); + std::vector < DefaultSymbolType > rhsF2 ( { tL, nE, tR } ); grammar.addRule ( nE, rhsE1 ); grammar.addRule ( nEp, rhsEp1 ); @@ -50,7 +50,7 @@ void LL1ParseTable::testLL1Table ( ) { grammar.addRule ( nF, rhsF2 ); // -------------------------------------------------- - std::map < std::pair < std::variant < alphabet::Symbol, string::Epsilon < > >, alphabet::Symbol >, std::set < std::vector < alphabet::Symbol > > > parseTable; + std::map < std::pair < std::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, std::set < std::vector < DefaultSymbolType > > > parseTable; parseTable[std::make_pair ( tA, nE )].insert ( rhsE1 ); parseTable[std::make_pair ( tL, nE )].insert ( rhsE1 ); @@ -72,7 +72,7 @@ void LL1ParseTable::testLL1Table ( ) { // -------------------------------------------------- - std::map < std::pair < std::variant < alphabet::Symbol, string::Epsilon < > >, alphabet::Symbol >, std::set < std::vector < alphabet::Symbol > > > parseTableAlgo = grammar::parsing::LL1ParseTable::parseTable ( grammar ); + std::map < std::pair < std::variant < DefaultSymbolType, string::Epsilon < > >, DefaultSymbolType >, std::set < std::vector < DefaultSymbolType > > > parseTableAlgo = grammar::parsing::LL1ParseTable::parseTable ( grammar ); std::cout << parseTable << std::endl; std::cout << parseTableAlgo << std::endl; diff --git a/alib2algo_experimental/test-src/grammar/parsing/LR0Parser.cpp b/alib2algo_experimental/test-src/grammar/parsing/LR0Parser.cpp index 9658e79753..5a4aeea2e0 100644 --- a/alib2algo_experimental/test-src/grammar/parsing/LR0Parser.cpp +++ b/alib2algo_experimental/test-src/grammar/parsing/LR0Parser.cpp @@ -304,16 +304,16 @@ void LR0Parser::testAutomaton ( ) { } }; - std::vector < label::Label > states; + std::vector < DefaultStateType > states; for (const grammar::parsing::LR0Items & currentItems : items) { - states.emplace_back( label::Label ( label::LR0ItemsLabel ( currentItems ) ) ); + states.emplace_back( DefaultStateType ( label::LR0ItemsLabel ( currentItems ) ) ); } automaton::DFA<> parsingAutomaton ( states[0] ); parsingAutomaton.addInputSymbols ( augmentedExpressionGrammar.getNonterminalAlphabet ( ) ); parsingAutomaton.addInputSymbols ( augmentedExpressionGrammar.getTerminalAlphabet ( ) ); - for (const label::Label & state : states) { + for (const DefaultStateType & state : states) { parsingAutomaton.addState(state); } diff --git a/alib2algo_experimental/test-src/grammar/parsing/LR0Parser.h b/alib2algo_experimental/test-src/grammar/parsing/LR0Parser.h index df14f529d3..19accf11cd 100644 --- a/alib2algo_experimental/test-src/grammar/parsing/LR0Parser.h +++ b/alib2algo_experimental/test-src/grammar/parsing/LR0Parser.h @@ -13,15 +13,15 @@ class LR0Parser : public CppUnit::TestFixture { CPPUNIT_TEST ( testAutomaton ); CPPUNIT_TEST_SUITE_END ( ); - alphabet::Symbol E = alphabet::Symbol ( 'E' ); - alphabet::Symbol T = alphabet::Symbol ( 'T' ); - alphabet::Symbol F = alphabet::Symbol ( 'F' ); - - alphabet::Symbol plus = alphabet::Symbol ( '+' ); - alphabet::Symbol times = alphabet::Symbol ( '*' ); - alphabet::Symbol leftParenthesis = alphabet::Symbol ( '(' ); - alphabet::Symbol rightParenthesis = alphabet::Symbol ( ')' ); - alphabet::Symbol identifier = alphabet::Symbol ( "id" ); + DefaultSymbolType E = DefaultSymbolType ( 'E' ); + DefaultSymbolType T = DefaultSymbolType ( 'T' ); + DefaultSymbolType F = DefaultSymbolType ( 'F' ); + + DefaultSymbolType plus = DefaultSymbolType ( '+' ); + DefaultSymbolType times = DefaultSymbolType ( '*' ); + DefaultSymbolType leftParenthesis = DefaultSymbolType ( '(' ); + DefaultSymbolType rightParenthesis = DefaultSymbolType ( ')' ); + DefaultSymbolType identifier = DefaultSymbolType ( "id" ); grammar::CFG < > getExpressionGrammar ( ); diff --git a/alib2algo_experimental/test-src/grammar/parsing/LRParser.cpp b/alib2algo_experimental/test-src/grammar/parsing/LRParser.cpp index ccaf9e707c..741ec709d3 100644 --- a/alib2algo_experimental/test-src/grammar/parsing/LRParser.cpp +++ b/alib2algo_experimental/test-src/grammar/parsing/LRParser.cpp @@ -35,7 +35,7 @@ grammar::CFG < > LRParser::getExpressionGrammar() { void LRParser::testEndOfInputSymbol ( ) { grammar::CFG < > expressionGrammar = getExpressionGrammar ( ); - alphabet::Symbol endOfInput = grammar::parsing::LRParser::getEndOfInputSymbol ( expressionGrammar ); + DefaultSymbolType endOfInput = grammar::parsing::LRParser::getEndOfInputSymbol ( expressionGrammar ); bool correct = true; if ( expressionGrammar.getTerminalAlphabet ( ) . find ( endOfInput ) != expressionGrammar.getTerminalAlphabet ( ) . end ( ) ) { @@ -52,7 +52,7 @@ void LRParser::testEndOfInputSymbol ( ) { void LRParser::testParseCorrectInput ( ) { grammar::CFG < > augmentedExpressionGrammar = grammar::parsing::LRParser::getAugmentedGrammar ( getExpressionGrammar ( ) ); - label::Label initialState ( label::Label ( label::LR0ItemsLabel ( { + DefaultStateType initialState ( DefaultStateType ( label::LR0ItemsLabel ( { { augmentedExpressionGrammar.getInitialSymbol(), { @@ -82,7 +82,7 @@ void LRParser::testParseCorrectInput ( ) { } } ) ) ); - std::vector < alphabet::Symbol > correctInput { + std::vector < DefaultSymbolType > correctInput { leftParenthesis, identifier, plus, @@ -108,7 +108,7 @@ void LRParser::testParseCorrectInput ( ) { void LRParser::testParseIncorrectInput ( ) { grammar::CFG < > augmentedExpressionGrammar = grammar::parsing::LRParser::getAugmentedGrammar ( getExpressionGrammar ( ) ); - label::Label initialState ( label::Label ( label::LR0ItemsLabel ( { + DefaultStateType initialState ( DefaultStateType ( label::LR0ItemsLabel ( { { augmentedExpressionGrammar.getInitialSymbol(), { @@ -138,7 +138,7 @@ void LRParser::testParseIncorrectInput ( ) { } } ) ) ); - std::vector < alphabet::Symbol > incorrectInput { + std::vector < DefaultSymbolType > incorrectInput { leftParenthesis, identifier, plus, diff --git a/alib2algo_experimental/test-src/grammar/parsing/LRParser.h b/alib2algo_experimental/test-src/grammar/parsing/LRParser.h index 39fe6254ad..6d3d3cb7a7 100644 --- a/alib2algo_experimental/test-src/grammar/parsing/LRParser.h +++ b/alib2algo_experimental/test-src/grammar/parsing/LRParser.h @@ -13,15 +13,15 @@ class LRParser : public CppUnit::TestFixture { CPPUNIT_TEST ( testParseIncorrectInput ); CPPUNIT_TEST_SUITE_END ( ); - alphabet::Symbol E = alphabet::Symbol ( 'E' ); - alphabet::Symbol T = alphabet::Symbol ( 'T' ); - alphabet::Symbol F = alphabet::Symbol ( 'F' ); - - alphabet::Symbol plus = alphabet::Symbol ( '+' ); - alphabet::Symbol times = alphabet::Symbol ( '*' ); - alphabet::Symbol leftParenthesis = alphabet::Symbol ( '(' ); - alphabet::Symbol rightParenthesis = alphabet::Symbol ( ')' ); - alphabet::Symbol identifier = alphabet::Symbol ( "id" ); + DefaultSymbolType E = DefaultSymbolType ( 'E' ); + DefaultSymbolType T = DefaultSymbolType ( 'T' ); + DefaultSymbolType F = DefaultSymbolType ( 'F' ); + + DefaultSymbolType plus = DefaultSymbolType ( '+' ); + DefaultSymbolType times = DefaultSymbolType ( '*' ); + DefaultSymbolType leftParenthesis = DefaultSymbolType ( '(' ); + DefaultSymbolType rightParenthesis = DefaultSymbolType ( ')' ); + DefaultSymbolType identifier = DefaultSymbolType ( "id" ); grammar::CFG < > getExpressionGrammar ( ); diff --git a/alib2algo_experimental/test-src/grammar/parsing/LeftFactorize.cpp b/alib2algo_experimental/test-src/grammar/parsing/LeftFactorize.cpp index e421bf65da..a528de5565 100644 --- a/alib2algo_experimental/test-src/grammar/parsing/LeftFactorize.cpp +++ b/alib2algo_experimental/test-src/grammar/parsing/LeftFactorize.cpp @@ -13,16 +13,16 @@ void LeftFactorize::tearDown ( ) { } void LeftFactorize::testLeftFactorize ( ) { - alphabet::Symbol S = alphabet::Symbol ( "S" ); - alphabet::Symbol Sp = alphabet::Symbol ( "S'" ); - alphabet::Symbol B = alphabet::Symbol ( "B" ); - alphabet::Symbol Bp = alphabet::Symbol ( "B'" ); - alphabet::Symbol C = alphabet::Symbol ( "C" ); - alphabet::Symbol Cp = alphabet::Symbol ( "C'" ); + DefaultSymbolType S = DefaultSymbolType ( "S" ); + DefaultSymbolType Sp = DefaultSymbolType ( "S'" ); + DefaultSymbolType B = DefaultSymbolType ( "B" ); + DefaultSymbolType Bp = DefaultSymbolType ( "B'" ); + DefaultSymbolType C = DefaultSymbolType ( "C" ); + DefaultSymbolType Cp = DefaultSymbolType ( "C'" ); - alphabet::Symbol a = alphabet::Symbol ( 'a' ); - alphabet::Symbol b = alphabet::Symbol ( 'b' ); - alphabet::Symbol c = alphabet::Symbol ( 'c' ); + DefaultSymbolType a = DefaultSymbolType ( 'a' ); + DefaultSymbolType b = DefaultSymbolType ( 'b' ); + DefaultSymbolType c = DefaultSymbolType ( 'c' ); grammar::CFG < > grammar ( S ); @@ -30,12 +30,12 @@ void LeftFactorize::testLeftFactorize ( ) { grammar.setNonterminalAlphabet ( { S, B, C } ); grammar.setInitialSymbol ( S ); - grammar.addRule ( S, std::vector < alphabet::Symbol > { a, B } ); - grammar.addRule ( S, std::vector < alphabet::Symbol > { a, C } ); - grammar.addRule ( B, std::vector < alphabet::Symbol > { b, B } ); - grammar.addRule ( B, std::vector < alphabet::Symbol > { b } ); - grammar.addRule ( C, std::vector < alphabet::Symbol > { c, C } ); - grammar.addRule ( C, std::vector < alphabet::Symbol > { c } ); + grammar.addRule ( S, std::vector < DefaultSymbolType > { a, B } ); + grammar.addRule ( S, std::vector < DefaultSymbolType > { a, C } ); + grammar.addRule ( B, std::vector < DefaultSymbolType > { b, B } ); + grammar.addRule ( B, std::vector < DefaultSymbolType > { b } ); + grammar.addRule ( C, std::vector < DefaultSymbolType > { c, C } ); + grammar.addRule ( C, std::vector < DefaultSymbolType > { c } ); grammar::CFG < > res = grammar; grammar::parsing::LeftFactorize::leftFactorize ( res, a, S); @@ -48,15 +48,15 @@ void LeftFactorize::testLeftFactorize ( ) { comp.setNonterminalAlphabet ( { S, Sp, B, Bp, C, Cp } ); comp.setInitialSymbol ( S ); - comp.addRule ( S, std::vector < alphabet::Symbol > { a, Sp } ); - comp.addRule ( Sp, std::vector < alphabet::Symbol > { B } ); - comp.addRule ( Sp, std::vector < alphabet::Symbol > { C } ); - comp.addRule ( B, std::vector < alphabet::Symbol > { b, Bp } ); - comp.addRule ( Bp, std::vector < alphabet::Symbol > { B } ); - comp.addRule ( Bp, std::vector < alphabet::Symbol > { } ); - comp.addRule ( C, std::vector < alphabet::Symbol > { c, Cp } ); - comp.addRule ( Cp, std::vector < alphabet::Symbol > { C } ); - comp.addRule ( Cp, std::vector < alphabet::Symbol > { } ); + comp.addRule ( S, std::vector < DefaultSymbolType > { a, Sp } ); + comp.addRule ( Sp, std::vector < DefaultSymbolType > { B } ); + comp.addRule ( Sp, std::vector < DefaultSymbolType > { C } ); + comp.addRule ( B, std::vector < DefaultSymbolType > { b, Bp } ); + comp.addRule ( Bp, std::vector < DefaultSymbolType > { B } ); + comp.addRule ( Bp, std::vector < DefaultSymbolType > { } ); + comp.addRule ( C, std::vector < DefaultSymbolType > { c, Cp } ); + comp.addRule ( Cp, std::vector < DefaultSymbolType > { C } ); + comp.addRule ( Cp, std::vector < DefaultSymbolType > { } ); // std::cout << res << std::endl << comp << std::endl; @@ -64,18 +64,18 @@ void LeftFactorize::testLeftFactorize ( ) { } void LeftFactorize::testLeftFactorize2 ( ) { - alphabet::Symbol S = alphabet::Symbol ( "S" ); - alphabet::Symbol Sp = alphabet::Symbol ( "S'" ); - alphabet::Symbol B = alphabet::Symbol ( "B" ); - alphabet::Symbol Bp = alphabet::Symbol ( "B'" ); - alphabet::Symbol C = alphabet::Symbol ( "C" ); - alphabet::Symbol Cp = alphabet::Symbol ( "C'" ); - alphabet::Symbol D = alphabet::Symbol ( "D" ); - - alphabet::Symbol a = alphabet::Symbol ( 'a' ); - alphabet::Symbol b = alphabet::Symbol ( 'b' ); - alphabet::Symbol c = alphabet::Symbol ( 'c' ); - alphabet::Symbol d = alphabet::Symbol ( 'd' ); + DefaultSymbolType S = DefaultSymbolType ( "S" ); + DefaultSymbolType Sp = DefaultSymbolType ( "S'" ); + DefaultSymbolType B = DefaultSymbolType ( "B" ); + DefaultSymbolType Bp = DefaultSymbolType ( "B'" ); + DefaultSymbolType C = DefaultSymbolType ( "C" ); + DefaultSymbolType Cp = DefaultSymbolType ( "C'" ); + DefaultSymbolType D = DefaultSymbolType ( "D" ); + + DefaultSymbolType a = DefaultSymbolType ( 'a' ); + DefaultSymbolType b = DefaultSymbolType ( 'b' ); + DefaultSymbolType c = DefaultSymbolType ( 'c' ); + DefaultSymbolType d = DefaultSymbolType ( 'd' ); grammar::CFG < > grammar ( S ); @@ -83,15 +83,15 @@ void LeftFactorize::testLeftFactorize2 ( ) { grammar.setNonterminalAlphabet ( { S, B, C, D } ); grammar.setInitialSymbol ( S ); - grammar.addRule ( S, std::vector < alphabet::Symbol > { a, B } ); - grammar.addRule ( S, std::vector < alphabet::Symbol > { a, C } ); - grammar.addRule ( S, std::vector < alphabet::Symbol > { D } ); - grammar.addRule ( B, std::vector < alphabet::Symbol > { b, B } ); - grammar.addRule ( B, std::vector < alphabet::Symbol > { b } ); - grammar.addRule ( C, std::vector < alphabet::Symbol > { c, C } ); - grammar.addRule ( C, std::vector < alphabet::Symbol > { c } ); - grammar.addRule ( C, std::vector < alphabet::Symbol > { b } ); - grammar.addRule ( D, std::vector < alphabet::Symbol > { d } ); + grammar.addRule ( S, std::vector < DefaultSymbolType > { a, B } ); + grammar.addRule ( S, std::vector < DefaultSymbolType > { a, C } ); + grammar.addRule ( S, std::vector < DefaultSymbolType > { D } ); + grammar.addRule ( B, std::vector < DefaultSymbolType > { b, B } ); + grammar.addRule ( B, std::vector < DefaultSymbolType > { b } ); + grammar.addRule ( C, std::vector < DefaultSymbolType > { c, C } ); + grammar.addRule ( C, std::vector < DefaultSymbolType > { c } ); + grammar.addRule ( C, std::vector < DefaultSymbolType > { b } ); + grammar.addRule ( D, std::vector < DefaultSymbolType > { d } ); grammar::CFG < > res = grammar; grammar::parsing::LeftFactorize::leftFactorize ( res, a, S); @@ -104,18 +104,18 @@ void LeftFactorize::testLeftFactorize2 ( ) { comp.setNonterminalAlphabet ( { S, Sp, B, Bp, C, Cp, D} ); comp.setInitialSymbol ( S ); - comp.addRule ( S, std::vector < alphabet::Symbol > { a, Sp } ); - comp.addRule ( S, std::vector < alphabet::Symbol > { D } ); - comp.addRule ( Sp, std::vector < alphabet::Symbol > { B } ); - comp.addRule ( Sp, std::vector < alphabet::Symbol > { C } ); - comp.addRule ( B, std::vector < alphabet::Symbol > { b, Bp } ); - comp.addRule ( Bp, std::vector < alphabet::Symbol > { B } ); - comp.addRule ( Bp, std::vector < alphabet::Symbol > { } ); - comp.addRule ( C, std::vector < alphabet::Symbol > { c, Cp } ); - comp.addRule ( C, std::vector < alphabet::Symbol > { b } ); - comp.addRule ( Cp, std::vector < alphabet::Symbol > { C } ); - comp.addRule ( Cp, std::vector < alphabet::Symbol > { } ); - comp.addRule ( D, std::vector < alphabet::Symbol > { d } ); + comp.addRule ( S, std::vector < DefaultSymbolType > { a, Sp } ); + comp.addRule ( S, std::vector < DefaultSymbolType > { D } ); + comp.addRule ( Sp, std::vector < DefaultSymbolType > { B } ); + comp.addRule ( Sp, std::vector < DefaultSymbolType > { C } ); + comp.addRule ( B, std::vector < DefaultSymbolType > { b, Bp } ); + comp.addRule ( Bp, std::vector < DefaultSymbolType > { B } ); + comp.addRule ( Bp, std::vector < DefaultSymbolType > { } ); + comp.addRule ( C, std::vector < DefaultSymbolType > { c, Cp } ); + comp.addRule ( C, std::vector < DefaultSymbolType > { b } ); + comp.addRule ( Cp, std::vector < DefaultSymbolType > { C } ); + comp.addRule ( Cp, std::vector < DefaultSymbolType > { } ); + comp.addRule ( D, std::vector < DefaultSymbolType > { d } ); // std::cout << res << std::endl << comp << std::endl; diff --git a/alib2algo_experimental/test-src/grammar/parsing/SLR1ParseTable.cpp b/alib2algo_experimental/test-src/grammar/parsing/SLR1ParseTable.cpp index 3331f6c898..4a7b682bbe 100644 --- a/alib2algo_experimental/test-src/grammar/parsing/SLR1ParseTable.cpp +++ b/alib2algo_experimental/test-src/grammar/parsing/SLR1ParseTable.cpp @@ -229,12 +229,12 @@ void SLR1ParseTable::testActionTable ( ) { } }; - std::vector < label::Label > states; + std::vector < DefaultStateType > states; for (const grammar::parsing::LR0Items & currentItems : items) { - states.emplace_back( label::Label ( label::LR0ItemsLabel ( currentItems ) ) ); + states.emplace_back( DefaultStateType ( label::LR0ItemsLabel ( currentItems ) ) ); } - alphabet::Symbol endOfInput = grammar::parsing::LRParser::getEndOfInputSymbol ( getExpressionGrammar ( ) ); + DefaultSymbolType endOfInput = grammar::parsing::LRParser::getEndOfInputSymbol ( getExpressionGrammar ( ) ); grammar::parsing::LRActionTable correctActionTable { { { states[0], identifier }, { grammar::parsing::LRAction::Shift, states[5] } }, { { states[0], leftParenthesis }, { grammar::parsing::LRAction::Shift, states[4] } }, @@ -252,7 +252,7 @@ void SLR1ParseTable::testActionTable ( ) { { { states[9], times }, { grammar::parsing::LRAction::Shift, states[7] } } }; - std::pair < alphabet::Symbol, std::vector < alphabet::Symbol > > reduceBy = { E, { T } }; + std::pair < DefaultSymbolType, std::vector < DefaultSymbolType > > reduceBy = { E, { T } }; correctActionTable.insert ( { { states[2], plus }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); correctActionTable.insert ( { { states[2], rightParenthesis }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); correctActionTable.insert ( { { states[2], endOfInput }, { grammar::parsing::LRAction::Reduce, reduceBy } } ); @@ -304,13 +304,13 @@ void SLR1ParseTable::testActionTable ( ) { bool correctContent = true; switch ( actionTablePair.second.first ) { case grammar::parsing::LRAction::Shift: - if ( actionTablePair.second.second.get < label::Label > ( ) != value->second.second.get < label::Label > ( ) ) { + if ( actionTablePair.second.second.get < DefaultStateType > ( ) != value->second.second.get < DefaultStateType > ( ) ) { correctContent = false; } break; case grammar::parsing::LRAction::Reduce: - if ( actionTablePair.second.second.get < std::pair < alphabet::Symbol, std::vector < alphabet::Symbol > > > ( ) != value->second.second.get < std::pair < alphabet::Symbol, std::vector < alphabet::Symbol > > > ( ) ) { + if ( actionTablePair.second.second.get < std::pair < DefaultSymbolType, std::vector < DefaultSymbolType > > > ( ) != value->second.second.get < std::pair < DefaultSymbolType, std::vector < DefaultSymbolType > > > ( ) ) { correctContent = false; } break; @@ -527,9 +527,9 @@ void SLR1ParseTable::testGotoTable ( ) { } }; - std::vector < label::Label > states; + std::vector < DefaultStateType > states; for (const grammar::parsing::LR0Items & currentItems : items) { - states.emplace_back( label::Label ( label::LR0ItemsLabel ( currentItems ) ) ); + states.emplace_back( DefaultStateType ( label::LR0ItemsLabel ( currentItems ) ) ); } grammar::parsing::LRGotoTable gotoTable { @@ -548,11 +548,11 @@ void SLR1ParseTable::testGotoTable ( ) { } void SLR1ParseTable::testUnambiguousNotSLR1Grammar ( ) { - alphabet::Symbol S = alphabet::Symbol ( 'S' ); - alphabet::Symbol L = alphabet::Symbol ( 'L' ); - alphabet::Symbol R = alphabet::Symbol ( 'R' ); + DefaultSymbolType S = DefaultSymbolType ( 'S' ); + DefaultSymbolType L = DefaultSymbolType ( 'L' ); + DefaultSymbolType R = DefaultSymbolType ( 'R' ); - alphabet::Symbol assignment = alphabet::Symbol ( '=' ); + DefaultSymbolType assignment = DefaultSymbolType ( '=' ); grammar::CFG < > unambiguousNotSLR1Grammar( { S, L, R }, diff --git a/alib2algo_experimental/test-src/grammar/parsing/SLR1ParseTable.h b/alib2algo_experimental/test-src/grammar/parsing/SLR1ParseTable.h index 6cb089bb15..d984dcd160 100644 --- a/alib2algo_experimental/test-src/grammar/parsing/SLR1ParseTable.h +++ b/alib2algo_experimental/test-src/grammar/parsing/SLR1ParseTable.h @@ -13,15 +13,15 @@ class SLR1ParseTable : public CppUnit::TestFixture { CPPUNIT_TEST ( testUnambiguousNotSLR1Grammar ); CPPUNIT_TEST_SUITE_END ( ); - alphabet::Symbol E = alphabet::Symbol ( 'E' ); - alphabet::Symbol T = alphabet::Symbol ( 'T' ); - alphabet::Symbol F = alphabet::Symbol ( 'F' ); - - alphabet::Symbol plus = alphabet::Symbol ( '+' ); - alphabet::Symbol times = alphabet::Symbol ( '*' ); - alphabet::Symbol leftParenthesis = alphabet::Symbol ( '(' ); - alphabet::Symbol rightParenthesis = alphabet::Symbol ( ')' ); - alphabet::Symbol identifier = alphabet::Symbol ( "id" ); + DefaultSymbolType E = DefaultSymbolType ( 'E' ); + DefaultSymbolType T = DefaultSymbolType ( 'T' ); + DefaultSymbolType F = DefaultSymbolType ( 'F' ); + + DefaultSymbolType plus = DefaultSymbolType ( '+' ); + DefaultSymbolType times = DefaultSymbolType ( '*' ); + DefaultSymbolType leftParenthesis = DefaultSymbolType ( '(' ); + DefaultSymbolType rightParenthesis = DefaultSymbolType ( ')' ); + DefaultSymbolType identifier = DefaultSymbolType ( "id" ); grammar::CFG < > getExpressionGrammar ( ); diff --git a/alib2data_experimental/src/grammar/parsing/LRParserTypes.h b/alib2data_experimental/src/grammar/parsing/LRParserTypes.h index 7d297d4b92..b73cedaf07 100644 --- a/alib2data_experimental/src/grammar/parsing/LRParserTypes.h +++ b/alib2data_experimental/src/grammar/parsing/LRParserTypes.h @@ -8,8 +8,8 @@ #ifndef LR_PARSER_TYPES_H_ #define LR_PARSER_TYPES_H_ -#include <alphabet/Symbol.h> -#include <label/Label.h> +#include <common/DefaultSymbolType.h> +#include <common/DefaultStateType.h> #include <map> #include <set> @@ -26,9 +26,9 @@ enum class LRAction { Accept }; -typedef std::map < alphabet::Symbol, std::set < std::pair < unsigned, std::vector < alphabet::Symbol > > > > LR0Items; -typedef std::map < std::pair < label::Label, alphabet::Symbol >, std::pair < LRAction, std::variant < label::Label, std::pair < alphabet::Symbol, std::vector < alphabet::Symbol > > > > > LRActionTable; -typedef std::map < std::pair < label::Label, alphabet::Symbol >, label::Label > LRGotoTable; +typedef std::map < DefaultSymbolType, std::set < std::pair < unsigned, std::vector < DefaultSymbolType > > > > LR0Items; +typedef std::map < std::pair < DefaultStateType, DefaultSymbolType >, std::pair < LRAction, std::variant < DefaultStateType, std::pair < DefaultSymbolType, std::vector < DefaultSymbolType > > > > > LRActionTable; +typedef std::map < std::pair < DefaultStateType, DefaultSymbolType >, DefaultStateType > LRGotoTable; } /* namespace parsing */ -- GitLab