Rad
Rad

Reputation: 21

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?

Upvotes: 2

Views: 1695

Answers (1)

Hyperboreus
Hyperboreus

Reputation: 32429

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.

Upvotes: 2

Related Questions