1
0
forked from GitHub/gf-core

store the index with every production

This commit is contained in:
Krasimir Angelov
2023-12-15 10:25:09 +01:00
parent f2d269ff65
commit 9313b45a4f
2 changed files with 28 additions and 34 deletions

View File

@@ -274,6 +274,7 @@ struct PgfLRReduceArg;
struct PGF_INTERNAL_DECL PgfLRProduction { struct PGF_INTERNAL_DECL PgfLRProduction {
ref<PgfConcrLin> lin; ref<PgfConcrLin> lin;
size_t index;
ref<Vector<ref<PgfLRReduceArg>>> args; ref<Vector<ref<PgfLRReduceArg>>> args;
}; };
@@ -299,8 +300,8 @@ struct PGF_INTERNAL_DECL PgfLRReduceArg {
struct PGF_INTERNAL_DECL PgfLRReduce { struct PGF_INTERNAL_DECL PgfLRReduce {
object lin_obj; object lin_obj;
size_t seq_idx;
size_t depth; size_t depth;
size_t r;
ref<Vector<object>> args; ref<Vector<object>> args;
}; };

View File

@@ -3,10 +3,10 @@
#include "parser.h" #include "parser.h"
#include <algorithm> #include <algorithm>
#define DEBUG_STATE_CREATION //#define DEBUG_STATE_CREATION
#define DEBUG_AUTOMATON //#define DEBUG_AUTOMATON
#define DEBUG_PARSER //#define DEBUG_PARSER
#define DEBUG_GENERATOR //#define DEBUG_GENERATOR
struct PgfLRTableMaker::CCat { struct PgfLRTableMaker::CCat {
CCat *parent; CCat *parent;
@@ -151,7 +151,8 @@ ref<PgfLRReduceArg> PgfLRTableMaker::CCat::persist() {
persistant->n_prods = n_prods; persistant->n_prods = n_prods;
for (size_t i = 0; i < n_prods; i++) { for (size_t i = 0; i < n_prods; i++) {
Production *prod = 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); auto children = vector_new<ref<PgfLRReduceArg>>(prod->args.count);
for (size_t j = 0; j < prod->args.count; j++) { for (size_t j = 0; j < prod->args.count; j++) {
if (prod->args[j] == NULL) { if (prod->args[j] == NULL) {
@@ -964,16 +965,9 @@ ref<PgfLRTable> PgfLRTableMaker::make()
Item *item = state->completed[i]; Item *item = state->completed[i];
ref<PgfLRReduce> reduction = vector_elem(lrstate->reductions,i); ref<PgfLRReduce> reduction = vector_elem(lrstate->reductions,i);
reduction->lin_obj = item->lin_obj; reduction->lin_obj = item->lin_obj;
reduction->seq_idx = item->seq_idx;
reduction->depth = item->stk_size; reduction->depth = item->stk_size;
reduction->args = vector_new<object>(item->args.count); 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++) { for (size_t j = 0; j < item->args.count; j++) {
if (item->args[j].ccat == NULL) { if (item->args[j].ccat == NULL) {
@@ -1004,14 +998,16 @@ struct PgfParser::Choice {
struct PgfParser::Production { struct PgfParser::Production {
ref<PgfConcrLin> lin; ref<PgfConcrLin> lin;
size_t index;
size_t n_args; size_t n_args;
Choice *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; size_t n_args = lin->args->len / lin->res->len;
Production *prod = (Production *) Production *prod = (Production *)
malloc(size+sizeof(Choice*)*n_args); malloc(size+sizeof(Choice*)*n_args);
prod->lin = lin; prod->lin = lin;
prod->index = index;
prod->n_args = n_args; prod->n_args = n_args;
for (size_t i = 0; i < n_args; i++) { for (size_t i = 0; i < n_args; i++) {
prod->args[i] = NULL; prod->args[i] = NULL;
@@ -1083,7 +1079,7 @@ void PgfParser::print_prod(Choice *choice, Production *prod)
ref<PgfDTyp> type = prod->lin->absfun->type; ref<PgfDTyp> type = prod->lin->absfun->type;
printer.puts(&prod->lin->name); printer.puts(&prod->lin->name);
printer.nprintf(32,"["); printer.nprintf(32,"/%zd[", prod->index);
PgfDBMarshaller m; PgfDBMarshaller m;
for (size_t i = 0; i < prod->n_args; i++) { for (size_t i = 0; i < prod->n_args; i++) {
Choice *choice = prod->args[i]; Choice *choice = prod->args[i];
@@ -1191,8 +1187,8 @@ PgfParser::Choice *PgfParser::intersect_choice(Choice *choice1, Choice *choice2,
im[key] = choice; im[key] = choice;
for (Production *prod1 : choice1->prods) { for (Production *prod1 : choice1->prods) {
for (Production *prod2 : choice2->prods) { for (Production *prod2 : choice2->prods) {
if (prod1->lin == prod2->lin) { if (prod1->lin == prod2->lin && prod1->index == prod2->index) {
Production *prod = new(prod1->lin) Production(); Production *prod = new(prod1->lin,prod1->index) Production();
choice->prods.push_back(prod); choice->prods.push_back(prod);
for (size_t i = 0; i < prod->n_args; i++) { for (size_t i = 0; i < prod->n_args; i++) {
@@ -1217,7 +1213,9 @@ void PgfParser::reduce(StackNode *parent, ref<PgfConcrLin> lin, ref<PgfLRReduce>
if (n == 0) { if (n == 0) {
ref<PgfConcrLincat> lincat = lin->lincat; 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++) { for (size_t i = 0; i < prod->n_args; i++) {
auto arg = *vector_elem(red->args, i); auto arg = *vector_elem(red->args, i);
@@ -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; return;
} }
if (*vector_elem(red->args, n-1)) { args.push_back(parent->choice);
args.push_back(parent->choice); for (auto node : parent->parents) {
for (auto node : parent->parents) { reduce(node, lin, red, n-1, args, parent->stage, after);
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.pop_back();
} }
PgfParser::Choice *PgfParser::retrieve_choice(ref<PgfLRReduceArg> arg) PgfParser::Choice *PgfParser::retrieve_choice(ref<PgfLRReduceArg> arg)
@@ -1266,7 +1258,7 @@ PgfParser::Choice *PgfParser::retrieve_choice(ref<PgfLRReduceArg> arg)
Choice *choice = new Choice(++last_fid); Choice *choice = new Choice(++last_fid);
for (size_t i = 0; i < arg->n_prods; i++) { 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++) { for (size_t i = 0; i < prod->n_args; i++) {
auto child = *vector_elem(arg->prods[i].args, i); auto child = *vector_elem(arg->prods[i].args, i);
prod->args[i] = retrieve_choice(child); prod->args[i] = retrieve_choice(child);
@@ -1309,7 +1301,7 @@ void PgfParser::reduce_all(StackNode *node)
ref<PgfConcrLincat>::untagged(red->lin_obj); ref<PgfConcrLincat>::untagged(red->lin_obj);
std::vector<Choice*> args; std::vector<Choice*> args;
if (before->end.pos == sentence->size) { 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);
} }
} }
} }
@@ -1357,9 +1349,10 @@ void PgfParser::start_matches(PgfTextSpot *end, PgfExn* err)
void PgfParser::match(ref<PgfConcrLin> lin, size_t seq_index, 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) { for (StackNode *parent : before->nodes) {
shift(parent, lin->lincat, r, prod, before, after); shift(parent, lin->lincat, r, prod, before, after);