/// <summary> /// 获取语法树堆栈的顶级记号 /// </summary> /// <param name="SyntaxTreeStack">语法树堆栈</param> /// <returns>顶级记号Hash</returns> /// <remarks>Author:Alex Leo</remarks> private static TokenRecord.TokenRecord SyntaxTreeStackGetTopToken(Stack <TokenRecord.TokenRecord> SyntaxTreeStack) { TokenRecord.TokenRecord TempToken = null; if (SyntaxTreeStack.Count > 0) { TempToken = SyntaxTreeStack.Pop(); while (SyntaxTreeStack.Count > 0) { SyntaxTreeStack.Peek().ChildList.Add(TempToken); TempToken = SyntaxTreeStack.Pop(); } } return(TempToken); }
/// <summary> /// 语法树堆栈分析,基于记号的优先级 /// </summary> /// <param name="SyntaxTreeStack">语法树堆栈</param> /// <param name="NewToken">新记号</param> /// <remarks>Author:Alex Leo</remarks> private static void SyntaxTreeStackAnalyse(Stack <TokenRecord.TokenRecord> SyntaxTreeStack, TokenRecord.TokenRecord NewToken) { if (SyntaxTreeStack.Count == 0) //如果语法树堆栈中不存在记号,则直接压栈 { SyntaxTreeStack.Push(NewToken); } else //否则,比较优先级进行栈操作 { //比较优先级,如果新Token优先级高,则压栈; //如果新Token优先级低,则弹栈,把弹出的Token设置为新Token的下级,并把新Token压栈; //相同优先级也弹栈,并将新Token压栈 //if (this.m_DicTokenTypePriority[SyntaxTreeStack.Peek().TokenType] < this.m_DicTokenTypePriority[NewToken.TokenType])//低进 if (SyntaxTreeStack.Peek().Priority < NewToken.Priority) //低进 { SyntaxTreeStack.Push(NewToken); //低进 } else { TokenRecord.TokenRecord TempToken = null; //如果堆栈中有记号,并且栈顶的记号优先级大于等于新记号的优先级,则继续弹栈 while (SyntaxTreeStack.Count > 0 && (SyntaxTreeStack.Peek().Priority >= NewToken.Priority)) { TempToken = SyntaxTreeStack.Pop(); if (SyntaxTreeStack.Count > 0) //检测栈顶是否可能为空,如果为空则退出 { if (SyntaxTreeStack.Peek().Priority >= NewToken.Priority) { SyntaxTreeStack.Peek().ChildList.Add(TempToken); } else { NewToken.ChildList.Add(TempToken); } } else { NewToken.ChildList.Add(TempToken); } } SyntaxTreeStack.Push(NewToken); //压栈 } } }