Parse boolean rule expressions into a syntax tree
I was asked this
No reports yet. Sign in to say where you were asked it.
Risk rules are written as text such as age >= 18 AND country == "US". Write a parser that turns one rule into a syntax tree and prints it in prefix form, for example (AND (>= age 18) (== country "US")), or reports that the rule is invalid.
A comparison is a field name, an operator (==, !=, <, <=, >, >=) and a literal, which is an integer or a double-quoted string with escaped quotes and backslashes. Comparisons combine with NOT, AND and OR (in that order of precedence, NOT binding tightest) and with nested parentheses. Reject anything malformed: unbalanced parentheses, a missing operand, a bad escape, trailing tokens. Talk through how you tokenize, how precedence and associativity fall out of your grammar, and how you would then evaluate the tree against a transaction.
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
