Home > Enterprise >  DSL for generating sequences
DSL for generating sequences

Time:03-05

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))

CodePudding user response:

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?
  • Related