DSL for generating sequences

68 Views Asked by At

trying to create DSL to generate sequences ... here is what i did so far :

    ?start : expr

    token : WORD                        
    repeat_token : token ":" INT        
    tokens : (token | repeat_token)+    
    repeat : ":" INT
    expr  : "(" tokens | expr ")"   repeat?

here is how the DSL look like :

   (a b:2 (c d:3):2 ):3

   [[a bb [[c ddd] [c ddd]] ] ... ]

I have problem with expr within expr ... ?

this fails:

 (a:2 (b))
1

There are 1 best solutions below

3
On BEST ANSWER

How do you see fitting (a:2 (b)) into your grammar? It doesn't seem like you can. Here's my logic:

The outer level has to be an expr because of the parens. In that expr you have both a repeat_token and another expr. I don't see anywhere that lets you have a sequence of elements that includes both repeat_tokens and exprs. Because of that, your input can't be parsed with your grammar.

As it is, a expr can only be in another expr all by itself, which doesn't seem very useful in general. That could only lead to extra sets of parentheses I think. What I think you need to do is allow an expr to be included in a tokens.

So then maybe:

?start : expr

    token : WORD                        
    repeat_token : token ":" INT        
    tokens : (token | repeat_token | expr)+    
    repeat : ":" INT
    expr  : "(" tokens ")" repeat?