1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.