def generate(inputfile, outputfile=None, dump=0, **flags):

    inputfilename = inputfile if isinstance(
        inputfile, string_types ) else inputfile.name
    if not outputfile:
        if inputfilename.endswith('.g'):
            outputfile = inputfilename[:-2] + '.py'
        else:
            raise Exception('Must specify output filename if input filename is not *.g')

    DIVIDER = '\n%%\n' # This pattern separates the pre/post parsers
    preparser, postparser = None, None # Code before and after the parser desc

    # Read the entire file
    if isinstance(inputfile, string_types):
        inputfile = open(inputfilename)
    s = inputfile.read()

    # See if there's a separation between the pre-parser and parser
    f = s.find(DIVIDER)
    if f >= 0: preparser, s = s[:f]+'\n\n', s[f+len(DIVIDER):]

    # See if there's a separation between the parser and post-parser
    f = s.find(DIVIDER)
    if f >= 0: s, postparser = s[:f], '\n\n'+s[f+len(DIVIDER):]

    # Create the parser and scanner and parse the text
    scanner = grammar.ParserDescriptionScanner(s, filename=inputfilename)
    if preparser: scanner.del_line += preparser.count('\n')

    parser = grammar.ParserDescription(scanner)
    t = runtime.wrap_error_reporter(parser, 'Parser')
    if t is None: return 1 # Failure
    if preparser is not None: t.preparser = preparser
    if postparser is not None: t.postparser = postparser

    # Add command line options to the set
    t.options.update(flags)

    # Generate the output
    if dump:
        t.dump_information()
    else:
        if isinstance(outputfile, string_types):
            outputfile = open(outputfile, 'w')
        t.output = outputfile
        t.generate_output()
    return 0
Generate a grammar, given an input filename (X.g)
dataset/ETHPy150Open smurfix/yapps/yapps/cli_tool.py/generate