From f87d9ef62b3295ddf6a861209f225691db4ca67d Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Pecka?= <peckato1@fit.cvut.cz>
Date: Sat, 26 Apr 2014 10:07:58 +0200
Subject: [PATCH] atrim: fix and improve grammar trimming

---
 atrim/src/atrim.cpp                           |  2 +-
 .../ContextFreeGrammarTransformations.cpp     | 27 +++++++------------
 tests.aconversion.sh                          |  2 +-
 3 files changed, 11 insertions(+), 20 deletions(-)

diff --git a/atrim/src/atrim.cpp b/atrim/src/atrim.cpp
index ec39783844..9f79a4d6cf 100644
--- a/atrim/src/atrim.cpp
+++ b/atrim/src/atrim.cpp
@@ -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'},
diff --git a/atrim/src/grammar/ContextFreeGrammarTransformations.cpp b/atrim/src/grammar/ContextFreeGrammarTransformations.cpp
index 553fabefc0..aa4af83ded 100644
--- a/atrim/src/grammar/ContextFreeGrammarTransformations.cpp
+++ b/atrim/src/grammar/ContextFreeGrammarTransformations.cpp
@@ -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 )
diff --git a/tests.aconversion.sh b/tests.aconversion.sh
index b8aab466f8..e32ddafe2f 100755
--- a/tests.aconversion.sh
+++ b/tests.aconversion.sh
@@ -1,6 +1,6 @@
 #!/usr/bin/env bash
 
-TESTCASE_ITERATIONS=20
+TESTCASE_ITERATIONS=200
 TESTCASE_TIMEOUT=5
 LOGFILE="log_tests.txt"
 
-- 
GitLab