Skip to content
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

Open
mschlaipfer opened this issue Sep 4, 2023 · 2 comments
Open

Generating alternation at most once #130

mschlaipfer opened this issue Sep 4, 2023 · 2 comments

Comments

@mschlaipfer
Copy link

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 for N 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.

entry [x=1,y=1,z=1]
   : component[x,y,z] EOF
   ;

component [x,y,z]
   : {x}? x_component (',' component[0,y,z])*
   | {y}? y_component (',' component[x,0,z])*
   | {z}? z_component (',' component[x,y,0])*
   ;

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

@renatahodovan
Copy link
Owner

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 component rule is the last alternative, which becomes available after all the other alternatives got executed.

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,
Reni

@mschlaipfer
Copy link
Author

mschlaipfer commented Sep 29, 2023

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 ,. I don't have time to sanitize my code right now, but could share eventually.

Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants