ç树节点指针变化值与全局 [英] C Treenode pointers changing values with globals

查看:113
本文介绍了ç树节点指针变化值与全局的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,这里的故事。我试图创建一个递归下降解析器tokenizes一个字符串,然后创建节点树掉那些标记。

所有的指针我的专业类正在......如果你在此之前与RDP的工作,你知道我在谈论与程序 - >语句 - > assignStmt ...等想法是该节目节点具有指向该语句节点的孩子,等等。

下面就是问题所在。当我到达我指着那个标记生成器从字符串创建的实际令牌树节点的结束。

所以,我们说的字符串是:

  firstvar = 1;

在这种情况下,有4个令牌[{ID} firstvar],[{}赋值=],[{}号1],[{scolon}]

和我希望我的assignStmt节点指向该语句的非装饰部分..即assignStmt的child1会[{ID} firstvar]和的child2会[{}号1] ...

但是。当我分配到child1 [{ID} firstvar],然后向前移动到下一个令牌的child1变化值作为我前进。所以,如果我改变全球令牌到下一个标记(在这种情况下,[{}赋值=]),则assignStmt的child1随之改变。

这是为什么?我能做什么?!谢谢!

  TOKEN * getNextToken(无效);
//这里只显示你知道回报...它在其他地方正常工作typedef结构节点{
    TOKEN *数据;    结构节点* child1,*的child2,* child3,* child4,*父母;}节点;TOKEN *记号。
象征符号;
结构节点*根;无效getsym()
{
    令牌= getNextToken();
    符号= token->符号;
}诠释的main()
{
    getsym();
    //所以,现在,从getsym()全球令牌的值为{标识符; firstvar}    结构节点* tempNode;
    tempNode =(结构节点*)释放calloc(1,sizeof的(结构节点));
    tempNode-> child1 = tempNode->的child2 = tempNode-> child3 = tempNode-> child4 = NULL;
    tempNode->数据=记号。    getsym();
    //但现在从getsym()全球令牌的值为{分配; =},和
    //随后tempNode->数据已经从它应该是什么改变
    // {标识符; firstvar}到全球令牌的新的价值是什么:{分配; =}}


解决方案

您正在返回一个指向一个全局变量,即使你修改全局变量的指针永远是相同的。

的解决方案是每次要么分配一个新对象时,或根本不使用指针和直接返回的结构和让结构的编译器手柄复印内部值

So, here's the story. I'm trying to create a recursive descent parser that tokenizes a string and then creates a tree of nodes out of those tokens.

All of the pointers for my major classes are working... if you're worked with an RDP before then you know what I'm talking about with program -> statement -> assignStmt... etc. The idea being that the program node has a child that points to the statement node, etc.

Here's the problem. When I get to the end of the treenode I'm pointing to the actual tokens that the tokenizer created from the string.

So, let's say the string is:

 firstvar = 1;

In this case there are 4 tokens [{id} firstvar], [{assignment} =], [{number} 1], [{scolon}]

And I want my assignStmt node to point to the non-decorator portions of that statement.. namely, child1 of assignStmt would be [{id} firstvar] and child2 would be [{number} 1]...

HOWEVER. When I assign child1 to [{id} firstvar], and then move onward to the next tokens, the value of child1 changes as I move forward. So, if I change my global token to the next token ( in this case [{assignment} =] ) then child1 of the assignStmt changes with it.

Why is this? What can I do?! Thank you!

TOKEN* getNextToken(void); 
//only shown here to you know the return... it's working properly elsewhere

typedef struct node {
    TOKEN *data;

    struct node *child1, *child2, *child3, *child4, *parent;

} node;

TOKEN *token;
Symbol sym;
struct node *root;

void getsym() 
{
    token = getNextToken();
    sym = token->sym;
}

int main()
{
    getsym();
    //So, right now, from getsym() the global token has the value {identifier; firstvar} 

    struct node* tempNode;
    tempNode = (struct node*) calloc(1, sizeof(struct node));
    tempNode->child1 = tempNode->child2 = tempNode->child3 = tempNode->child4 =  NULL;
    tempNode->data = token;

    getsym();
    //BUT NOW from getsym() the global token has the value {assignment; =}, and 
    //subsequently the tempNode->data has changed from what it should be 
    //{identifier; firstvar} to what the global token's new value is: {assignment; =}

}

解决方案

You are returning a pointer to a global variable, and that pointer will always be the same even if you modify the global variable.

The solution is to either allocate a new object each time, or to not use pointers at all and return the structure directly and let the compiler handle copying of the structures internal values.

这篇关于ç树节点指针变化值与全局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆