再次快速提问。我正在创建一个递归函数,如果“源”规则类型与目标字符相同,它将在“源”规则数组中查找元素并将这些规则应用于规则“目标数组”。此外,该函数检查目标字符是否在符号数组中,如果不在则添加它(并且还在新应用的规则上抛出一些标志)。这一切都是由递归调用驱动的,该调用使用计数器来确定已经通过了多少次迭代,并用于确定目标数组中应该应用新规则的位置,因此我们不会覆盖。
我也加入了一些调试代码来显示结果。
这是函数本身:
//Recursively tack on any non terminal pointed elements
int recursiveTack(rule * inrule[], char target, rule * targetrule[],
int counter, char symbols[])
{
printf("Got into recursiveTack\n");
printf("target is %c\n", target);
printf("counter is %d", counter);
for (int k = 0; k < sizeof(inrule); k++)
{
if (inrule[k]->type == target)
{
//doublecheck to see if we're trying to overwrite
if (targetrule[counter]->used = true)
{
counter++;
}
targetrule[counter]->head = inrule[k]->head;
targetrule[counter]->type = inrule[k]->type;
targetrule[counter]->used = true;
//Check to see if the elements are new to the symbols table and need to be added
if (!contains(returnGotoChar(targetrule[counter]), symbols))
{
//If not then add the new symbol
addChar(returnGotoChar(targetrule[counter]), symbols);
//Also set the goto status of the rule
targetrule[counter]->needsGoto = true;
//Also set the rule's currentGotoChar
targetrule[counter]->currentGotoChar = returnGotoChar(
targetrule[counter]);
}
counter++;
//recursivly add elements from non terminal nodes
if (isNonTerm(targetrule[counter]))
{
char newTarget = returnGotoChar(targetrule[counter]);
counter = recursiveTack(inrule, newTarget, targetrule, counter,
symbols);
}
}
}
//return how many elements we've added
return counter;
}
这是电话:
if(isNonTerm(I[i+first][second]))
{
printf("Confirmed non termainal\n");
printf("Second being passed: %d\n", second);
//Adds each nonterminal rule to the rules for the I[i+first] array
second = recursiveTack(I[i], targetSymbol, I[i+first], second, symbols[first]);
}
在此之前,所有传入的数组都已初始化。但是,我得到的输出表明递归在它开始之前在某处被杀死。
输出:
Second being passed: 0
Confirmed non termainal
Got into recursiveTack
target is E
Segmentation fault
任何帮助都会很棒,如果需要的话,我也可以使用该程序的其余部分,不过它大约有 700 行,包括注释。我很确定这只是缺少一些简单内容的另一种情况,但请告诉我您的想法。