
from grammpy import Grammar


def _copy_grammar(grammar):
    return Grammar(terminals=(item.s for item in grammar.terms()),
                   nonterminals=grammar.nonterms(),
                   rules=grammar.rules(),
                   start_symbol=grammar.start_get())


def remove_nongenerating_symbol(grammar: Grammar, transform_grammar=False) -> Grammar:
    if transform_grammar is False:
        grammar = _copy_grammar(grammar)
    generates = set(item.s for item in grammar.terms())
    while True:
        additional = generates.copy()
        for rule in grammar.rules():
            rightPart = rule.right
            allIn = True
            for symbol in rightPart:
                if symbol not in generates:
                    allIn = False
            if not allIn:
                continue
            for symbol in rule.left:
                additional.add(symbol)
        if additional == generates:
            break
        generates = additional
    allNonterms = list(grammar.nonterms())
    for nonterm in allNonterms:
        if nonterm not in generates:
            grammar.remove_nonterm(nonterm)
    return grammar


from copy import copy
from grammpy import Grammar


def _copy_grammar(grammar):
    return copy(grammar)

def remove_nongenerating_symbol(grammar: Grammar, transform_grammar=False) -> Grammar:
    if transform_grammar is False:
        grammar = _copy_grammar(grammar)
    generates = set(item.s for item in grammar.terms())
    while True:
        additional = generates.copy()
        for rule in grammar.rules():
            rightPart = rule.right
            allIn = True
            for symbol in rightPart:
                if symbol not in generates:
                    allIn = False
            if not allIn:
                continue
            for symbol in rule.left:
                additional.add(symbol)
        if additional == generates:
            break
        generates = additional
    allNonterms = list(grammar.nonterms())
    for nonterm in allNonterms:
        if nonterm not in generates:
            grammar.remove_nonterm(nonterm)
    return grammar

Switch to new version of grammpy (1.1.2) and use copy method
Switch to new version of grammpy (1.1.2) and use copy method

  
+ from copy import copy
  from grammpy import Grammar
  
  
  def _copy_grammar(grammar):
+     return copy(grammar)
-     return Grammar(terminals=(item.s for item in grammar.terms()),
-                    nonterminals=grammar.nonterms(),
-                    rules=grammar.rules(),
-                    start_symbol=grammar.start_get())
- 
  
  def remove_nongenerating_symbol(grammar: Grammar, transform_grammar=False) -> Grammar:
      if transform_grammar is False:
          grammar = _copy_grammar(grammar)
      generates = set(item.s for item in grammar.terms())
      while True:
          additional = generates.copy()
          for rule in grammar.rules():
              rightPart = rule.right
              allIn = True
              for symbol in rightPart:
                  if symbol not in generates:
                      allIn = False
              if not allIn:
                  continue
              for symbol in rule.left:
                  additional.add(symbol)
          if additional == generates:
              break
          generates = additional
      allNonterms = list(grammar.nonterms())
      for nonterm in allNonterms:
          if nonterm not in generates:
              grammar.remove_nonterm(nonterm)
      return grammar
  
Switch to new version of grammpy (1.1.2) and use copy method
## Code Before:

from grammpy import Grammar


def _copy_grammar(grammar):
    return Grammar(terminals=(item.s for item in grammar.terms()),
                   nonterminals=grammar.nonterms(),
                   rules=grammar.rules(),
                   start_symbol=grammar.start_get())


def remove_nongenerating_symbol(grammar: Grammar, transform_grammar=False) -> Grammar:
    if transform_grammar is False:
        grammar = _copy_grammar(grammar)
    generates = set(item.s for item in grammar.terms())
    while True:
        additional = generates.copy()
        for rule in grammar.rules():
            rightPart = rule.right
            allIn = True
            for symbol in rightPart:
                if symbol not in generates:
                    allIn = False
            if not allIn:
                continue
            for symbol in rule.left:
                additional.add(symbol)
        if additional == generates:
            break
        generates = additional
    allNonterms = list(grammar.nonterms())
    for nonterm in allNonterms:
        if nonterm not in generates:
            grammar.remove_nonterm(nonterm)
    return grammar

## Instruction:
Switch to new version of grammpy (1.1.2) and use copy method
## Code After:

from copy import copy
from grammpy import Grammar


def _copy_grammar(grammar):
    return copy(grammar)

def remove_nongenerating_symbol(grammar: Grammar, transform_grammar=False) -> Grammar:
    if transform_grammar is False:
        grammar = _copy_grammar(grammar)
    generates = set(item.s for item in grammar.terms())
    while True:
        additional = generates.copy()
        for rule in grammar.rules():
            rightPart = rule.right
            allIn = True
            for symbol in rightPart:
                if symbol not in generates:
                    allIn = False
            if not allIn:
                continue
            for symbol in rule.left:
                additional.add(symbol)
        if additional == generates:
            break
        generates = additional
    allNonterms = list(grammar.nonterms())
    for nonterm in allNonterms:
        if nonterm not in generates:
            grammar.remove_nonterm(nonterm)
    return grammar
