Skip to content
Snippets Groups Projects
Commit f87d9ef6 authored by Tomáš Pecka's avatar Tomáš Pecka
Browse files

atrim: fix and improve grammar trimming

parent 1354c7e9
No related branches found
No related tags found
No related merge requests found
......@@ -36,7 +36,7 @@ void help( void )
 
int main(int argc, char* argv[])
{
bool del_dead_only = true, del_unreachable_only = true;
bool del_dead_only = false, del_unreachable_only = false;
 
static struct option long_options[] = {
{"help", no_argument, NULL, 'h'},
......
......@@ -23,20 +23,12 @@ set<Symbol> ContextFreeGrammarTransformations::getProductiveNonTerminals( const
while( true )
{
Ni.push_back( Ni.at( i - 1 ) );
bool valid = true;
 
for( const auto & rule : grammar.getRules( ) )
{
for( const auto & symbol : rule.getRightSide( ) )
{
if( ! isInSet( symbol, Ni.at( i - 1 ) ) && ! isInSet( symbol, grammar.getTerminalSymbols( ) ) )
{
valid = false;
break;
}
}
if( valid )
if( all_of( rule.getRightSide( ).begin( ), rule.getRightSide( ).end( ), [ i, Ni, grammar ]( Symbol const& symbol ) -> bool {
return isInSet( symbol, Ni.at( i - 1 ) ) || isInSet( symbol, grammar.getTerminalSymbols( ) );
} ) )
Ni.at( i ).insert( rule.getLeftSide( ).front( ) );
}
 
......@@ -57,7 +49,7 @@ bool ContextFreeGrammarTransformations::isLanguageEmpty( const grammar::ContextF
 
ContextFreeGrammar ContextFreeGrammarTransformations::removeUnreachableSymbols( const ContextFreeGrammar & grammar )
{
// 1.
// 1
deque<set<Symbol>> Vi;
Vi.push_back( set<Symbol>( ) );
Vi.at( 0 ).insert( grammar.getStartSymbol( ) );
......@@ -73,10 +65,7 @@ ContextFreeGrammar ContextFreeGrammarTransformations::removeUnreachableSymbols(
{
if( isInSet( rule.getLeftSide( ).front( ), Vi.at( i - 1 ) ) )
{
for( const auto & symbol : rule.getRightSide( ) )
{
Vi.at( i ).insert( symbol );
}
Vi.at( i ).insert( rule.getRightSide( ).begin( ), rule.getRightSide( ).end( ) );
}
}
 
......@@ -103,8 +92,9 @@ ContextFreeGrammar ContextFreeGrammarTransformations::removeUnreachableSymbols(
// A->\alpha: if A \in N' and \alpha in V_i*, then A->\alpha in P
for( const auto & rule : grammar.getRules( ) )
{
if( isInSet( rule.getLeftSide( ).front( ) , ret.getNonTerminalSymbols( ) ) &&
all_of( rule.getRightSide( ).begin( ), rule.getRightSide( ).end( ), [ Vi, i ]( const Symbol & symb ){ return isInSet( symb, Vi.at( i ) ); } ) )
if( isInSet( rule.getLeftSide( ).front( ) , newNonTerminals ) && all_of( rule.getRightSide( ).begin( ), rule.getRightSide( ).end( ), [ Vi, i ]( Symbol const& symb ) -> bool {
return isInSet( symb, Vi.at( i ) );
} ) )
{
ret.addRule( rule );
}
......@@ -119,6 +109,7 @@ ContextFreeGrammar ContextFreeGrammarTransformations::removeUnproductiveSymbols(
{
// 1.
set<Symbol> Nt = getProductiveNonTerminals( grammar );
ContextFreeGrammar G1;
 
for( const auto & symbol : Nt )
......
#!/usr/bin/env bash
 
TESTCASE_ITERATIONS=20
TESTCASE_ITERATIONS=200
TESTCASE_TIMEOUT=5
LOGFILE="log_tests.txt"
 
......
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