package algs54; // section 5.4
import stdlib.*;
/* ***********************************************************************
 *  Compilation:  javac GREP.java
 *  Execution:    java GREP regexp < input.txt
 *  Dependencies: NFA.java
 *  Data files:   http://algs4.cs.princeton.edu/54regexp/tinyL.txt
 *
 *  This program takes an RE as a command-line argument and prints
 *  the lines from standard input having some substring that
 *  is in the language described by the RE.
 *
 *  % more tinyL.txt
 *  AC
 *  AD
 *  AAA
 *  ABD
 *  ADD
 *  BCD
 *  ABCCBD
 *  BABAAA
 *  BABBAAA
 *
 *  %  java GREP "(A*B|AC)D" < tinyL.txt
 *  ABD
 *  ABCCBD
 *
 *************************************************************************/

public class GREP {
	public static void main(String[] args) {
		StdIn.fromFile ("data/tinyL.txt");
		args = new String[] { "(A*B|AC)D" };

		String regexp = "(.*" + args[0] + ".*)";
		NFA nfa = new NFA(regexp);
		while (StdIn.hasNextLine()) {
			String txt = StdIn.readLine();
			if (nfa.recognizes(txt)) {
				StdOut.println(txt);
			}
		}
	}
}
