Search code examples
context-free-grammardata-conversionrascal

Is there a way in Rascal to generate the syntax definition of a grammar from its corresponding datatype?


To make the question a little more specific. I was wondering if I could generate Rascal parseable code from the built-in grammar datastructure, which in turn is parsed rascal code of course. I would like this since it is easier readable and also a nice feature to have since it would make this part of parsing conversable.

so from an instance of this:

data Grammar = \grammar(set[Symbol] starts, map[Symbol sort, Production def] rules);

To something like this:

start syntax E 
    = E "+" T
    | T
    ;

syntax T
    = T "*" F
    | F
    ;

syntax F
    = "(" E ")"
    | "a"
    ;

Solution

  • Yes, the utility can be found in 'lang:: rascal::format::Grammar'

    'grammar2rascal' formats an entire grammar as a rascal program which defines the same grammar.

    'topProd2rascal' maps a single rule back to its concrete definition in rascal notation.