Last active
June 20, 2022 06:05
-
-
Save CCXXXI/48bf2f7f35068d964a6b27a71e1fa13a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("<", "<").replace(">", ">") | |
assert "&" not in s | |
return '"' + s + '"' | |
def con(s: str) -> str: | |
s = s.replace("<", "<").replace(">", ">") | |
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