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

continue refactoring

parent 75ad547d
No related branches found
No related tags found
No related merge requests found
/*
* CornerSubstitution.cpp
*
* Created on: 9. 6. 2015
* Author: Jan Travnicek
*/
#include "First.h"
#include "common/Substitute.h"
#include "CornerSubstitution.h"
#include <grammar/ContextFree/CFG.h>
namespace grammar {
namespace parsing {
void CornerSubstitution::cornerSubstitution ( grammar::CFG & grammar, const alphabet::Symbol & terminal, const alphabet::Symbol & nonterminal ) {
grammar::CFG res ( grammar.getInitialSymbol ( ) );
res.setNonterminalAlphabet ( grammar.getNonterminalAlphabet ( ) );
res.setTerminalAlphabet ( grammar.getTerminalAlphabet ( ) );
for ( const std::pair < alphabet::Symbol, std::set < std::vector < alphabet::Symbol > > > & rule : grammar.getRules ( ) ) {
const alphabet::Symbol & lhs = rule.first;
for ( const std::vector < alphabet::Symbol > & rhs : rule.second ) {
if ( ( lhs == nonterminal ) && ( rhs.size ( ) > 0 ) && ( rhs[0] == nonterminal ) && First::first ( grammar, rhs ).count ( terminal ) )
Substitute::substitute ( grammar, res, lhs, rhs, rhs.begin ( ) );
else
res.addRule ( lhs, rhs );
}
}
grammar = res;
}
} /* namespace parsing */
} /* namespace grammar */
/*
* CornerSubstitution.h
*
* Created on: 9. 6. 2015
* Author: Jan Travnicek
*/
#ifndef CORNER_SUBSTITUTION_H_
#define CORNER_SUBSTITUTION_H_
#include <grammar/GrammarFeatures.h>
#include <alphabet/Symbol.h>
namespace grammar {
namespace parsing {
class CornerSubstitution {
public:
static void cornerSubstitution ( grammar::CFG & grammar, const alphabet::Symbol & terminal, const alphabet::Symbol & nonterminal );
};
} /* namespace parsing */
} /* namespace grammar */
#endif /* CORNER_SUBSTITUTION_H_ */
...@@ -2,7 +2,7 @@ ...@@ -2,7 +2,7 @@
* DeterministicLL1ParseTable.cpp * DeterministicLL1ParseTable.cpp
* *
* Created on: 9. 6. 2015 * Created on: 9. 6. 2015
* Author: Tomas Pecka * Author: Jan Travnicek
*/ */
   
#include "DeterministicLL1Grammar.h" #include "DeterministicLL1Grammar.h"
...@@ -10,7 +10,7 @@ ...@@ -10,7 +10,7 @@
#include "First.h" #include "First.h"
#include "../properties/NullableNonterminals.h" #include "../properties/NullableNonterminals.h"
#include "common/Substitute.h" #include "common/Substitute.h"
#include "LeftFactorize.h" #include "HandleFirstFirstConflict.h"
   
#include <grammar/ContextFree/CFG.h> #include <grammar/ContextFree/CFG.h>
#include <exception/AlibException.h> #include <exception/AlibException.h>
...@@ -21,37 +21,6 @@ namespace grammar { ...@@ -21,37 +21,6 @@ namespace grammar {
   
namespace parsing { namespace parsing {
   
void cornerSubstitution ( grammar::CFG & grammar, const alphabet::Symbol & terminal, const alphabet::Symbol & nonterminal ) {
grammar::CFG res ( grammar.getInitialSymbol ( ) );
res.setNonterminalAlphabet ( grammar.getNonterminalAlphabet ( ) );
res.setTerminalAlphabet ( grammar.getTerminalAlphabet ( ) );
for ( const std::pair < alphabet::Symbol, std::set < std::vector < alphabet::Symbol > > > & rule : grammar.getRules ( ) ) {
const alphabet::Symbol & lhs = rule.first;
for ( const std::vector < alphabet::Symbol > & rhs : rule.second ) {
if ( ( lhs == nonterminal ) && ( rhs.size ( ) > 0 ) && ( rhs[0] == nonterminal ) && First::first ( grammar, rhs ).count ( terminal ) )
Substitute::substitute ( grammar, res, lhs, rhs, rhs.begin ( ) );
else
res.addRule ( lhs, rhs );
}
}
grammar = res;
}
bool 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 )
if ( ( rhs.size ( ) > 0 ) && grammar.getNonterminalAlphabet ( ).count ( rhs[0] ) && First::first ( grammar, rhs ).count ( terminal ) ) {
cornerSubstitution ( grammar, terminal, nonterminal );
return true;
}
LeftFactorize::leftFactorize ( grammar, terminal, nonterminal );
return true;
}
alphabet::Symbol handleAbsobtion ( const grammar::CFG & orig, grammar::CFG & res, const alphabet::Symbol & nonterminal, const alphabet::Symbol & terminal ) { alphabet::Symbol handleAbsobtion ( const grammar::CFG & orig, grammar::CFG & res, const alphabet::Symbol & nonterminal, const alphabet::Symbol & terminal ) {
alphabet::Symbol newSymbol = alphabet::createUniqueSymbol ( alphabet::Symbol ( alphabet::SymbolPairSymbol ( std::make_pair ( nonterminal, terminal ) ) ), res.getTerminalAlphabet ( ), res.getNonterminalAlphabet ( ) ); alphabet::Symbol newSymbol = alphabet::createUniqueSymbol ( alphabet::Symbol ( alphabet::SymbolPairSymbol ( std::make_pair ( nonterminal, terminal ) ) ), res.getTerminalAlphabet ( ), res.getNonterminalAlphabet ( ) );
   
...@@ -175,7 +144,7 @@ bool transformToLL1 ( grammar::CFG & grammar, const alphabet::Symbol & terminal, ...@@ -175,7 +144,7 @@ bool transformToLL1 ( grammar::CFG & grammar, const alphabet::Symbol & terminal,
} ) ) } ) )
return handleFirstFollowConflict ( grammar, terminal, nonterminal, rhsds ); return handleFirstFollowConflict ( grammar, terminal, nonterminal, rhsds );
else else
return handleFirstFirstConflict ( grammar, terminal, nonterminal, rhsds ); return HandleFirstFirstConflict::handleFirstFirstConflict ( grammar, terminal, nonterminal, rhsds );
} }
   
grammar::CFG DeterministicLL1Grammar::convert ( const grammar::CFG & param ) { grammar::CFG DeterministicLL1Grammar::convert ( const grammar::CFG & param ) {
......
/*
* DeterministicLL1ParseTable.cpp
*
* Created on: 9. 6. 2015
* Author: Jan Travnicek
*/
#include "HandleFirstFirstConflict.h"
#include "First.h"
#include "LeftFactorize.h"
#include "CornerSubstitution.h"
#include <grammar/ContextFree/CFG.h>
namespace grammar {
namespace parsing {
bool 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 )
if ( ( rhs.size ( ) > 0 ) && grammar.getNonterminalAlphabet ( ).count ( rhs[0] ) && First::first ( grammar, rhs ).count ( terminal ) ) {
CornerSubstitution::cornerSubstitution ( grammar, terminal, nonterminal );
return true;
}
LeftFactorize::leftFactorize ( grammar, terminal, nonterminal );
return true;
}
} /* namespace parsing */
} /* namespace grammar */
/*
* HandleFirstFirstConflict.h
*
* Created on: 9. 6. 2015
* Author: Jan Travnicek
*/
#ifndef HANDLE_FIRST_FIRST_CONFLICT_H_
#define HANDLE_FIRST_FIRST_CONFLICT_H_
#include <grammar/GrammarFeatures.h>
#include <alphabet/Symbol.h>
#include <vector>
namespace grammar {
namespace parsing {
class HandleFirstFirstConflict {
public:
static bool handleFirstFirstConflict ( grammar::CFG & grammar, const alphabet::Symbol & terminal, const alphabet::Symbol & nonterminal, const std::set < std::vector < alphabet::Symbol > > & rhsds );
};
} /* namespace parsing */
} /* namespace grammar */
#endif /* HANDLE_FIRST_FIRST_CONFLICT_H_ */
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