それで、ここに話があります。文字列をトークン化し、それらのトークンからノードのツリーを作成する再帰降下パーサーを作成しようとしています。
私の主要なクラスのすべてのポインターが機能しています...以前にRDPを使用したことがある場合は、プログラム->ステートメント-> assignStmt...などで私が話していることを知っています.ノードには、ステートメント ノードなどを指す子があります。
これが問題です。ツリーノードの最後に到達すると、トークナイザーが文字列から作成した実際のトークンを指しています。
したがって、文字列は次のようになります。
firstvar = 1;
この場合、[{id} firstvar]、[{assignment} =]、[{number} 1]、[{scolon}] の 4 つのトークンがあります。
そして、私の assignStmt ノードがそのステートメントの非デコレータ部分を指すようにしたい.. つまり、assignStmt の child1 は [{id} firstvar] になり、child2 は [{number} 1] になります...
でも。child1 を [{id} firstvar] に割り当ててから次のトークンに進むと、進むにつれて child1 の値が変化します。したがって、グローバル トークンを次のトークン (この場合は [{assignment} =] ) に変更すると、assignStmt の child1 が変更されます。
どうしてこれなの?私に何ができる?!ありがとうございました!
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; =}
}