简体   繁体   中英

DSL for generating sequences

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

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_token s and expr s. 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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM