def write_nonterminal_arcs(start_state, loop_state, next_state, nonterminals, left_context_phones):
    'This function relates to the grammar-decoding setup, see\n    kaldi-asr.org/doc/grammar.html.  It is called from write_fst_no_silence\n    and write_fst_silence, and writes to the stdout some extra arcs\n    in the lexicon FST that relate to nonterminal symbols.\n    See the section "Special symbols in L.fst,\n    kaldi-asr.org/doc/grammar.html#grammar_special_l.\n       start_state: the start-state of L.fst.\n       loop_state:  the state of high out-degree in L.fst where words leave\n                  and enter.\n       next_state: the number from which this function can start allocating its\n                  own states.  the updated value of next_state will be returned.\n       nonterminals: the user-defined nonterminal symbols as a list of\n          strings, e.g. [\'#nonterm:contact_list\', ... ].\n       left_context_phones: a list of phones that may appear as left-context,\n          e.g. [\'a\', \'ah\', ... \'#nonterm_bos\'].\n    '
    shared_state = next_state
    next_state += 1
    final_state = next_state
    next_state += 1
    print('{src}\t{dest}\t{phone}\t{word}\t{cost}'.format(src=start_state, dest=shared_state, phone='#nonterm_begin', word='#nonterm_begin', cost=0.0))
    for nonterminal in nonterminals:
        print('{src}\t{dest}\t{phone}\t{word}\t{cost}'.format(src=loop_state, dest=shared_state, phone=nonterminal, word=nonterminal, cost=0.0))
    this_cost = (- math.log((1.0 / len(left_context_phones))))
    for left_context_phone in left_context_phones:
        print('{src}\t{dest}\t{phone}\t{word}\t{cost}'.format(src=shared_state, dest=loop_state, phone=left_context_phone, word='<eps>', cost=this_cost))
    print('{src}\t{dest}\t{phone}\t{word}\t{cost}'.format(src=loop_state, dest=final_state, phone='#nonterm_end', word='#nonterm_end', cost=0.0))
    print('{state}\t{final_cost}'.format(state=final_state, final_cost=0.0))
    return next_state
This function relates to the grammar-decoding setup, see
kaldi-asr.org/doc/grammar.html.  It is called from write_fst_no_silence
and write_fst_silence, and writes to the stdout some extra arcs
in the lexicon FST that relate to nonterminal symbols.
See the section "Special symbols in L.fst,
kaldi-asr.org/doc/grammar.html#grammar_special_l.
   start_state: the start-state of L.fst.
   loop_state:  the state of high out-degree in L.fst where words leave
              and enter.
   next_state: the number from which this function can start allocating its
              own states.  the updated value of next_state will be returned.
   nonterminals: the user-defined nonterminal symbols as a list of
      strings, e.g. ['#nonterm:contact_list', ... ].
   left_context_phones: a list of phones that may appear as left-context,
      e.g. ['a', 'ah', ... '#nonterm_bos'].
def write_nonterminal_arcs(start_state, loop_state, next_state, nonterminals, left_context_phones):
    'This function relates to the grammar-decoding setup, see\n    kaldi-asr.org/doc/grammar.html.  It is called from write_fst_no_silence\n    and write_fst_silence, and writes to the stdout some extra arcs\n    in the lexicon FST that relate to nonterminal symbols.\n    See the section "Special symbols in L.fst,\n    kaldi-asr.org/doc/grammar.html#grammar_special_l.\n       start_state: the start-state of L.fst.\n       loop_state:  the state of high out-degree in L.fst where words leave\n                  and enter.\n       next_state: the number from which this function can start allocating its\n                  own states.  the updated value of next_state will be returned.\n       nonterminals: the user-defined nonterminal symbols as a list of\n          strings, e.g. [\'#nonterm:contact_list\', ... ].\n       left_context_phones: a list of phones that may appear as left-context,\n          e.g. [\'a\', \'ah\', ... \'#nonterm_bos\'].\n    '
    shared_state = next_state
    next_state += 1
    final_state = next_state
    next_state += 1
    print('{src}\t{dest}\t{phone}\t{word}\t{cost}'.format(src=start_state, dest=shared_state, phone='#nonterm_begin', word='#nonterm_begin', cost=0.0))
    for nonterminal in nonterminals:
        print('{src}\t{dest}\t{phone}\t{word}\t{cost}'.format(src=loop_state, dest=shared_state, phone=nonterminal, word=nonterminal, cost=0.0))
    this_cost = (- math.log((1.0 / len(left_context_phones))))
    for left_context_phone in left_context_phones:
        print('{src}\t{dest}\t{phone}\t{word}\t{cost}'.format(src=shared_state, dest=loop_state, phone=left_context_phone, word='<eps>', cost=this_cost))
    print('{src}\t{dest}\t{phone}\t{word}\t{cost}'.format(src=loop_state, dest=final_state, phone='#nonterm_end', word='#nonterm_end', cost=0.0))
    print('{state}\t{final_cost}'.format(state=final_state, final_cost=0.0))
    return next_state