Giter Club home page Giter Club logo

Comments (2)

renatahodovan avatar renatahodovan commented on June 18, 2024

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

from grammarinator.

mschlaipfer avatar mschlaipfer commented on June 18, 2024

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!

from grammarinator.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.