<p>A <strong>boolean expression</strong> is an expression that evaluates to either <code>true</code> or <code>false</code>. It can be in one of the following shapes:</p>

<ul>
	<li><code>&#39;t&#39;</code> that evaluates to <code>true</code>.</li>
	<li><code>&#39;f&#39;</code> that evaluates to <code>false</code>.</li>
	<li><code>&#39;!(subExpr)&#39;</code> that evaluates to <strong>the logical NOT</strong> of the inner expression <code>subExpr</code>.</li>
	<li><code>&#39;&amp;(subExpr<sub>1</sub>, subExpr<sub>2</sub>, ..., subExpr<sub>n</sub>)&#39;</code> that evaluates to <strong>the logical AND</strong> of the inner expressions <code>subExpr<sub>1</sub>, subExpr<sub>2</sub>, ..., subExpr<sub>n</sub></code> where <code>n &gt;= 1</code>.</li>
	<li><code>&#39;|(subExpr<sub>1</sub>, subExpr<sub>2</sub>, ..., subExpr<sub>n</sub>)&#39;</code> that evaluates to <strong>the logical OR</strong> of the inner expressions <code>subExpr<sub>1</sub>, subExpr<sub>2</sub>, ..., subExpr<sub>n</sub></code> where <code>n &gt;= 1</code>.</li>
</ul>

<p>Given a string <code>expression</code> that represents a <strong>boolean expression</strong>, return <em>the evaluation of that expression</em>.</p>

<p>It is <strong>guaranteed</strong> that the given expression is valid and follows the given rules.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> expression = &quot;&amp;(|(f))&quot;
<strong>Output:</strong> false
<strong>Explanation:</strong> 
First, evaluate |(f) --&gt; f. The expression is now &quot;&amp;(f)&quot;.
Then, evaluate &amp;(f) --&gt; f. The expression is now &quot;f&quot;.
Finally, return false.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> expression = &quot;|(f,f,f,t)&quot;
<strong>Output:</strong> true
<strong>Explanation:</strong> The evaluation of (false OR false OR false OR true) is true.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> expression = &quot;!(&amp;(f,t))&quot;
<strong>Output:</strong> true
<strong>Explanation:</strong> 
First, evaluate &amp;(f,t) --&gt; (false AND true) --&gt; false --&gt; f. The expression is now &quot;!(f)&quot;.
Then, evaluate !(f) --&gt; NOT false --&gt; true. We return true.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= expression.length &lt;= 2 * 10<sup>4</sup></code></li>
	<li>expression[i] is one following characters: <code>&#39;(&#39;</code>, <code>&#39;)&#39;</code>, <code>&#39;&amp;&#39;</code>, <code>&#39;|&#39;</code>, <code>&#39;!&#39;</code>, <code>&#39;t&#39;</code>, <code>&#39;f&#39;</code>, and <code>&#39;,&#39;</code>.</li>
</ul>

class LiteralExpression:
    def __init__(self, literal):
        self.literal = literal

    def interpret(self):
        return self.literal == "t"

class LogicalExpression:
    def __init__(self, operator, expressions):
        self.operator = operator
        self.expressions = expressions

    def interpret(self):
        if self.operator == "!":
            sub_expression = self.expressions[0]
            return not sub_expression.interpret()
        elif self.operator == "&":
            return all(sub_expression.interpret() for sub_expression in self.expressions)
        elif self.operator == "|":
            return any(sub_expression.interpret() for sub_expression in self.expressions)

class Parser:
    def __init__(self, tokens):
        self.tokens = tokens
        self.i = 0

    def has_more_tokens(self):
        return self.i < len(self.tokens)

    def get_next_token(self):
        if not self.has_more_tokens():
            return None

        token = self.tokens[self.i]
        self.i += 1
        return token

    def parse(self):
        token = self.get_next_token()
        if token in "!&|":
            operator = token
            # (
            self.get_next_token()
            expressions = [self.parse()]
            while self.get_next_token() == ",":
                expressions.append(self.parse())
            return LogicalExpression(operator, expressions)
        elif token in "tf":
            return LiteralExpression(token)


class Solution:
    def parseBoolExpr(self, expression: str) -> bool:
        # grammar
        # expression = logical_expression | literal
        # logical_expression = ("!" | "&" | "|") "(" expression ")"
        # literal = "t" | "f"
        tokens = list(expression)
        parser = Parser(tokens)
        ast = parser.parse()

        return ast.interpret()
17600