What type of algorithm would be used to construct a syntax tree from an expression represented in prefix order 开发者_高级运维notation?
A simple recursive algorithm can convert a prefix-order expression to a syntax tree.
GetNextPrefixExpression(tokenStream)
nextToken = tokenStream.GetNextToken()
if nextToken.IsValue()
return new Value(nextToken)
else if nextToken.IsUnaryOperator()
return new UnaryOperator(nextToken, GetNextPrefixExpression(tokenStream))
else if nextToken.IsBinaryOperator()
return new BinaryOperator(nextToken, GetNextPrefixExpression(tokenStream), GetNextPrefixExpression(tokenStream))
else if nextToken.IsTrinaryOperator()
return new TrinaryOperator(nextToken, GetNextPrefixExpression(tokenStream), GetNextPrefixExpression(tokenStream), GetNextPrefixExpression(tokenStream))
精彩评论