01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
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);
}
}
}
}
|