code: def _calculate_first_plans(
      nonterminal_to_dfas, first_plans, nonterminal
  ):
      """
      Calculates the first plan in the first_plans dictionary for every given
      nonterminal. This is going to be used to know when to create stack nodes.
      """
      dfas = nonterminal_to_dfas[nonterminal]
      new_first_plans = {}
      first_plans[nonterminal] = None  # dummy to detect left recursion
      # We only need to check the first dfa. All the following ones are not
      # interesting to find first terminals.
      state = dfas[0]
      for transition, next_ in state.transitions.items():
          # It's a string. We have finally found a possible first token.
          new_first_plans[transition] = [next_.next_dfa]
  
      for nonterminal2, next_ in state.nonterminal_arcs.items():
          # It's a nonterminal and we have either a left recursion issue
          # in the grammar or we have to recurse.
          try:
              first_plans2 = first_plans[nonterminal2]
          except KeyError:
              first_plans2 = _calculate_first_plans(
                  nonterminal_to_dfas, first_plans, nonterminal2
              )
          else:
              if first_plans2 is None:
                  raise ValueError(
                      "left recursion for rule %r" % nonterminal
                  )
  
          for t, pushes in first_plans2.items():
              new_first_plans[t] = [next_] + pushes
  
      first_plans[nonterminal] = new_first_plans
      return new_first_plans
  
imports: from ast import literal_eval
  from typing import TypeVar, Generic, Mapping, Sequence, Set, Union
  from parso.pgen2.grammar_parser import GrammarParser, NFAState
label_desc: Use image node_id 8 for calling a global function with example usage: _calculate_first_plans(nonterminal_to_dfas, first_plans, nonterminal) and returns: new_first_plans