From e133f250761c67b4465181f41909e78c272901d3 Mon Sep 17 00:00:00 2001 From: Matthew Sotoudeh Date: Mon, 19 Feb 2024 03:42:42 -0800 Subject: Parse trees, dangling else, etc. --- earlpy/earlpy.py | 25 +++++++++++++++++++------ 1 file changed, 19 insertions(+), 6 deletions(-) (limited to 'earlpy/earlpy.py') 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") -- cgit v1.2.3