Skip to content

Commit

Permalink
store the index with every production
Browse files Browse the repository at this point in the history
  • Loading branch information
krangelov committed Dec 15, 2023
1 parent f2d269f commit 9313b45
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 34 deletions.
3 changes: 2 additions & 1 deletion src/runtime/c/pgf/data.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ struct PgfLRReduceArg;

struct PGF_INTERNAL_DECL PgfLRProduction {
ref<PgfConcrLin> lin;
size_t index;
ref<Vector<ref<PgfLRReduceArg>>> args;
};

Expand All @@ -299,8 +300,8 @@ struct PGF_INTERNAL_DECL PgfLRReduceArg {

struct PGF_INTERNAL_DECL PgfLRReduce {
object lin_obj;
size_t seq_idx;
size_t depth;
size_t r;
ref<Vector<object>> args;
};

Expand Down
59 changes: 26 additions & 33 deletions src/runtime/c/pgf/parser.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
#include "parser.h"
#include <algorithm>

#define DEBUG_STATE_CREATION
#define DEBUG_AUTOMATON
#define DEBUG_PARSER
#define DEBUG_GENERATOR
//#define DEBUG_STATE_CREATION
//#define DEBUG_AUTOMATON
//#define DEBUG_PARSER
//#define DEBUG_GENERATOR

struct PgfLRTableMaker::CCat {
CCat *parent;
Expand Down Expand Up @@ -151,7 +151,8 @@ ref<PgfLRReduceArg> PgfLRTableMaker::CCat::persist() {
persistant->n_prods = n_prods;
for (size_t i = 0; i < n_prods; i++) {
Production *prod = prods[i];
persistant->prods[i].lin = prod->lin;
persistant->prods[i].lin = prod->lin;
persistant->prods[i].index = prod->index;
auto children = vector_new<ref<PgfLRReduceArg>>(prod->args.count);
for (size_t j = 0; j < prod->args.count; j++) {
if (prod->args[j] == NULL) {
Expand Down Expand Up @@ -964,16 +965,9 @@ ref<PgfLRTable> PgfLRTableMaker::make()
Item *item = state->completed[i];
ref<PgfLRReduce> reduction = vector_elem(lrstate->reductions,i);
reduction->lin_obj = item->lin_obj;
reduction->seq_idx = item->seq_idx;
reduction->depth = item->stk_size;
reduction->args = vector_new<object>(item->args.count);
reduction->r = 0;

if (ref<PgfConcrLin>::get_tag(item->lin_obj) == PgfConcrLin::tag) {
auto lin =
ref<PgfConcrLin>::untagged(item->lin_obj);
size_t n_fields = lin->seqs->len / lin->res->len;
reduction->r = item->seq_idx % n_fields;
}

for (size_t j = 0; j < item->args.count; j++) {
if (item->args[j].ccat == NULL) {
Expand Down Expand Up @@ -1004,14 +998,16 @@ struct PgfParser::Choice {

struct PgfParser::Production {
ref<PgfConcrLin> lin;
size_t index;
size_t n_args;
Choice *args[];

void *operator new(size_t size, ref<PgfConcrLin> lin) {
void *operator new(size_t size, ref<PgfConcrLin> lin, size_t index) {
size_t n_args = lin->args->len / lin->res->len;
Production *prod = (Production *)
malloc(size+sizeof(Choice*)*n_args);
prod->lin = lin;
prod->index = index;
prod->n_args = n_args;
for (size_t i = 0; i < n_args; i++) {
prod->args[i] = NULL;
Expand Down Expand Up @@ -1083,7 +1079,7 @@ void PgfParser::print_prod(Choice *choice, Production *prod)

ref<PgfDTyp> type = prod->lin->absfun->type;
printer.puts(&prod->lin->name);
printer.nprintf(32,"[");
printer.nprintf(32,"/%zd[", prod->index);
PgfDBMarshaller m;
for (size_t i = 0; i < prod->n_args; i++) {
Choice *choice = prod->args[i];
Expand Down Expand Up @@ -1191,8 +1187,8 @@ PgfParser::Choice *PgfParser::intersect_choice(Choice *choice1, Choice *choice2,
im[key] = choice;
for (Production *prod1 : choice1->prods) {
for (Production *prod2 : choice2->prods) {
if (prod1->lin == prod2->lin) {
Production *prod = new(prod1->lin) Production();
if (prod1->lin == prod2->lin && prod1->index == prod2->index) {
Production *prod = new(prod1->lin,prod1->index) Production();
choice->prods.push_back(prod);

for (size_t i = 0; i < prod->n_args; i++) {
Expand All @@ -1217,7 +1213,9 @@ void PgfParser::reduce(StackNode *parent, ref<PgfConcrLin> lin, ref<PgfLRReduce>
if (n == 0) {
ref<PgfConcrLincat> lincat = lin->lincat;

Production *prod = new(lin) Production();
size_t index = red->seq_idx / lincat->fields->len;
size_t r = red->seq_idx % lincat->fields->len;
Production *prod = new(lin,index) Production();

for (size_t i = 0; i < prod->n_args; i++) {
auto arg = *vector_elem(red->args, i);
Expand All @@ -1242,21 +1240,15 @@ void PgfParser::reduce(StackNode *parent, ref<PgfConcrLin> lin, ref<PgfLRReduce>
}
}

shift(parent, lincat, red->r, prod, before, after);
shift(parent, lincat, r, prod, before, after);
return;
}

if (*vector_elem(red->args, n-1)) {
args.push_back(parent->choice);
for (auto node : parent->parents) {
reduce(node, lin, red, n-1, args, parent->stage, after);
}
args.pop_back();
} else {
args.push_back(NULL);
reduce(parent, lin, red, n-1, args, before, after);
args.pop_back();
args.push_back(parent->choice);
for (auto node : parent->parents) {
reduce(node, lin, red, n-1, args, parent->stage, after);
}
args.pop_back();
}

PgfParser::Choice *PgfParser::retrieve_choice(ref<PgfLRReduceArg> arg)
Expand All @@ -1266,7 +1258,7 @@ PgfParser::Choice *PgfParser::retrieve_choice(ref<PgfLRReduceArg> arg)

Choice *choice = new Choice(++last_fid);
for (size_t i = 0; i < arg->n_prods; i++) {
Production *prod = new(arg->prods[i].lin) Production();
Production *prod = new(arg->prods[i].lin, arg->prods[i].index) Production();
for (size_t i = 0; i < prod->n_args; i++) {
auto child = *vector_elem(arg->prods[i].args, i);
prod->args[i] = retrieve_choice(child);
Expand Down Expand Up @@ -1309,7 +1301,7 @@ void PgfParser::reduce_all(StackNode *node)
ref<PgfConcrLincat>::untagged(red->lin_obj);
std::vector<Choice*> args;
if (before->end.pos == sentence->size) {
complete(node, lincat, red->r, red->depth, args);
complete(node, lincat, red->seq_idx % lincat->fields->len, red->depth, args);
}
}
}
Expand Down Expand Up @@ -1357,9 +1349,10 @@ void PgfParser::start_matches(PgfTextSpot *end, PgfExn* err)

void PgfParser::match(ref<PgfConcrLin> lin, size_t seq_index, PgfExn* err)
{
size_t r = seq_index % lin->lincat->fields->len;
size_t index = seq_index / lin->lincat->fields->len;
size_t r = seq_index % lin->lincat->fields->len;

Production *prod = new(lin) Production();
Production *prod = new(lin,index) Production();

for (StackNode *parent : before->nodes) {
shift(parent, lin->lincat, r, prod, before, after);
Expand Down

0 comments on commit 9313b45

Please sign in to comment.