开发者

Algorithm to build syntax tree from prefix order expression

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))
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜