class_docstr: A simple bottom-up CFG parser that uses two operations, "shift"
  and "reduce", to find a single parse for a text.
  
  ``ShiftReduceParser`` maintains a stack, which records the
  structure of a portion of the text.  This stack is a list of
  strings and Trees that collectively cover a portion of
  the text.  For example, while parsing the sentence "the dog saw
  the man" with a typical grammar, ``ShiftReduceParser`` will produce
  the following stack, which covers "the dog saw"::
  
     [(NP: (Det: 'the') (N: 'dog')), (V: 'saw')]
  
  ``ShiftReduceParser`` attempts to extend the stack to cover the
  entire text, and to combine the stack elements into a single tree,
  producing a complete parse for the sentence.
  
  Initially, the stack is empty.  It is extended to cover the text,
  from left to right, by repeatedly applying two operations:
  
    - "shift" moves a token from the beginning of the text to the
      end of the stack.
    - "reduce" uses a CFG production to combine the rightmost stack
      elements into a single Tree.
  
  Often, more than one operation can be performed on a given stack.
  In this case, ``ShiftReduceParser`` uses the following heuristics
  to decide which operation to perform:
  
    - Only shift if no reductions are available.
    - If multiple reductions are available, then apply the reduction
      whose CFG production is listed earliest in the grammar.
  
  Note that these heuristics are not guaranteed to choose an
  operation that leads to a parse of the text.  Also, if multiple
  parses exists, ``ShiftReduceParser`` will return at most one of
  them.
  
  :see: ``nltk.grammar``
class_docstr_tok: ["A","simple","bottom-up","CFG","parser","that","uses","two","operations",",","``","shift","''","and","``","reduce","''",",","to","find","a","single","parse","for","a","text",".","``","ShiftReduceParser","``","maintains","a","stack",",","which","records","the","structure","of","a","portion","of","the","text",".","This","stack","is","a","list","of","strings","and","Trees","that","collectively","cover","a","portion","of","the","text",".","For","example",",","while","parsing","the","sentence","``","the","dog","saw","the","man","''","with","a","typical","grammar",",","``","ShiftReduceParser","``","will","produce","the","following","stack",",","which","covers","``","the","dog","saw","''",":",":","[","(","NP",":","(","Det",":","'the","'",")","(","N",":","'dog","'",")",")",",","(","V",":","'saw","'",")","]","``","ShiftReduceParser","``","attempts","to","extend","the","stack","to","cover","the","entire","text",",","and","to","combine","the","stack","elements","into","a","single","tree",",","producing","a","complete","parse","for","the","sentence",".","Initially",",","the","stack","is","empty",".","It","is","extended","to","cover","the","text",",","from","left","to","right",",","by","repeatedly","applying","two","operations",":","-","``","shift","''","moves","a","token","from","the","beginning","of","the","text","to","the","end","of","the","stack",".","-","``","reduce","''","uses","a","CFG","production","to","combine","the","rightmost","stack","elements","into","a","single","Tree",".","Often",",","more","than","one","operation","can","be","performed","on","a","given","stack",".","In","this","case",",","``","ShiftReduceParser","``","uses","the","following","heuristics","to","decide","which","operation","to","perform",":","-","Only","shift","if","no","reductions","are","available",".","-","If","multiple","reductions","are","available",",","then","apply","the","reduction","whose","CFG","production","is","listed","earliest","in","the","grammar",".","Note","that","these","heuristics","are","not","guaranteed","to","choose","an","operation","that","leads","to","a","parse","of","the","text",".","Also",",","if","multiple","parses","exists",",","``","ShiftReduceParser","``","will","return","at","most","one","of","them",".",":","see",":","``","nltk.grammar","``"]
docstr: Find a CFG production whose right hand side matches the
  rightmost stack elements; and combine those stack elements
  into a single Tree, with the node specified by the
  production's left-hand side.  If more than one CFG production
  matches the stack, then use the production that is listed
  earliest in the grammar.  The new Tree replaces the
  elements in the stack.
  
  :rtype: Production or None
  :return: If a reduction is performed, then return the CFG
      production that the reduction is based on; otherwise,
      return false.
  :type stack: list(string and Tree)
  :param stack: A list of strings and Trees, encoding
      the structure of the text that has been parsed so far.
  :type remaining_text: list(str)
  :param remaining_text: The portion of the text that is not yet
      covered by ``stack``.
docstr_tok: ["Find","a","CFG","production","whose","right","hand","side","matches","the","rightmost","stack","elements",";","and","combine","those","stack","elements","into","a","single","Tree",",","with","the","node","specified","by","the","production","'s","left-hand","side",".","If","more","than","one","CFG","production","matches","the","stack",",","then","use","the","production","that","is","listed","earliest","in","the","grammar",".","The","new","Tree","replaces","the","elements","in","the","stack",".",":","rtype",":","Production","or","None",":","return",":","If","a","reduction","is","performed",",","then","return","the","CFG","production","that","the","reduction","is","based","on",";","otherwise",",","return","false",".",":","type","stack",":","list","(","string","and","Tree",")",":","param","stack",":","A","list","of","strings","and","Trees",",","encoding","the","structure","of","the","text","that","has","been","parsed","so","far",".",":","type","remaining_text",":","list","(","str",")",":","param","remaining_text",":","The","portion","of","the","text","that","is","not","yet","covered","by","``","stack","``","."]
code: def _reduce(self, stack, remaining_text, production=None):
      """
      Find a CFG production whose right hand side matches the
      rightmost stack elements; and combine those stack elements
      into a single Tree, with the node specified by the
      production's left-hand side.  If more than one CFG production
      matches the stack, then use the production that is listed
      earliest in the grammar.  The new Tree replaces the
      elements in the stack.
  
      :rtype: Production or None
      :return: If a reduction is performed, then return the CFG
          production that the reduction is based on; otherwise,
          return false.
      :type stack: list(string and Tree)
      :param stack: A list of strings and Trees, encoding
          the structure of the text that has been parsed so far.
      :type remaining_text: list(str)
      :param remaining_text: The portion of the text that is not yet
          covered by ``stack``.
      """
      if production is None:
          productions = self._grammar.productions()
      else:
          productions = [production]
  
      # Try each production, in order.
      for production in productions:
          rhslen = len(production.rhs())
  
          # check if the RHS of a production matches the top of the stack
          if self._match_rhs(production.rhs(), stack[-rhslen:]):
              # combine the tree to reflect the reduction
              tree = Tree(production.lhs().symbol(), stack[-rhslen:])
              stack[-rhslen:] = [tree]
  
              # We reduced something
              if self._trace:
                  self._trace_reduce(stack, production, remaining_text)
              return production
  
      # We didn't reduce anything
      return None
  
imports: from nltk.grammar import Nonterminal
  from nltk.parse.api import ParserI
  from nltk.tree import Tree
label_desc: Use image node_id 6 for calling the ShiftReduceParser obj's underlying member method code with example usage: obj._reduce(stack, remaining_text, production) and returns: None, production