Write a program in JAVA that prints all strings defined by a
Write a program in JAVA that prints all strings defined by a regular expression up to a given length. Assume the alphabet ={1,0}.
Prompt the user to enter a regular expression and an integer n. Print all the strings, with length n, defined by the regular expression.Use console based I/O.
[The empty string can be printed with the Unicode “\\u03B5”, but Unicode may not be supported in some environments].
A samplerun of the program is shown bellow.
Regular expression: 01*|(10)*
n: 5
-------Strings in the language -------
0
01
10
011
0111
1010
01111
Solution
//Generator.java
//This program generates the n number of strings of given regular expression
import java.util.*;
 public class Generator
 {
 private static Scanner s;
public static void main(String[] args){
     System.out.println(\"Enter the regex to generate string matching to the regex :\");
 s = new Scanner(System.in);
    String regex=s.next();
 System.out.println(\"Enter the value of n :\");
 int n=s.nextInt();
 Xeger x=new Xeger(regex);
 //Generating five strings that matches the given regex
    for (int i=0; i < n; i++) {
        String ans=x.generate(); //generate new string
        if(ans==\"\")
            System.out.println(\"\\u30B5\"); //Prints epsilon when string is empty.In some it will just print empty or 0
        else
     System.out.println(ans);
 }
 }
 }
//Xeger.java
//This program generates string of given expression and return strings
//This program uses the automation.jar
//For this you need to add this automation.jar into your java classpath or if you are using eclise then add it to lib folder and build the path.
 import java.util.List;
 import java.util.Random;
import dk.brics.automaton.Automaton;
 import dk.brics.automaton.RegExp;
 import dk.brics.automaton.State;
 import dk.brics.automaton.Transition;
/**
 * An object that will generate text from a regular expression. In a way, it\'s the opposite of a regular expression
 * matcher: an instance of this class will produce text that is guaranteed to match the regular expression passed in.
 */
 public class Xeger {
    private final Automaton automaton;
     private Random random;
    /**
      * Constructs a new instance, accepting the regular expression and the randomizer.
      *
      * @param regex The regular expression. (Not <code>null</code>.)
      * @param random The object that will randomize the way the String is generated. (Not <code>null</code>.)
      * @throws IllegalArgumentException If the regular expression is invalid.
      */
     public Xeger(String regex, Random random) {
         assert regex != null;
         assert random != null;
         this.automaton = new RegExp(regex).toAutomaton();
         this.random = random;
     }
    /**
      * As {@link nl.flotsam.xeger.Xeger#Xeger(String, java.util.Random)}, creating a {@link java.util.Random} instance
      * implicityly.
      */
     public Xeger(String regex) {
         this(regex, new Random());
     }
    /**
      * Generates a random String that is guaranteed to match the regular expression passed to the constructor.
      */
     public String generate() {
         StringBuilder builder = new StringBuilder();
         generate(builder, automaton.getInitialState());
         return builder.toString();
     }
    private void generate(StringBuilder builder, State state) {
         List<Transition> transitions = state.getSortedTransitions(false);
         if (transitions.size() == 0) {
             assert state.isAccept();
             return;
         }
         int nroptions = state.isAccept() ? transitions.size() : transitions.size() - 1;
         int option = Xeger.getRandomInt(0, nroptions, random);
         if (state.isAccept() && option == 0) {          // 0 is considered stop
             return;
         }
         // Moving on to next transition
         Transition transition = transitions.get(option - (state.isAccept() ? 1 : 0));
         appendChoice(builder, transition);
         generate(builder, transition.getDest());
     }
    private void appendChoice(StringBuilder builder, Transition transition) {
         char c = (char) Xeger.getRandomInt(transition.getMin(), transition.getMax(), random);
         builder.append(c);
     }
   public Random getRandom() {
        return random;
    }
   public void setRandom(Random random) {
        this.random = random;
    }
   /**
    * Generates a random number within the given bounds.
    *
    * @param min The minimum number (inclusive).
    * @param max The maximum number (inclusive).
    * @param random The object used as the randomizer.
    * @return A random number in the given range.
    */
    static int getRandomInt(int min, int max, Random random) {
        // Use random.nextInt as it guarantees a uniform distribution
        int maxForRandom=max-min+1;
        return random.nextInt(maxForRandom) + min;
    }
 }
//Output :
Enter the regex to generate string matching to the regex :
 01*|(10)*
 Enter the value of n :
 3
101010
 0111
101010101010



