-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Generating alternation at most once #130
Comments
Hi @mschlaipfer ! I'm sorry for the late reply. I'm not sure that every detail of your use case is clear to me: would you like to generate a random subset of the alternatives (i.e., generate every alternative zero or one time in a random order) or to generate a permutation of the alternatives (i.e., generate every alternative exactly one time in a random order)? Both of them is possible and your solution is quite close to the right one: Subset generation: entry [x=1,y=1,z=1]
: component[x,y,z] EOF
;
component [x,y,z]
: {x}? 'x' (',' component[0,y,z])?
| {y}? 'y' (',' component[x,0,z])?
| {z}? 'z' (',' component[x,y,0])?
| {x+y+z == 0}?
; Permutation generation: entry [x=1,y=1,z=1]
: component[x,y,z] EOF
;
component [x,y,z]
: {x}? 'x' (',' component[0,y,z])
| {y}? 'y' (',' component[x,0,z])
| {z}? 'z' (',' component[x,y,0])
| {x+y+z == 0}?
; The solution is to add an artificial alternative which is selected if all the other alternatives are exhausted. For subsets, the recursive subrules should be optional, enabling to interrupt the recursion at any iteration. While in case of permutation, the only exit point for the recursive I hope this helps! If you have any further questions, don't hesitate to ask, I'll try my best to reply much faster than this time. :) Cheers, |
Hi, thanks for getting back to me. Both are of interest, actually I care about permutations with required and optional elements. Using your idea of the empty alternative I made progress and have something I'm happy with for generation. I didn't get parsing to work with ANTLR predicates, yet, though. The above solution you proposed needed a bit more work to avoid trailing Thanks again! |
Hi,
I'm exploring the use of grammarinator for a project.
I'd like to generate an alternation for which the order does not matter, but each alternative should be produced at most once. I want to avoid listing all the
N!
possibilities forN
alternatives.I noticed that grammarinator has some support for ANTLR's actions, and I got as far as defining the following grammar (the number of alternatives is larger than 3 in my real use case), which produces syntactically correct Python code. Generation fails with a
ValueError: Total of weights must be greater than zero
, which makes sense as well.Is there a way to accomplish what I want to do without listing the alternatives? I thought of guarding the
(',' component[...])*
with{y+z>0}?
(and similar).I have already written a custom generator that overrides the code-generated one that does what I want, but I'm not sure how well that will play with mutations/transformations, yet. I'd be interested if you have thoughts on this as well.
Thanks,
Matthias
The text was updated successfully, but these errors were encountered: