Skip to content

Instantly share code, notes, and snippets.

@CCXXXI
Last active June 20, 2022 06:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CCXXXI/48bf2f7f35068d964a6b27a71e1fa13a to your computer and use it in GitHub Desktop.
Save CCXXXI/48bf2f7f35068d964a6b27a71e1fa13a to your computer and use it in GitHub Desktop.
import json
from collections import defaultdict
# read data.json
data = json.load(open("data.json"))
d = defaultdict(dict)
for col in data:
col: dict
n = col["Column1"]
for k in col.keys():
if k == "Column1":
continue
d[k][n] = col[k]
def quote(s):
s = s.replace("&lt;", "<").replace("&gt;", ">")
assert "&" not in s
return '"' + s + '"'
def con(s: str) -> str:
s = s.replace("&lt;", "<").replace("&gt;", ">")
assert "&" not in s
if s.startswith("s"):
return f"Shift({s[1:]})"
elif s.startswith("r"):
return f"Reduce({s[1:]})"
else:
assert s == "acc"
return "Acc()"
for m in d:
print(
f"""
static{{
Map<Integer, Action> map = new HashMap<>();
transitions.put({quote(m)}, map);
"""
)
for k, v in d[m].items():
print(f"map.put({k}, new {con(v)});")
print("}")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment