Skip to content

Commit

Permalink
Refactor.
Browse files Browse the repository at this point in the history
  • Loading branch information
weetmuts committed Nov 5, 2024
1 parent f0bec03 commit a424b03
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 33 deletions.
39 changes: 26 additions & 13 deletions src/main/c/parts/ixml.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,7 @@ void free_ixml_terminal(IXMLTerminal *t);
IXMLNonTerminal *new_ixml_nonterminal();
IXMLNonTerminal *copy_ixml_nonterminal(IXMLNonTerminal *nt);
void free_ixml_nonterminal(IXMLNonTerminal *t);
void free_ixml_term(IXMLTerm *t);

char *generate_rule_name(XMQParseState *state);
void make_last_term_optional(XMQParseState *state);
Expand Down Expand Up @@ -1604,9 +1605,17 @@ void add_yaep_tmp_terminals_to_rule(XMQParseState *state, IXMLRule *rule)
IXMLTerminal *t = (IXMLTerminal*)hashmap_get(state->ixml_terminals_map, te->name);
if (t == NULL)
{
// This terminal was not already in the map. Add it.
add_yaep_terminal(state, te);
t = te;
}
add_yaep_terminal_to_rule(state, te);
else
{
// This terminal was already in the map. Free the terminal and add a
// pointer the already stored termina to the rule.
free_ixml_terminal(te);
}
add_yaep_terminal_to_rule(state, t);
state->ixml_tmp_terminals->elements[i] = NULL;
}
}
Expand Down Expand Up @@ -1643,6 +1652,7 @@ void free_ixml_rule(IXMLRule *r)
{
if (r->rule_name) free_ixml_nonterminal(r->rule_name);
r->rule_name = NULL;
// vector_free_and_values(r->rhs, (FreeFuncPtr)free_ixml_term);
vector_free(r->rhs);
r->rhs = NULL;
free(r);
Expand Down Expand Up @@ -1684,6 +1694,21 @@ void free_ixml_nonterminal(IXMLNonTerminal *nt)
free(nt);
}

void free_ixml_term(IXMLTerm *t)
{
if (t->type == IXML_TERMINAL)
{
IXMLTerminal *te = (IXMLTerminal*)t;
free_ixml_terminal(te);
}
else if (t->type == IXML_NON_TERMINAL)
{
IXMLNonTerminal *nt = (IXMLNonTerminal*)t;
free_ixml_nonterminal(nt);
}
else assert(false);
}

char *generate_rule_name(XMQParseState *state)
{
char buf[16];
Expand Down Expand Up @@ -1733,17 +1758,5 @@ void make_last_term_optional(XMQParseState *state)

#else

// Empty function when XMQ_NO_IXML is defined.
bool xmq_parse_ixml_grammar(YaepGrammar *g,
struct yaep_tree_node **root,
int *ambiguous,
XMQDoc *doq,
const char *start,
const char *stop,
bool build_xml_of_ixml)
{
return false;
}


#endif // IXML_MODULE
32 changes: 22 additions & 10 deletions src/main/c/parts/yaep.c
Original file line number Diff line number Diff line change
Expand Up @@ -2981,7 +2981,7 @@ static hash_table_t set_term_lookahead_tab; /* key is(core, distances, lookeahed

/* The following two variables contains all input tokens and their
number. The variables can be read externally.*/
static YaepTok*toks;
static YaepTok *toks;
static int toks_len;
static int tok_curr;

Expand All @@ -2995,7 +2995,7 @@ static int n_all_sits;
/* The following two dimensional array(the first dimension is context
number, the second one is situation number) contains references to
all possible situations.*/
static YaepSituation***sit_table;
static YaepSituation ***sit_table;

/* The following vlo is indexed by situation context number and gives
array which is indexed by situation number
Expand Down Expand Up @@ -3769,13 +3769,16 @@ static void rule_new_stop()

OS_TOP_FINISH(rules_ptr->rules_os);
OS_TOP_EXPAND(rules_ptr->rules_os, rules_ptr->curr_rule->rhs_len* sizeof(int));
rules_ptr->curr_rule->marks =(char*)calloc(rules_ptr->curr_rule->rhs_len, sizeof(1)); // IXML
rules_ptr->curr_rule->order =(int*) OS_TOP_BEGIN(rules_ptr->rules_os);
rules_ptr->curr_rule->order = (int*)OS_TOP_BEGIN(rules_ptr->rules_os);
OS_TOP_FINISH(rules_ptr->rules_os);
for(i = 0; i < rules_ptr->curr_rule->rhs_len; i++)
{
rules_ptr->curr_rule->order[i] = -1;
}

OS_TOP_EXPAND(rules_ptr->rules_os, rules_ptr->curr_rule->rhs_len* sizeof(char));
rules_ptr->curr_rule->marks = (char*)OS_TOP_BEGIN(rules_ptr->rules_os);
OS_TOP_FINISH(rules_ptr->rules_os);
}

#ifndef NO_YAEP_DEBUG_PRINT
Expand Down Expand Up @@ -4262,13 +4265,16 @@ static void set_add_new_nonstart_sit(YaepSituation*sit, int parent)
(situation, the corresponding distance) without duplicates
because we also forms core_symb_vect at that time.*/
for(i = new_n_start_sits; i < new_core->n_sits; i++)
{
if (new_sits[i] == sit && new_core->parent_indexes[i] == parent)
{
return;
}
}
OS_TOP_EXPAND(set_sits_os, sizeof(YaepSituation*));
new_sits = new_core->sits =(YaepSituation**) OS_TOP_BEGIN(set_sits_os);
OS_TOP_EXPAND(set_parent_indexes_os, sizeof(int));
new_core->parent_indexes
=(int*) OS_TOP_BEGIN(set_parent_indexes_os) - new_n_start_sits;
new_core->parent_indexes = (int*)OS_TOP_BEGIN(set_parent_indexes_os) - new_n_start_sits;
new_sits[new_core->n_sits++] = sit;
new_core->parent_indexes[new_core->n_all_dists++] = parent;
n_parent_indexes++;
Expand Down Expand Up @@ -5313,7 +5319,6 @@ yaep_read_grammar(YaepGrammar*g, int strict_p,
rule_new_stop();
// IXML
rule->mark = mark;
rule->marks =(char*)calloc(rhs_len, sizeof(char));
memcpy(rule->marks, marks, rhs_len);

if (transl != NULL)
Expand Down Expand Up @@ -6327,9 +6332,16 @@ static void build_pl()
{
term = toks[tok_curr].symb;
if (grammar->lookahead_level != 0)
lookahead_term_num =(tok_curr < toks_len - 1
? toks[tok_curr +
1].symb->u.term.term_num : -1);
{
if (tok_curr < toks_len-1)
{
lookahead_term_num = toks[tok_curr+1].symb->u.term.term_num;
}
else
{
lookahead_term_num = -1;
}
}

#ifndef NO_YAEP_DEBUG_PRINT
if (grammar->debug_level > 2)
Expand Down
32 changes: 22 additions & 10 deletions src/main/c/yaep/src/yaep.c
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,7 @@ static hash_table_t set_term_lookahead_tab; /* key is(core, distances, lookeahed

/* The following two variables contains all input tokens and their
number. The variables can be read externally.*/
static YaepTok*toks;
static YaepTok *toks;
static int toks_len;
static int tok_curr;

Expand All @@ -665,7 +665,7 @@ static int n_all_sits;
/* The following two dimensional array(the first dimension is context
number, the second one is situation number) contains references to
all possible situations.*/
static YaepSituation***sit_table;
static YaepSituation ***sit_table;

/* The following vlo is indexed by situation context number and gives
array which is indexed by situation number
Expand Down Expand Up @@ -1439,13 +1439,16 @@ static void rule_new_stop()

OS_TOP_FINISH(rules_ptr->rules_os);
OS_TOP_EXPAND(rules_ptr->rules_os, rules_ptr->curr_rule->rhs_len* sizeof(int));
rules_ptr->curr_rule->marks =(char*)calloc(rules_ptr->curr_rule->rhs_len, sizeof(1)); // IXML
rules_ptr->curr_rule->order =(int*) OS_TOP_BEGIN(rules_ptr->rules_os);
rules_ptr->curr_rule->order = (int*)OS_TOP_BEGIN(rules_ptr->rules_os);
OS_TOP_FINISH(rules_ptr->rules_os);
for(i = 0; i < rules_ptr->curr_rule->rhs_len; i++)
{
rules_ptr->curr_rule->order[i] = -1;
}

OS_TOP_EXPAND(rules_ptr->rules_os, rules_ptr->curr_rule->rhs_len* sizeof(char));
rules_ptr->curr_rule->marks = (char*)OS_TOP_BEGIN(rules_ptr->rules_os);
OS_TOP_FINISH(rules_ptr->rules_os);
}

#ifndef NO_YAEP_DEBUG_PRINT
Expand Down Expand Up @@ -1932,13 +1935,16 @@ static void set_add_new_nonstart_sit(YaepSituation*sit, int parent)
(situation, the corresponding distance) without duplicates
because we also forms core_symb_vect at that time.*/
for(i = new_n_start_sits; i < new_core->n_sits; i++)
{
if (new_sits[i] == sit && new_core->parent_indexes[i] == parent)
{
return;
}
}
OS_TOP_EXPAND(set_sits_os, sizeof(YaepSituation*));
new_sits = new_core->sits =(YaepSituation**) OS_TOP_BEGIN(set_sits_os);
OS_TOP_EXPAND(set_parent_indexes_os, sizeof(int));
new_core->parent_indexes
=(int*) OS_TOP_BEGIN(set_parent_indexes_os) - new_n_start_sits;
new_core->parent_indexes = (int*)OS_TOP_BEGIN(set_parent_indexes_os) - new_n_start_sits;
new_sits[new_core->n_sits++] = sit;
new_core->parent_indexes[new_core->n_all_dists++] = parent;
n_parent_indexes++;
Expand Down Expand Up @@ -2983,7 +2989,6 @@ yaep_read_grammar(YaepGrammar*g, int strict_p,
rule_new_stop();
// IXML
rule->mark = mark;
rule->marks =(char*)calloc(rhs_len, sizeof(char));
memcpy(rule->marks, marks, rhs_len);

if (transl != NULL)
Expand Down Expand Up @@ -3997,9 +4002,16 @@ static void build_pl()
{
term = toks[tok_curr].symb;
if (grammar->lookahead_level != 0)
lookahead_term_num =(tok_curr < toks_len - 1
? toks[tok_curr +
1].symb->u.term.term_num : -1);
{
if (tok_curr < toks_len-1)
{
lookahead_term_num = toks[tok_curr+1].symb->u.term.term_num;
}
else
{
lookahead_term_num = -1;
}
}

#ifndef NO_YAEP_DEBUG_PRINT
if (grammar->debug_level > 2)
Expand Down

0 comments on commit a424b03

Please sign in to comment.