【发布时间】:2016-10-16 19:39:37
【问题描述】:
我是 C 编程的初学者,我正在尝试创建一个算法来查找 first 和 follow 给定语法的集合.txt 文件。
输入语法如下:
S-abA|e|bhG|AKX
A-Kb|d
K-j|c|e
X-p
到目前为止,我只是在尝试定义用于查找这些集合的结构(这就是我遇到的问题)。
我正在使用一个列表向量,其中每个列表将包含我的语法的 RHS 值,并且向量中的每个位置(由输入文件中的规则或行数定义)将是指向列表。 下面的函数编译并几乎工作。产品被正确读取并且变量“producao”工作正常,我尝试将其打印出来并且它正确获取每个值,但是当我将这些值添加到列表中时,当我打印整个清单是:
我遇到问题的功能发布在下面:
criaListaRegras()
{
lista *regras;
regras = cria_lista(); // initialize list pointing to null
lista **vetor; // declare the vector of list pointers
vetor = malloc(sizeof(lista)*numLinhas);
int i = 0;
int numLinha = 0;
char producao[10]; // Each production in the RHS will have at most 9 characters and will be added to this variable, one at a time.
FILE *entrada;
entrada = fopen("entrada.txt", "r"); // input file
fseek(entrada, 2, SEEK_SET); // seek to RHS of the rule. (S-ab|AbB), makes fp go to 'a'.
char linha; // Will be used to parse the input
while ((linha = getc(entrada)) != EOF){
if (linha == '\n'){ // checks if a rule has ended, so that we add end of string to production and append that into the list.
producao[i] = '\0';
inserir(regras, producao); // append production (producao) into the list (regras).
imprimir(regras); // should print all values inside "regras" but only print the last value added * "number of productions found so far".
vetor[numLinha] = regras; // should assign a list into its position in the pre-defined vector of lists at position "numLinha".
i = 0; // resets string position counter to 0 so we can reuse it for next production.
numLinha++; // increment line/rule counter.
fseek(entrada, 2, SEEK_CUR);
}
if (linha == '|'){ // our production delim is '|', so if we find it, means a production has ended and we should add it into the list.
producao[i] = '\0';
inserir(regras, producao);
i = 0;
}
if(linha != '|' && linha != '\n'){ // this means the current char (linha) is part of some RHS production, so we should concatenate it into the variable producao on position 'i' and increment 'i'.
producao[i] = linha;
i++;
}
}
}
criaListaRegras() 中使用的其他函数对于测试值工作正常,可以在下面找到:
lista *cria_lista()
{
lista *novo = (lista*)malloc(sizeof(lista));
novo->next = NULL;
return novo;
}
void imprimir(lista *original)
{
lista * aux = original->next;
while(aux!= NULL){
printf("%s\n", aux->nome);
aux=aux->next;
}
}
void inserir(lista *original, char *nome)
{
lista *base = original;
lista *aux = original->next;
lista *novo;
while(aux!= NULL){
aux=aux->next;
base=base->next;
}
novo=(lista*)malloc(sizeof(lista));
novo->nome = nome;
novo->next = aux;
base->next = novo;
}
如果有人能指出我做错了什么,我会很高兴。
【问题讨论】:
-
注意:
char linha;-->int linha; (since you are usingwhile ((linha = getc(entrada)) != EOF ) {` )
标签: c linked-list grammar