parsingtatsu

Can you specify an arbitrary function as a terminal for a Tatsu grammar


Can you specify a terminal in a Tatsu grammar to be an arbitrary function rather than an actual BNF rule. This can be done in a peginator grammar. You can specify that when generating the PEG parser that when it needs to implement a given terminal that it will call an external function rather than an actual rule. I'd like to be able to leverage this in a Tatsu grammar.


Solution

  • The way I do what you want is to define an empty rule in the grammar, and then write a semantic action that returns the desired value:

    use_special_func = () ;
    
    class MySemantics:
        def use_special_func(self, ast):
            return str(random.random())
    

    https://tatsu.readthedocs.io/en/stable/semantics.html