Skip to content
Snippets Groups Projects
Commit a505a3c8 authored by Jan Trávníček's avatar Jan Trávníček
Browse files

simplify creation of unique symbols and states

parent c730d59f
No related branches found
No related tags found
No related merge requests found
......@@ -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 } );
}
}
}
......
......@@ -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:
......
......@@ -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});
......
......@@ -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 ) {
......
......@@ -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." );
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment