대학 프로젝트 용 lex & yacc을 사용하여 컴파일러를 빌드하려하고 있습니다.트리를 문자열로 축소
나는 트리의 깊이 우선 탐색을 수행하여하지 널 문자 *의 연결로 얻은 문자열의 char *를 (일부 널 (null)이 될 수 있음)이 포함 된 트리 구조를 축소 할
#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의 세그먼트 오류를 얻을 :
문자 포인터의 유효한 배열에array
점을 가정
Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7ab9b2a in strlen() from /usr/lib/libc.so.6
'if (! size_left) {printf (...); -1을 반환합니다. }'재귀 호출이 떨어지면 위험합니다. – wildplasser
while (p) len + = strlen (* p ++);'while (p) len + = strlen (* p ++);'->'while 비슷한 오류 (p = array; * p; p ++) {len + = strlen (* p); 'loop] ** 2 ** collapse_branch()의 끝에있는'string = str;'은 쓸모가 없습니다. – wildplasser