001package algs54; // section 5.4
002import stdlib.*;
003/* ***********************************************************************
004 *  Compilation:  javac GREP.java
005 *  Execution:    java GREP regexp < input.txt
006 *  Dependencies: NFA.java
007 *  Data files:   http://algs4.cs.princeton.edu/54regexp/tinyL.txt
008 *
009 *  This program takes an RE as a command-line argument and prints
010 *  the lines from standard input having some substring that
011 *  is in the language described by the RE.
012 *
013 *  % more tinyL.txt
014 *  AC
015 *  AD
016 *  AAA
017 *  ABD
018 *  ADD
019 *  BCD
020 *  ABCCBD
021 *  BABAAA
022 *  BABBAAA
023 *
024 *  %  java GREP "(A*B|AC)D" < tinyL.txt
025 *  ABD
026 *  ABCCBD
027 *
028 *************************************************************************/
029
030public class GREP {
031        public static void main(String[] args) {
032                StdIn.fromFile ("data/tinyL.txt");
033                args = new String[] { "(A*B|AC)D" };
034
035                String regexp = "(.*" + args[0] + ".*)";
036                NFA nfa = new NFA(regexp);
037                while (StdIn.hasNextLine()) {
038                        String txt = StdIn.readLine();
039                        if (nfa.recognizes(txt)) {
040                                StdOut.println(txt);
041                        }
042                }
043        }
044}