开发者

Algorithm to build syntax tree from prefix order expression

开发者 https://www.devze.com 2023-01-07 02:03 出处:网络
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 expressio

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

精彩评论

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