0

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))
3
  • This doesn't matter to the question, but I'm looking at how your expression transforms into a sequence. What I don't get is why there are so many square braces. It is clear that a repeat doesn't of itself wrap the result square braces. So then it seems that the square braces have to come from the parens. There are only two levels of nesting of parens, and yet there are 4 levels of nesting of the square braces. I don't see where all those braces come from...what the rule is for generating them. Commented Mar 5, 2022 at 5:18
  • Oh, I guess you'd get there if a repeat of something inside parens causes an additional set of braces to be added around the result, but a repeat on a word does not lead to a wrapping set of braces. For this one example, that works. Commented Mar 5, 2022 at 5:21
  • in fact it doesnt matter .. I'm flattening it at the end Commented Mar 5, 2022 at 15:01

1 Answer 1

1

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?
Sign up to request clarification or add additional context in comments.

3 Comments

I only just noticed that you asked about an expr within another expr. So maybe you didn't the explanation at the top, as you already saw tyat. I hope my proposed solution gets you moving. I haven't played with grammars in years, so I'm not sure if that's the right answer or not.
thanks will try that out ..
it worked ;) .. nice

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.