import Common

def Generate(parser, filename):
    outfile = Common.CreateFile(filename, format='c')
    write = outfile.write

    last_lhs = ''
    for rule in parser.grammar.rules:
        if last_lhs != rule.lhs:
            
            last_lhs = rule.lhs
            new_lhs = 1
            write('\n%s ::=' % rule.lhs)
        else:
            indent = len(rule.lhs) + 3
            write(' '*indent + '|')
        rhs = rule.rhs or ['/* empty */']
        for symbol in rule.rhs:
            write(' %s' % symbol)
        write('\n')

    outfile.close()
    return
