मेरे पास एक एएसटी (सार सिंटेक्स पेड़) है और अब मैं इसे 2 या अधिक संख्या देकर अपने कंपाइलर का परीक्षण करना चाहता हूं और गणित संचालन (कैलकुलेटर की तरह) के परिणाम के साथ आउटपुट की अपेक्षा करना चाहता हूं।एएसटी दुभाषिया?
मेरा सवाल है, दुभाषिया बनाने का सबसे अच्छा तरीका क्या है? एएसटी नोड्स का दौरा रिकर्सिव है, इसलिए मुझे नहीं पता कि जब तक मैं पेड़ के अंत तक नहीं पहुंच जाता तब तक कितने encapsulated गणना मौजूद है। लेकिन चूंकि यह पुनरावृत्ति द्वारा पुनरावृत्ति किया जाता है, इसलिए मैं अंत में सभी परिचालन कैसे कर सकता हूं? ,
int interpret(tree t)
{ /* left to right, top down scan of tree */
switch (t->nodetype) {
case NodeTypeInt:
return t->value;
case NodeTypeVariable:
return t->symbtable_entry->value
case NodeTypeAdd:
{ int leftvalue= interpret(t->leftchild);
int rightvalue= interpret(t->rightchild);
return leftvalue+rightvalue;
}
case NodeTypeMultiply:
{ int leftvalue= interpret(t->leftchild);
int rightvalue= interpret(t->rightchild);
return leftvalue*rightvalue;
}
...
case NodeTypeStatementSequence: // assuming a right-leaning tree
{ interpret(t->leftchild);
interpret(t->rightchild);
return 0;
}
case NodeTypeAssignment:
{ int right_value=interpret(t->rightchild);
assert: t->leftchild->Nodetype==NodeTypeVariable;
t->leftchild->symbtable_entry->value=right_value;
return right_value;
}
case NodeTypeCompareForEqual:
{ int leftvalue= interpret(t->leftchild);
int rightvalue= interpret(t->rightchild);
return leftvalue==rightvalue;
}
case NodeTypeIfThenElse
{ int condition=interpret(t->leftchild);
if (condition) interpret(t->secondchild);
else intepret(t->thirdchild);
return 0;
case NodeTypeWhile
{ int condition;
while (condition=interpret(t->leftchild))
interpret(t->rightchild);
return 0;
...
}
}
क्या है "गोटो" करने के लिए परेशान है, क्योंकि इस दुभाषिया का ध्यान की बात परिवर्तन:
धन्यवाद
अगर आप कहां थे और ऑपरेटर की तुलना में तुलना करते हैं तो आप कैसे करेंगे? – Nitrate
तुलना करने के लिए दुभाषिया को पैच देखें तुलनात्मक, असाइनमेंट, IfThenElse –
धन्यवाद बहुत ईरा! – Nitrate