Boolean expression parser

143 Views Asked by At

I'd like to implement a parser that helps me working on more complex boolean logic expressions for designing digital circuits. But I seem to be dumb.

start:equation

equation: SYMBOL
        | expression

expression: nottk

nottk: "!" SYMBOL

and: expression "*" expression


SYMBOL: /[A-Za-z]/

This is my grammar for now. I know that it is incomplete, but I first wanted to get the surrounding code working in principle!

For now I use a Transformer to convert symbols into sympy smbols. I am using sympy for working with the expressions, because I don't fancy implementing operations on logic expressions on my own and sympy has most of everything I need.

class SymbolTransformer(Transformer):
    def SYMBOL(self, token: Token):
        """Converts every symbol into asympy symbol"""
        return token.update(value=sympy.symbols(f'{token.value}'))

Just for good measure this is my Transformer.

I tried to implement everything as a Transformer, but it wasn't working, since Non-terminals work differently.

So I am asking, How would you change my grammar, or what would you use to implement generating sympy expressions from a Tree.

0

There are 0 best solutions below