-
Notifications
You must be signed in to change notification settings - Fork 0
/
lp2tex.py
37 lines (27 loc) · 945 Bytes
/
lp2tex.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
# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import sys
import re
with open(sys.argv[1], 'r') as theFile:
lpCode = theFile.read()
# Regex for inference rules
infer_r = r"(.*?):-(.*?)\."
infer_s = r"\\infer{\g<1>}{\g<2>}"
# Regex for idents
ident_r = r"(^|\n|\r|\t|\W)([a-z]\w*)"
ident_s = r"\g<1>\\texttt{\g<2>}"
# Regex for lone backaslahes
back_r = r"\\(?!texttt)"
back_s = r"\\backslash"
# First we add \texttt around identifiers
step1 = re.sub(
ident_r, ident_s, lpCode, 0, re.MULTILINE)
# Then we replace \ by \backslash (except for \texttt of course)
step2 = re.sub(
back_r, back_s, step1, 0, re.MULTILINE)
# Then we substitute various symbols by their latex version
step3 = step2.replace(
"=>", "\\Rightarrow").replace(r" ", "~")
# Eventually we replace :- clauses by inference rules
result = re.sub(infer_r, infer_s, step3, 0, re.MULTILINE)
print(result)