def p_rules(p):
 if (len(p) == 2):
  rule = p[1]
 else:
  rule = p[2]
 embedded = []
 embed_count = 0
 rulename = rule[0]
 rulecount = 1
 for r in rule[1]:
  print ('def p_%s_%d(p):' % (rulename, rulecount))
  prod = []
  prodcode = ''
  for i in range(len(r)):
   item = r[i]
   if (item[0] == '{'):
    if (i == (len(r) - 1)):
     prodcode = item
     break
    else:
     embed_name = ('_embed%d_%s' % (embed_count, rulename))
     prod.append(embed_name)
     embedded.append((embed_name, item))
     embed_count += 1
   else:
    prod.append(item)
  print ("    '''%s : %s'''" % (rulename, ' '.join(prod)))
  print_code(prodcode, 4)
  print
  rulecount += 1
 for (e, code) in embedded:
  print ('def p_%s(p):' % e)
  print ("    '''%s : '''" % e)
  print_code(code, 4)
  print

def p_rules ( p ) : if ( len ( p ) == 2 ) : rule = p [ 1 ] else : rule = p [ 2 ] embedded = [ ] embed_count = 0 rulename = rule [ 0 ] rulecount = 1 for r in rule [ 1 ] : print ( 'def p_%s_%d(p):' % ( rulename , rulecount ) ) prod = [ ] prodcode = '' for i in range ( len ( r ) ) : item = r [ i ] if ( item [ 0 ] == '{' ) : if ( i == ( len ( r ) - 1 ) ) : prodcode = item break else : embed_name = ( '_embed%d_%s' % ( embed_count , rulename ) ) prod . append ( embed_name ) embedded . append ( ( embed_name , item ) ) embed_count += 1 else : prod . append ( item ) print ( "    '''%s : %s'''" % ( rulename , ' ' . join ( prod ) ) ) print_code ( prodcode , 4 ) print rulecount += 1 for ( e , code ) in embedded : print ( 'def p_%s(p):' % e ) print ( "    '''%s : '''" % e ) print_code ( code , 4 ) print
rules : rules rule | rule .