开发者

Are the unary operators tree-nodes in the AST?

So I am trying to build and AST for boolean expression. Basically the expression is composed of boolean operands separated by AND, OR, NOT and paranthesis. Ex:

(true AND false) AND NOT(NOT true)

false AND NOT TRUE OR true

I am working in C# and I have classes for the TreeNode which holds the operands of an expression (LHS, RHS) and the the operator and I开发者_开发百科 also have classes for the leafs of the tree that hold the boolean values.

What I am not sure how to represent is the unary NOT operator... Should I use the TreeNode class and use just on of the branches? Should I create a 'Link' class that links two expressions via an unary operator edge?


I see three possibilities:

  • What you supposed with an NOT node that has only one child (if you don't mind that your tree won't be binary)
  • Give each node a property indicating if it is negated or not
  • Or the electronics way of using NAND and one child being True.
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜