diff --git a/alib2algo/src/grammar/parsing/ExtractRightContext.cpp b/alib2algo/src/grammar/parsing/ExtractRightContext.cpp index 6d1ad8f9a37f149d582ee75f46260baed723829e..835c95a58247f513cded6a74f32a0b16c42bc73b 100644 --- a/alib2algo/src/grammar/parsing/ExtractRightContext.cpp +++ b/alib2algo/src/grammar/parsing/ExtractRightContext.cpp @@ -23,12 +23,18 @@ void ExtractRightContext::extractRightContext ( grammar::CFG & grammar, const al 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 ) - 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 ) ) - Substitute::substitute ( grammar, res, lhs, rhs, iter ); - else - res.addRule ( lhs, rhs ); + for ( const std::vector < alphabet::Symbol > & 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 ) ) { + Substitute::substitute ( grammar, res, lhs, rhs, iter + 1 ); + substitued = true; + break; + } + if( ! substitued ) + res.addRule ( lhs, rhs ); + } } diff --git a/alib2algo/test-src/grammar/parsing/CornerSubstitution.cpp b/alib2algo/test-src/grammar/parsing/CornerSubstitution.cpp index 6bd7aa9ee20e18dbc568608df5d83a8483c0fe82..6b586609cdd81757343cace8ed765ec3f80b3bb9 100644 --- a/alib2algo/test-src/grammar/parsing/CornerSubstitution.cpp +++ b/alib2algo/test-src/grammar/parsing/CornerSubstitution.cpp @@ -52,7 +52,7 @@ void CornerSubstitution::testCornerSubstitution ( ) { comp.addRule ( C, std::vector < alphabet::Symbol > { a, C } ); comp.addRule ( C, std::vector < alphabet::Symbol > { b, B } ); - std::cout << res << std::endl << comp << std::endl; +// std::cout << res << std::endl << comp << std::endl; CPPUNIT_ASSERT ( res == comp ); } @@ -99,7 +99,7 @@ void CornerSubstitution::testCornerSubstitution2 ( ) { comp.addRule ( C, std::vector < alphabet::Symbol > { a, C } ); comp.addRule ( C, std::vector < alphabet::Symbol > { B, b } ); - std::cout << res << std::endl << comp << std::endl; +// std::cout << res << std::endl << comp << std::endl; CPPUNIT_ASSERT ( res == comp ); } diff --git a/alib2algo/test-src/grammar/parsing/ExtractRightContext.cpp b/alib2algo/test-src/grammar/parsing/ExtractRightContext.cpp new file mode 100644 index 0000000000000000000000000000000000000000..69184b1a894ed3dc3106e6ee6462ecc36910b5ea --- /dev/null +++ b/alib2algo/test-src/grammar/parsing/ExtractRightContext.cpp @@ -0,0 +1,60 @@ +#include "ExtractRightContext.h" + +#include "grammar/ContextFree/CFG.h" +#include "grammar/parsing/ExtractRightContext.h" + +CPPUNIT_TEST_SUITE_NAMED_REGISTRATION ( ExtractRightContext, "grammar" ); +CPPUNIT_TEST_SUITE_REGISTRATION ( ExtractRightContext ); + +void ExtractRightContext::setUp ( ) { +} + +void ExtractRightContext::tearDown ( ) { +} + +void ExtractRightContext::testExtractRightContext ( ) { + alphabet::Symbol S = alphabet::symbolFrom ( "S" ); + alphabet::Symbol A = alphabet::symbolFrom ( "A" ); + alphabet::Symbol C = alphabet::symbolFrom ( "C" ); + + alphabet::Symbol a = alphabet::symbolFrom ( 'a' ); + alphabet::Symbol b = alphabet::symbolFrom ( 'b' ); + alphabet::Symbol c = alphabet::symbolFrom ( 'c' ); + + grammar::CFG grammar ( S ); + + grammar.setTerminalAlphabet ( { a, b, c } ); + 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 ( C, {a, b} ); + grammar.addRule ( C, {c, C} ); + + grammar::CFG res = grammar; + grammar::parsing::ExtractRightContext::extractRightContext ( res, a, { A } ); + + grammar::CFG comp ( S ); + + comp.setTerminalAlphabet ( { a, b, c } ); + 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 } ); + + std::cout << res << std::endl << comp << std::endl; + + CPPUNIT_ASSERT ( res == comp ); +} + +void ExtractRightContext::testExtractRightContext2 ( ) { +} diff --git a/alib2algo/test-src/grammar/parsing/ExtractRightContext.h b/alib2algo/test-src/grammar/parsing/ExtractRightContext.h new file mode 100644 index 0000000000000000000000000000000000000000..f471356bc83f0076b9481973bffa7c8b8ca8fdfb --- /dev/null +++ b/alib2algo/test-src/grammar/parsing/ExtractRightContext.h @@ -0,0 +1,20 @@ +#ifndef EXTRACT_RIGHT_CONTEXT_TEST_H_ +#define EXTRACT_RIGHT_CONTEXT_TEST_H_ + +#include <cppunit/extensions/HelperMacros.h> + +class ExtractRightContext : public CppUnit::TestFixture { + CPPUNIT_TEST_SUITE ( ExtractRightContext ); + CPPUNIT_TEST ( testExtractRightContext ); + CPPUNIT_TEST ( testExtractRightContext2 ); + CPPUNIT_TEST_SUITE_END ( ); + +public: + void setUp ( ); + void tearDown ( ); + + void testExtractRightContext ( ); + void testExtractRightContext2 ( ); +}; + +#endif /* EXTRACT_RIGHT_CONTEXT_TEST_H_ */ diff --git a/alib2algo/test-src/grammar/parsing/HandleFirstFirstConflict.cpp b/alib2algo/test-src/grammar/parsing/HandleFirstFirstConflict.cpp index dbbafa42df0d3412b2a33f3e49a48bfb5d3efd16..eb002db5ad47fae0cf14e6c6d42471750ec7b7f7 100644 --- a/alib2algo/test-src/grammar/parsing/HandleFirstFirstConflict.cpp +++ b/alib2algo/test-src/grammar/parsing/HandleFirstFirstConflict.cpp @@ -53,7 +53,7 @@ void HandleFirstFirstConflict::testHandleFirstFirstConflict ( ) { comp.addRule ( C, std::vector < alphabet::Symbol > { a, C } ); comp.addRule ( C, std::vector < alphabet::Symbol > { b, B } ); - std::cout << res << std::endl << comp << std::endl; +// std::cout << res << std::endl << comp << std::endl; CPPUNIT_ASSERT ( res == comp ); @@ -74,7 +74,7 @@ void HandleFirstFirstConflict::testHandleFirstFirstConflict ( ) { comp2.addRule ( C, std::vector < alphabet::Symbol > { a, C } ); comp2.addRule ( C, std::vector < alphabet::Symbol > { b, B } ); - std::cout << res << std::endl << comp2 << std::endl; +// std::cout << res << std::endl << comp2 << std::endl; CPPUNIT_ASSERT ( res == comp2 ); } @@ -122,7 +122,7 @@ void HandleFirstFirstConflict::testHandleFirstFirstConflict2 ( ) { comp.addRule ( C, std::vector < alphabet::Symbol > { a, C } ); comp.addRule ( C, std::vector < alphabet::Symbol > { B, b } ); - std::cout << res << std::endl << comp << std::endl; +// std::cout << res << std::endl << comp << std::endl; CPPUNIT_ASSERT ( res == comp ); @@ -144,7 +144,7 @@ void HandleFirstFirstConflict::testHandleFirstFirstConflict2 ( ) { comp2.addRule ( C, std::vector < alphabet::Symbol > { a, C } ); comp2.addRule ( C, std::vector < alphabet::Symbol > { B, b } ); - std::cout << res << std::endl << comp2 << std::endl; +// std::cout << res << std::endl << comp2 << std::endl; CPPUNIT_ASSERT ( res == comp2 ); } diff --git a/alib2algo/test-src/grammar/parsing/LeftFactorize.cpp b/alib2algo/test-src/grammar/parsing/LeftFactorize.cpp index 83b366f12b9619256c0f3b200154b7ad1efd678c..f5c9f254ae103dcd529a856aad77cd4b645099dc 100644 --- a/alib2algo/test-src/grammar/parsing/LeftFactorize.cpp +++ b/alib2algo/test-src/grammar/parsing/LeftFactorize.cpp @@ -58,7 +58,7 @@ void LeftFactorize::testLeftFactorize ( ) { comp.addRule ( Cp, std::vector < alphabet::Symbol > { C } ); comp.addRule ( Cp, std::vector < alphabet::Symbol > { } ); - std::cout << res << std::endl << comp << std::endl; +// std::cout << res << std::endl << comp << std::endl; CPPUNIT_ASSERT ( res == comp ); } @@ -117,7 +117,7 @@ void LeftFactorize::testLeftFactorize2 ( ) { comp.addRule ( Cp, std::vector < alphabet::Symbol > { } ); comp.addRule ( D, std::vector < alphabet::Symbol > { d } ); - std::cout << res << std::endl << comp << std::endl; +// std::cout << res << std::endl << comp << std::endl; CPPUNIT_ASSERT ( res == comp ); }