mirror of
https://github.com/python/cpython.git
synced 2024-11-28 04:15:11 +08:00
71876fa438
* Refactor Parser/pgen and add documentation and explanations To improve the readability and maintainability of the parser generator perform the following transformations: * Separate the metagrammar parser in its own class to simplify the parser generator logic. * Create separate classes for DFAs and NFAs and move methods that act exclusively on them from the parser generator to these classes. * Add docstrings and comment documenting the process to go from the grammar file into NFAs and then DFAs. Detail some of the algorithms and give some background explanations of some concepts that will helps readers not familiar with the parser generation process. * Select more descriptive names for some variables and variables. * PEP8 formatting and quote-style homogenization. The output of the parser generator remains the same (Include/graminit.h and Python/graminit.c remain untouched by running the new parser generator).
34 lines
1019 B
Python
34 lines
1019 B
Python
import argparse
|
|
|
|
from .pgen import ParserGenerator
|
|
|
|
|
|
def main():
|
|
parser = argparse.ArgumentParser(description="Parser generator main program.")
|
|
parser.add_argument(
|
|
"grammar", type=str, help="The file with the grammar definition in EBNF format"
|
|
)
|
|
parser.add_argument("tokens", type=str, help="The file with the token definitions")
|
|
parser.add_argument(
|
|
"graminit_h",
|
|
type=argparse.FileType("w"),
|
|
help="The path to write the grammar's non-terminals as #defines",
|
|
)
|
|
parser.add_argument(
|
|
"graminit_c",
|
|
type=argparse.FileType("w"),
|
|
help="The path to write the grammar as initialized data",
|
|
)
|
|
|
|
parser.add_argument("--verbose", "-v", action="count")
|
|
args = parser.parse_args()
|
|
|
|
p = ParserGenerator(args.grammar, args.tokens, verbose=args.verbose)
|
|
grammar = p.make_grammar()
|
|
grammar.produce_graminit_h(args.graminit_h.write)
|
|
grammar.produce_graminit_c(args.graminit_c.write)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|