• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import argparse
2
3from .pgen import ParserGenerator
4
5
6def main():
7    parser = argparse.ArgumentParser(description="Parser generator main program.")
8    parser.add_argument(
9        "grammar", type=str, help="The file with the grammar definition in EBNF format"
10    )
11    parser.add_argument("tokens", type=str, help="The file with the token definitions")
12    parser.add_argument(
13        "graminit_h",
14        type=argparse.FileType("w"),
15        help="The path to write the grammar's non-terminals as #defines",
16    )
17    parser.add_argument(
18        "graminit_c",
19        type=argparse.FileType("w"),
20        help="The path to write the grammar as initialized data",
21    )
22
23    parser.add_argument("--verbose", "-v", action="count")
24    parser.add_argument(
25        "--graph",
26        type=argparse.FileType("w"),
27        action="store",
28        metavar="GRAPH_OUTPUT_FILE",
29        help="Dumps a DOT representation of the generated automata in a file",
30    )
31
32    args = parser.parse_args()
33
34    p = ParserGenerator(
35        args.grammar, args.tokens, verbose=args.verbose, graph_file=args.graph
36    )
37    grammar = p.make_grammar()
38    grammar.produce_graminit_h(args.graminit_h.write)
39    grammar.produce_graminit_c(args.graminit_c.write)
40
41
42if __name__ == "__main__":
43    main()
44