From 75ad547d2bc927d8bf8052e23e3c6e90227a485d Mon Sep 17 00:00:00 2001 From: Jan Travnicek <Jan.Travnicek@fit.cvut.cz> Date: Thu, 5 Nov 2015 21:11:59 +0100 Subject: [PATCH] refactor code part 1 --- .../parsing/DeterministicLL1Grammar.cpp | 42 +++--------------- .../src/grammar/parsing/LeftFactorize.cpp | 43 +++++++++++++++++++ alib2algo/src/grammar/parsing/LeftFactorize.h | 29 +++++++++++++ .../src/grammar/parsing/common/Substitute.cpp | 26 +++++++++++ .../src/grammar/parsing/common/Substitute.h | 30 +++++++++++++ 5 files changed, 133 insertions(+), 37 deletions(-) create mode 100644 alib2algo/src/grammar/parsing/LeftFactorize.cpp create mode 100644 alib2algo/src/grammar/parsing/LeftFactorize.h create mode 100644 alib2algo/src/grammar/parsing/common/Substitute.cpp create mode 100644 alib2algo/src/grammar/parsing/common/Substitute.h diff --git a/alib2algo/src/grammar/parsing/DeterministicLL1Grammar.cpp b/alib2algo/src/grammar/parsing/DeterministicLL1Grammar.cpp index ac0002ff43..39d411e5cd 100644 --- a/alib2algo/src/grammar/parsing/DeterministicLL1Grammar.cpp +++ b/alib2algo/src/grammar/parsing/DeterministicLL1Grammar.cpp @@ -9,6 +9,8 @@ #include "LL1ParseTable.h" #include "First.h" #include "../properties/NullableNonterminals.h" +#include "common/Substitute.h" +#include "LeftFactorize.h" #include <grammar/ContextFree/CFG.h> #include <exception/AlibException.h> @@ -19,40 +21,6 @@ namespace grammar { namespace parsing { -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 ) { - for ( const std::vector < alphabet::Symbol > & rhs : orig.getRules ( ).find ( * nonterminal )->second ) { - std::vector < alphabet::Symbol > newRHS ( origRHS.begin ( ), nonterminal ); - newRHS.insert ( newRHS.end ( ), rhs.begin ( ), rhs.end ( ) ); - newRHS.insert ( newRHS.end ( ), nonterminal + 1, origRHS.end ( ) ); - res.addRule ( origLHS, newRHS ); - } -} - -void leftFactorize ( grammar::CFG & grammar, const alphabet::Symbol & terminal, const alphabet::Symbol & nonterminal ) { - grammar::CFG res ( grammar.getInitialSymbol ( ) ); - - res.setNonterminalAlphabet ( grammar.getNonterminalAlphabet ( ) ); - res.setTerminalAlphabet ( grammar.getTerminalAlphabet ( ) ); - - alphabet::Symbol primed = alphabet::createUniqueSymbol ( nonterminal, grammar.getTerminalAlphabet ( ), grammar.getNonterminalAlphabet ( ) ); - res.addNonterminalSymbol ( primed ); - - 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] == terminal ) ) { - res.addRule ( lhs, std::vector < alphabet::Symbol > { terminal, primed } ); - res.addRule ( primed, std::vector < alphabet::Symbol > ( rhs.begin ( ) + 1, rhs.end ( ) ) ); - } else { - res.addRule ( lhs, rhs ); - } - } - } - - grammar = res; -} - void cornerSubstitution ( grammar::CFG & grammar, const alphabet::Symbol & terminal, const alphabet::Symbol & nonterminal ) { grammar::CFG res ( grammar.getInitialSymbol ( ) ); @@ -64,7 +32,7 @@ void cornerSubstitution ( grammar::CFG & grammar, const alphabet::Symbol & termi 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 ( grammar, res, lhs, rhs, rhs.begin ( ) ); + Substitute::substitute ( grammar, res, lhs, rhs, rhs.begin ( ) ); else res.addRule ( lhs, rhs ); } @@ -80,7 +48,7 @@ bool handleFirstFirstConflict ( grammar::CFG & grammar, const alphabet::Symbol & return true; } - leftFactorize ( grammar, terminal, nonterminal ); + LeftFactorize::leftFactorize ( grammar, terminal, nonterminal ); return true; } @@ -147,7 +115,7 @@ void extractRightContext ( grammar::CFG & grammar, const alphabet::Symbol & term if ( nonterminals.count ( * iter ) && First::first ( grammar, std::vector < alphabet::Symbol > ( iter + 1, rhs.end ( ) ) ).count ( terminal ) && !grammar.getTerminalAlphabet ( ).count ( * iter ) ) { substitued = true; - substitute ( grammar, res, lhs, rhs, iter ); + Substitute::substitute ( grammar, res, lhs, rhs, iter ); } if ( !substitued ) diff --git a/alib2algo/src/grammar/parsing/LeftFactorize.cpp b/alib2algo/src/grammar/parsing/LeftFactorize.cpp new file mode 100644 index 0000000000..b608cfce5b --- /dev/null +++ b/alib2algo/src/grammar/parsing/LeftFactorize.cpp @@ -0,0 +1,43 @@ +/* + * LeftFactorize.cpp + * + * Created on: 9. 6. 2015 + * Author: Jan Travnicek + */ + +#include "LeftFactorize.h" + +#include <grammar/ContextFree/CFG.h> + +namespace grammar { + +namespace parsing { + +void LeftFactorize::leftFactorize ( grammar::CFG & grammar, const alphabet::Symbol & terminal, const alphabet::Symbol & nonterminal ) { + grammar::CFG res ( grammar.getInitialSymbol ( ) ); + + res.setNonterminalAlphabet ( grammar.getNonterminalAlphabet ( ) ); + res.setTerminalAlphabet ( grammar.getTerminalAlphabet ( ) ); + + alphabet::Symbol primed = alphabet::createUniqueSymbol ( nonterminal, grammar.getTerminalAlphabet ( ), grammar.getNonterminalAlphabet ( ) ); + res.addNonterminalSymbol ( primed ); + + 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] == terminal ) ) { + res.addRule ( lhs, std::vector < alphabet::Symbol > { terminal, primed } ); + res.addRule ( primed, std::vector < alphabet::Symbol > ( rhs.begin ( ) + 1, rhs.end ( ) ) ); + } else { + res.addRule ( lhs, rhs ); + } + } + } + + grammar = res; +} + +} /* namespace parsing */ + +} /* namespace grammar */ diff --git a/alib2algo/src/grammar/parsing/LeftFactorize.h b/alib2algo/src/grammar/parsing/LeftFactorize.h new file mode 100644 index 0000000000..74626750aa --- /dev/null +++ b/alib2algo/src/grammar/parsing/LeftFactorize.h @@ -0,0 +1,29 @@ +/* + * LeftFactorize.h + * + * Created on: 9. 6. 2015 + * Author: Jan Travnicek + */ + +#ifndef LEFT_FACTORIZE_H_ +#define LEFT_FACTORIZE_H_ + +#include <common/multipleDispatch.hpp> +#include <alphabet/Symbol.h> +#include <grammar/GrammarFeatures.h> + +namespace grammar { + +namespace parsing { + +class LeftFactorize { +public: + static void leftFactorize ( grammar::CFG & grammar, const alphabet::Symbol & terminal, const alphabet::Symbol & nonterminal ); + +}; + +} /* namespace parsing */ + +} /* namespace grammar */ + +#endif /* LEFT_FACTORIZE_H_ */ diff --git a/alib2algo/src/grammar/parsing/common/Substitute.cpp b/alib2algo/src/grammar/parsing/common/Substitute.cpp new file mode 100644 index 0000000000..16e6b0fa02 --- /dev/null +++ b/alib2algo/src/grammar/parsing/common/Substitute.cpp @@ -0,0 +1,26 @@ +/* + * Substitute.cpp + * + * Created on: 9. 6. 2015 + * Author: Jan Travnicek + */ + +#include "Substitute.h" +#include <grammar/ContextFree/CFG.h> + +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 ) { + for ( const std::vector < alphabet::Symbol > & rhs : orig.getRules ( ).find ( * nonterminal )->second ) { + std::vector < alphabet::Symbol > newRHS ( origRHS.begin ( ), nonterminal ); + newRHS.insert ( newRHS.end ( ), rhs.begin ( ), rhs.end ( ) ); + newRHS.insert ( newRHS.end ( ), nonterminal + 1, origRHS.end ( ) ); + res.addRule ( origLHS, newRHS ); + } +} + +} /* namespace parsing */ + +} /* namespace grammar */ diff --git a/alib2algo/src/grammar/parsing/common/Substitute.h b/alib2algo/src/grammar/parsing/common/Substitute.h new file mode 100644 index 0000000000..99be640fde --- /dev/null +++ b/alib2algo/src/grammar/parsing/common/Substitute.h @@ -0,0 +1,30 @@ +/* + * Substitute.h + * + * Created on: 9. 6. 2015 + * Author: Jan Travnicek + */ + +#ifndef SUBSTITUTE_H_ +#define SUBSTITUTE_H_ + +#include <common/multipleDispatch.hpp> +#include <grammar/GrammarFeatures.h> +#include <alphabet/Symbol.h> +#include <vector> + +namespace grammar { + +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 ); + +}; + +} /* namespace parsing */ + +} /* namespace grammar */ + +#endif /* SUBSTITUTE_H_ */ -- GitLab