-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlatexify.py
52 lines (45 loc) · 1.69 KB
/
latexify.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""Mark up a grammar for printing with latex.sty"""
from pygramm.llparse import *
from pygramm.char_classes import CharClasses
from pygramm.unit_productions import UnitProductions
from pygramm.grammar import FactorEmpty
import argparse
import sys
import logging
logging.basicConfig()
log = logging.getLogger(__name__)
log.setLevel(logging.DEBUG)
def cli() -> object:
"""Command line interface"""
parser = argparse.ArgumentParser("Convert pygramm grammar for LaTeX processing")
parser.add_argument("infile", nargs="?", default=sys.stdin,
type=argparse.FileType("r"),
help="pygramm grammar file to read")
parser.add_argument("outfile", nargs="?", default=sys.stdout,
type=argparse.FileType("w"),
help="LaTeX file to write")
parser.add_argument("--factor_units", action="store_true",
help="Factor unit productions")
parser.add_argument("--char_classes", action="store_true",
help="Form character classes")
parser.add_argument("--factor_empty",action="store_true",
help="Factor empty rhs to a single non-terminal")
args = parser.parse_args()
return args
def main():
args = cli()
f = args.infile
gram = parse(f)
gram.finalize()
if args.factor_units:
xform = UnitProductions(gram)
xform.transform_all_rhs(gram)
if args.factor_empty:
xform = FactorEmpty(gram)
xform.transform_all_rhs(gram)
if args.char_classes:
xform = CharClasses(gram)
xform.transform_all_rhs(gram)
print(gram.latex(), file=args.outfile)
if __name__ == "__main__":
main()