1
Chapter Eight: Regular Expression Applications
Formal Language, chapter 8, slide 1
Chapter Eight: Regular Expression Applications Formal Language, - - PowerPoint PPT Presentation
Chapter Eight: Regular Expression Applications Formal Language, chapter 8, slide 1 1 We have seen some of the implementation techniques related to DFAs and NFAs. These important techniques are like tricks of the programmer's trade,
1
Formal Language, chapter 8, slide 1
2
Formal Language, chapter 8, slide 2
3
Formal Language, chapter 8, slide 3
4
Formal Language, chapter 8, slide 4
5
Formal Language, chapter 8, slide 5
6
Formal Language, chapter 8, slide 6
7
Formal Language, chapter 8, slide 7
8
Formal Language, chapter 8, slide 8
9
Formal Language, chapter 8, slide 9
10
Formal Language, chapter 8, slide 10
11
Formal Language, chapter 8, slide 11
12
Formal Language, chapter 8, slide 12
13
Formal Language, chapter 8, slide 13
14
Formal Language, chapter 8, slide 14
15
Formal Language, chapter 8, slide 15
16
Formal Language, chapter 8, slide 16
17
Formal Language, chapter 8, slide 17
18
Formal Language, chapter 8, slide 18
19
– it enters an accepting state: that's a match – enters a non-accepting trap state: restart the DFA from the next possible starting position – hits the end of the string: restart the DFA from the next possible starting position
Formal Language, chapter 8, slide 19
20
– The string abcab contains two substrings that match the regexp ab
– The string abb contains three substrings that match the regexp ab*, and they all start at the first symbol
Formal Language, chapter 8, slide 20
21
Formal Language, chapter 8, slide 21
22
Formal Language, chapter 8, slide 22
23
Formal Language, chapter 8, slide 23
24
to test
Formal Language, chapter 8, slide 24
25
Formal Language, chapter 8, slide 25
26
import java.io.*; import java.util.regex.*; /** * A Java application to demonstrate the Java package * java.util.regex. We take one command-line argument, * which is treated as a regexp and compiled into a * Pattern. We then use that pattern to filter the * standard input, echoing to standard output only * those lines that match the Pattern. */
Formal Language, chapter 8, slide 26
27
class RegexFilter { public static void main(String[] args) throws IOException { Pattern p = Pattern.compile(args[0]); // the regexp BufferedReader in = // standard input new BufferedReader(new InputStreamReader(System.in)); // Read and echo lines until EOF. String s = in.readLine(); while (s!=null) { Matcher m = p.matcher(s); if (m.matches()) System.out.println(s); s = in.readLine(); } } }
Formal Language, chapter 8, slide 27
28
Formal Language, chapter 8, slide 28
29
Formal Language, chapter 8, slide 29
30
– Convert regexp to automaton – Simulate the automaton on a text file – Discard automaton when finished
Formal Language, chapter 8, slide 30
31
Formal Language, chapter 8, slide 31
32
Formal Language, chapter 8, slide 32
33
Formal Language, chapter 8, slide 33
34
Formal Language, chapter 8, slide 34
35
Formal Language, chapter 8, slide 35
36
Formal Language, chapter 8, slide 36
37
Formal Language, chapter 8, slide 37
38
Formal Language, chapter 8, slide 38
39
– lex generates some source – Other tools (like yacc) generate some source – Some is written by hand
Formal Language, chapter 8, slide 39