summaryrefslogtreecommitdiff
path: root/earlpy/earlpy.py
diff options
context:
space:
mode:
authorMatthew Sotoudeh <matthew@masot.net>2024-02-19 03:42:42 -0800
committerMatthew Sotoudeh <matthew@masot.net>2024-02-19 03:42:42 -0800
commite133f250761c67b4465181f41909e78c272901d3 (patch)
treefc75f55dd2a3f4dddc589a8c259f887e90ef3a1e /earlpy/earlpy.py
parentffc6388571004b17e3a3dee2511ec99076ee803a (diff)
Parse trees, dangling else, etc.
Diffstat (limited to 'earlpy/earlpy.py')
-rw-r--r--earlpy/earlpy.py25
1 files changed, 19 insertions, 6 deletions
diff --git a/earlpy/earlpy.py b/earlpy/earlpy.py
index d48c1be..7fbf0f0 100644
--- a/earlpy/earlpy.py
+++ b/earlpy/earlpy.py
@@ -73,17 +73,23 @@ class Parser:
for token in tokens]
# production id
nodes = [t[0] for t in struct.iter_unpack("Q", res.stdout[8+(8*3*n_tokens):])]
+ # print(nodes)
# REPARSE the nodes
root = Node(self.productions[nodes[0]][1],
self.productions[nodes[0]][0])
- nodes = nodes[1:]
+ nodes.pop(0)
stack = [root]
while stack:
node = stack[-1]
+ # print(len(stack))
+ # if isinstance(node, tuple):
+ # print("\t", node)
+ # else:
+ # print("\t", node.symbol.name, [s.name for s in node.production])
+
if (isinstance(node, tuple)
- or len(node.production) == len(node.contents)):
- # COMPLETE!
+ or len(node.production) == len(node.contents)):
stack.pop()
if stack: stack[-1].contents.append(node)
else:
@@ -235,8 +241,12 @@ class Parser:
rule = [self.name_to_symbol[x] for x in rule]
self.productions.append((rule, symbol))
prods = ', '.join(map(str, range(start_idx, len(self.productions))))
- put(", {" + prods + ", 0}")
+ if prods:
+ put(", {" + prods + ", 0}")
+ else:
+ put(", {0}")
else:
+ self.productions.append(([], symbol))
put(", {0}")
put(" };")
putl(f"#define N_PRODUCTIONS {len(self.productions)}")
@@ -244,8 +254,11 @@ class Parser:
putl("symbol_id_t PRODUCTION_ID_TO_PRODUCTION[N_PRODUCTIONS][MAX_PRODUCTION_LEN] = { {0}")
for i, (production, _) in enumerate(self.productions):
if i == 0: continue
- production = ', '.join([symbol.name for symbol in production])
- put(", {" + production + ", 0}")
+ production = ', '.join(str(symbol.id) for symbol in production)
+ if production:
+ put(", {" + production + ", 0}")
+ else:
+ put(", {0}")
put(" };")
putl("symbol_id_t PRODUCTION_ID_TO_SYMBOL[N_PRODUCTIONS] = { 0")
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback