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

first test of AbsorbTerminalSymbol alg.

parent 5cd304ef
No related branches found
No related tags found
No related merge requests found
......@@ -17,6 +17,8 @@ namespace parsing {
alphabet::Symbol AbsorbTerminalSymbol::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 ( ) );
 
res.addNonterminalSymbol ( newSymbol );
for ( const std::vector < alphabet::Symbol > & rhs : orig.getRules ( ).find ( nonterminal )->second ) {
std::vector < alphabet::Symbol > newRHS ( rhs.begin ( ), rhs.end ( ) );
newRHS.push_back ( terminal );
......@@ -32,11 +34,10 @@ void AbsorbTerminalSymbol::absorbTerminalSymbol ( grammar::CFG & grammar, const
res.setNonterminalAlphabet ( grammar.getNonterminalAlphabet ( ) );
res.setTerminalAlphabet ( grammar.getTerminalAlphabet ( ) );
 
std::map < alphabet::Symbol, alphabet::Symbol > nonterminalsPrimed; // terminal is fixed in particular calls
std::map < alphabet::Symbol, alphabet::Symbol > nonterminalsPrimed; // terminal is fixed in particular calls
 
for ( const alphabet::Symbol & nonterminal : nonterminals ) {
alphabet::Symbol newSymbol = handleAbsobtion ( grammar, res, nonterminal, terminal );
res.addNonterminalSymbol ( newSymbol );
nonterminalsPrimed.insert ( std::make_pair ( nonterminal, newSymbol ) );
}
 
......@@ -46,8 +47,8 @@ void AbsorbTerminalSymbol::absorbTerminalSymbol ( grammar::CFG & grammar, const
for ( const std::vector < alphabet::Symbol > & rhs : rule.second ) {
std::vector < alphabet::Symbol > newRHS;
 
for ( std::vector < alphabet::Symbol >::const_iterator iter = rhs.begin ( ); iter + 1 != rhs.end ( ); ++iter ) {
if ( nonterminals.count ( * iter ) && ( terminal == * ( iter + 1 ) ) ) {
for ( std::vector < alphabet::Symbol >::const_iterator iter = rhs.begin ( ); iter != rhs.end ( ); ++iter ) {
if ( ( iter + 1 != rhs.end ( ) ) && nonterminals.count ( * iter ) && ( terminal == * ( iter + 1 ) ) ) {
newRHS.push_back ( nonterminalsPrimed.find ( * iter )->second );
++iter;
} else {
......
......@@ -47,11 +47,12 @@ bool HandleFirstFollowConflict::handleFirstFollowConflict ( grammar::CFG & gramm
// find whether all occurrences of a symbol in symbolsEndingWithNonterminal are followed by terminal symbol (the paremeter)
for ( const std::pair < alphabet::Symbol, std::set < std::vector < alphabet::Symbol > > > & rule : grammar.getRules ( ) )
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 ( symbolsEndingWithNonterminal.count ( * iter ) && grammar.getNonterminalAlphabet ( ).count ( * ( iter + 1 ) ) && First::first ( grammar, std::vector < alphabet::Symbol > ( iter + 1, rhs.end ( ) ) ).count ( terminal ) ) {
ExtractRightContext::extractRightContext ( grammar, terminal, symbolsEndingWithNonterminal );
return true;
}
if ( rhs.size ( ) > 0 )
for ( std::vector < alphabet::Symbol >::const_iterator iter = rhs.begin ( ); iter + 1 != rhs.end ( ); ++iter )
if ( symbolsEndingWithNonterminal.count ( * iter ) && grammar.getNonterminalAlphabet ( ).count ( * ( iter + 1 ) ) && First::first ( grammar, std::vector < alphabet::Symbol > ( iter + 1, rhs.end ( ) ) ).count ( terminal ) ) {
ExtractRightContext::extractRightContext ( grammar, terminal, symbolsEndingWithNonterminal );
return true;
}
 
}
 
......
#include "AbsorbTerminalSymbol.h"
#include "grammar/ContextFree/CFG.h"
#include <alphabet/SymbolPairSymbol.h>
#include "grammar/parsing/AbsorbTerminalSymbol.h"
#include "factory/StringDataFactory.hpp"
CPPUNIT_TEST_SUITE_NAMED_REGISTRATION ( AbsorbTerminalSymbol, "grammar" );
CPPUNIT_TEST_SUITE_REGISTRATION ( AbsorbTerminalSymbol );
void AbsorbTerminalSymbol::setUp ( ) {
}
void AbsorbTerminalSymbol::tearDown ( ) {
}
void AbsorbTerminalSymbol::testAbsorbTerminalSymbol ( ) {
alphabet::Symbol A = alphabet::symbolFrom ( "A" );
alphabet::Symbol B = alphabet::symbolFrom ( "B" );
alphabet::Symbol C = alphabet::symbolFrom ( "C" );
alphabet::Symbol a = alphabet::symbolFrom ( 'a' );
alphabet::Symbol b = alphabet::symbolFrom ( 'b' );
alphabet::Symbol c = alphabet::symbolFrom ( 'c' );
alphabet::Symbol Ba = alphabet::createUniqueSymbol ( alphabet::Symbol ( alphabet::SymbolPairSymbol ( std::make_pair ( B, a ) ) ), { }, { } );
grammar::CFG grammar ( A );
grammar.setTerminalAlphabet ( { a, b, c } );
grammar.setNonterminalAlphabet ( { A, B, C } );
grammar.setInitialSymbol ( A );
grammar.addRule ( A, { B, a, C } );
grammar.addRule ( B, { } );
grammar.addRule ( B, { a, a, C } );
grammar.addRule ( C, { c } );
grammar.addRule ( C, { b, C } );
grammar::CFG res = grammar;
grammar::parsing::AbsorbTerminalSymbol::absorbTerminalSymbol ( res, a, { B } );
grammar::CFG comp ( A );
comp.setTerminalAlphabet ( { a, b, c } );
comp.setNonterminalAlphabet ( { A, B, Ba, C } );
comp.setInitialSymbol ( A );
comp.addRule ( A, { Ba, C } );
comp.addRule ( B, { } );
comp.addRule ( B, { a, a, C } );
comp.addRule ( Ba, { a } );
comp.addRule ( Ba, { a, a, C, a } );
comp.addRule ( C, { c } );
comp.addRule ( C, { b, C } );
std::cout << alib::StringDataFactory::toString ( grammar::Grammar ( res ) ) << std::endl << alib::StringDataFactory::toString ( grammar::Grammar ( comp ) ) << std::endl;
CPPUNIT_ASSERT ( res == comp );
}
void AbsorbTerminalSymbol::testAbsorbTerminalSymbol2 ( ) {
}
#ifndef ABSORB_TERMINAL_SYMBOL_TEST_H_
#define ABSORB_TERMINAL_SYMBOL_TEST_H_
#include <cppunit/extensions/HelperMacros.h>
class AbsorbTerminalSymbol : public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE ( AbsorbTerminalSymbol );
CPPUNIT_TEST ( testAbsorbTerminalSymbol );
CPPUNIT_TEST ( testAbsorbTerminalSymbol2 );
CPPUNIT_TEST_SUITE_END ( );
public:
void setUp ( );
void tearDown ( );
void testAbsorbTerminalSymbol ( );
void testAbsorbTerminalSymbol2 ( );
};
#endif /* ABSORB_TERMINAL_SYMBOL_TEST_H_ */
......@@ -51,7 +51,7 @@ void ExtractRightContext::testExtractRightContext ( ) {
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;
// std::cout << res << std::endl << comp << std::endl;
 
CPPUNIT_ASSERT ( res == comp );
}
......@@ -98,7 +98,7 @@ void ExtractRightContext::testExtractRightContext2 ( ) {
comp.addRule ( C, std::vector < alphabet::Symbol > { A, a, b } );
comp.addRule ( C, std::vector < alphabet::Symbol > { c, C } );
 
std::cout << res << std::endl << comp << std::endl;
// std::cout << res << std::endl << comp << std::endl;
 
CPPUNIT_ASSERT ( res == comp );
}
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