diff --git a/alib2algo/src/grammar/parsing/LR0Parser.cpp b/alib2algo/src/grammar/parsing/LR0Parser.cpp index 0f8ea0632697eae1a77bda847048affe07fd3f08..65354ff9bbb372098441aa7f4cba1affdcdc5e30 100644 --- a/alib2algo/src/grammar/parsing/LR0Parser.cpp +++ b/alib2algo/src/grammar/parsing/LR0Parser.cpp @@ -28,17 +28,17 @@ LR0Items LR0Parser::getClosure ( LR0Items items, grammar::CFG originalGrammar ) unsigned position = item.first; std::vector < alphabet::Symbol > rightHandSide = item.second; - if ( position == rightHandSide.size ( ) ) { + if ( position == rightHandSide.size ( ) ) continue; - } std::map < alphabet::Symbol, std::set < std::vector < alphabet::Symbol > > >::const_iterator rulesIterator = originalGrammar.getRules ( ) . find(rightHandSide[position]); - if ( rulesIterator != originalGrammar.getRules ( ) . end ( ) ) { - for ( const std::vector < alphabet::Symbol > & rule : rulesIterator->second ) { - if (items[rightHandSide[position]].find ( { 0, rule } ) == items[rightHandSide[position]].end ( ) ) { - changed = true; - items[rightHandSide[position]].insert ( { 0, rule } ); - } + if ( rulesIterator == originalGrammar.getRules ( ) . end ( ) ) + continue; + + for ( const std::vector < alphabet::Symbol > & rule : rulesIterator->second ) { + if (items[rightHandSide[position]].find ( { 0, rule } ) == items[rightHandSide[position]].end ( ) ) { + changed = true; + items[rightHandSide[position]].insert ( { 0, rule } ); } } } diff --git a/alib2algo/src/grammar/parsing/LRParser.cpp b/alib2algo/src/grammar/parsing/LRParser.cpp index 5336ce63a30dad2ed82143dba74019086441f4a0..6275d1442d3cedd8032633b56f3d9583f3a19afb 100644 --- a/alib2algo/src/grammar/parsing/LRParser.cpp +++ b/alib2algo/src/grammar/parsing/LRParser.cpp @@ -15,18 +15,11 @@ namespace grammar { namespace parsing { alphabet::Symbol LRParser::getEndOfInputSymbol ( grammar::CFG originalGrammar ) { - alphabet::Symbol endOfInputSymbol {alphabet::EndSymbol()}; - while ( originalGrammar.getTerminalAlphabet ( ) . find ( endOfInputSymbol ) != originalGrammar.getTerminalAlphabet ( ) . end ( ) ) { - endOfInputSymbol.inc ( ); - } - return endOfInputSymbol; + return alphabet::createUniqueSymbol ( alphabet::Symbol ( alphabet::EndSymbol ( ) ), originalGrammar.getTerminalAlphabet ( ), originalGrammar.getNonterminalAlphabet ( ) ); } grammar::CFG LRParser::getAugmentedGrammar ( grammar::CFG originalGrammar ) { - alphabet::Symbol initialSymbol = originalGrammar.getInitialSymbol ( ); - do { - initialSymbol = initialSymbol.next ( ); - } while ( originalGrammar.getNonterminalAlphabet ( ) . find ( initialSymbol ) != originalGrammar.getNonterminalAlphabet ( ) . end ( ) ); + alphabet::Symbol initialSymbol = alphabet::createUniqueSymbol ( originalGrammar.getInitialSymbol(), originalGrammar.getTerminalAlphabet ( ), originalGrammar.getNonterminalAlphabet ( ) ); originalGrammar.addNonterminalSymbol ( initialSymbol ); originalGrammar.addRule ( initialSymbol, { originalGrammar.getInitialSymbol ( ) } ); @@ -42,9 +35,8 @@ bool LRParser::parse ( LRActionTable actionTable, LRGotoTable gotoTable, automat unsigned currentPosition = 0; while ( true ) { LRActionTable::iterator actionIterator = actionTable.find ( { states.top ( ), input[currentPosition] } ); - if ( actionIterator == actionTable.end ( ) ) { + if ( actionIterator == actionTable.end ( ) ) return false; - } switch ( actionIterator->second.first ) { case LRAction::Shift: diff --git a/alib2algo/test-src/grammar/simplify/GrammarToCNFTest.cpp b/alib2algo/test-src/grammar/simplify/GrammarToCNFTest.cpp index c380d7864aa32f13ea2bba3a3b58fa0780772e19..9efbbadbef670b8a439dca5a95b7b89745f91ed1 100644 --- a/alib2algo/test-src/grammar/simplify/GrammarToCNFTest.cpp +++ b/alib2algo/test-src/grammar/simplify/GrammarToCNFTest.cpp @@ -78,9 +78,9 @@ void GrammarToCNFTest::testToCNFRules2() { alphabet::Symbol aP = alphabet::symbolFrom("a'"); alphabet::Symbol bP = alphabet::symbolFrom("b'"); alphabet::Symbol cP = alphabet::symbolFrom("c'"); - alphabet::Symbol Xb = alphabet::Symbol(alphabet::UniqueSymbol(alphabet::Symbol(alphabet::SymbolPairSymbol(std::make_pair(X, b))), primitive::Integer(0))); - alphabet::Symbol aX = alphabet::Symbol(alphabet::UniqueSymbol(alphabet::Symbol(alphabet::SymbolPairSymbol(std::make_pair(a, X))), primitive::Integer(0))); - alphabet::Symbol bX = alphabet::Symbol(alphabet::UniqueSymbol(alphabet::Symbol(alphabet::SymbolPairSymbol(std::make_pair(b, X))), primitive::Integer(0))); + alphabet::Symbol Xb = alphabet::Symbol(alphabet::Symbol(alphabet::SymbolPairSymbol(std::make_pair(X, b)))); + alphabet::Symbol aX = alphabet::Symbol(alphabet::Symbol(alphabet::SymbolPairSymbol(std::make_pair(a, X)))); + alphabet::Symbol bX = alphabet::Symbol(alphabet::Symbol(alphabet::SymbolPairSymbol(std::make_pair(b, X)))); grammar::CNF grammar3(S); grammar3.setNonterminalAlphabet({S, X, Y, aP, bP, cP, Xb, aX, bX}); diff --git a/alib2data/src/alphabet/Symbol.cpp b/alib2data/src/alphabet/Symbol.cpp index 5950769270663925cfb67f70d0cab94023aa3714..886350a10796308b628a40855c48c6fa3799d79b 100644 --- a/alib2data/src/alphabet/Symbol.cpp +++ b/alib2data/src/alphabet/Symbol.cpp @@ -31,37 +31,25 @@ alphabet::Symbol createUniqueSymbol ( alphabet::Symbol attempt, const std::set < int i = 0; do { - attempt.inc ( ); - if ( ( terminalAlphabet.count ( attempt ) == 0 ) && ( nonterminalAlphabet.count ( attempt ) == 0 ) ) return attempt; - } while ( ++i < INT_MAX ); + + attempt.inc ( ); + } while ( i++ < INT_MAX ); throw SymbolException ( "Could not create unique symbol." ); } alphabet::Symbol symbolFrom ( int number ) { - return alphabet::Symbol { - alphabet::LabeledSymbol { - label::labelFrom ( number ) - } - }; + return alphabet::Symbol { alphabet::LabeledSymbol { label::labelFrom ( number ) } }; } alphabet::Symbol symbolFrom ( char character ) { - return alphabet::Symbol { - alphabet::LabeledSymbol { - label::labelFrom ( character ) - } - }; + return alphabet::Symbol { alphabet::LabeledSymbol { label::labelFrom ( character ) } }; } alphabet::Symbol symbolFrom ( std::string string ) { - return alphabet::Symbol { - alphabet::LabeledSymbol { - label::labelFrom ( std::move ( string ) ) - } - }; + return alphabet::Symbol { alphabet::LabeledSymbol { label::labelFrom ( std::move ( string ) ) } }; } alphabet::Symbol symbolFrom ( const char * string ) { diff --git a/alib2data/src/automaton/Automaton.cpp b/alib2data/src/automaton/Automaton.cpp index e31eb826040c7f66e371cdc93fa076fcf83e148e..357ea9f76ab5cf9fc72751ee36e00049ab57aa63 100644 --- a/alib2data/src/automaton/Automaton.cpp +++ b/alib2data/src/automaton/Automaton.cpp @@ -19,11 +19,11 @@ State createUniqueState ( State nextState, const std::set < State > & other ) { int i = 0; do { - nextState.getName ( ).inc ( ); - if ( other.count ( nextState ) == 0 ) return nextState; - } while ( ++i < INT_MAX ); + + nextState.getName ( ).inc ( ); + } while ( i++ < INT_MAX ); throw AutomatonException ( "Could not create unique state." ); }