perrymil
perrymil

Reputation: 43

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.

Upvotes: 0

Views: 27

Answers (1)

Apalala
Apalala

Reputation: 9244

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

Upvotes: 1

Related Questions