Я пытаюсь создать компилятор с использованием lex и yacc для Linux для университетского проекта.
Я хочу свернуть древовидную структуру, содержащую char* (некоторые из которых могут быть нулевыми), в строку, полученную в результате конкатенации ненулевого char*, выполнив обход дерева в глубину
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_EXPR_LEN 512
struct tnode {
char *txt;
struct tnode *child;
struct tnode *next;
};
int collapse_branch_rec (struct tnode *n, char **array, int size_left)
{
if (!n)
return size_left;
if (!size_left)
printf ("an expression is too long to be evaluated; "
"split it into sub-expressions.");
if (n->txt) {
array++ = n->txt;
}
size_left = collapse_branch_rec (n->child, array, --size_left);
return collapse_branch_rec (n->next, array, --size_left);
}
void collapse_branch (struct tnode* root, char *string)
{
char **array = malloc (MAX_EXPR_LEN);
collapse_branch_rec (root, array, MAX_EXPR_LEN);
char **p = array;
int len;
while (p)
len += strlen (*p++);
p = array;
char str[len+1];
while (p)
strcat (str, *p++);
string = str;
}
struct tnode *getnode(char *txt)
{
struct tnode *n = malloc (sizeof (struct tnode));
if (txt)
n->txt = strdup (txt);
return n;
}
int main()
{
char **buffer = malloc (MAX_EXPR_LEN);
struct tnode *a, *b, *c, *d;
a = getnode ("(");
b = getnode (NULL);
c = getnode (")");
d = getnode ("5");
b->child = a;
a->next = d;
d->next = c;
char *string;
collapse_branch (b, string);
printf ("the result is %s", string);
return 0;
}
Однако я получаю эту ошибку компиляции, которую я не могу понять:
new.c: In function ‘collapse_branch_rec’:
new.c:36:33: error: lvalue required as left operand of assignment
array++ = n->txt;
Что это означает? и как мне решить мою проблему?
Изменить: даже если я исправлю разыменование указателя, я все равно получаю ошибку сегментации в libc:
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7ab9b2a in strlen () from /usr/lib/libc.so.6
if (!size_left) { printf (...); return -1; }
падать на рекурсивные вызовы здесь опасно. - person wildplasser   schedule 17.01.2015while (p) len += strlen (*p++);
--›while (*p) len += strlen (*p++);
[или рассмотрите возможность использования циклаfor (p=array; *p; p++) { len += strlen(*p);
] 2string = str;
в конце функцииcolla_branch() бесполезна. - person wildplasser   schedule 17.01.2015