Merge: nitunit: Use markdown2
[nit.git] / contrib / re_parser / README.md
1 # RE Parser
2
3 RE parser provides a simple API to regular expression parsing.
4 It is also able to convert a regular expression into a NFA or a DFA and produce dot files from it.
5
6 ## Building RE parser
7
8 From the `re_parser` directory:
9
10 ~~~raw
11 make all
12 ~~~
13
14 ## RE parser in command line
15
16 RE parser can be used as a command line tool to generate NFA and DFA dot files from a regular expression:
17
18 ~~~raw
19 ./re_parser "a(b|c)+d*"
20 ~~~
21
22 Will produce the two files `nfa.dot` and `dfa.dot`.
23
24 These can be directly viwed with `xdot`:
25
26 ~~~raw
27 xdot nfa.dot
28 xdot dfa.dot
29 ~~~
30
31 Or translated to png images with `dot`:
32
33 ~~~raw
34 dot -Tpng -onfa.png nfa.dot
35 dot -Tpng -odfa.png dfa.dot
36 ~~~
37
38 See `man dot` for available formats.
39
40 ## RE parser as a web app
41
42 RE parser comes with a web app that allow users to submit regular expression and see the resulting NFA and DFA.
43
44 To run the web app server:
45
46 ~~~raw
47 ./re_app --host localhost --port 3000
48 ~~~
49
50 The server will be available at <a href='http://localhost:3000'>http://localhost:3000</a>.
51
52 ## RE parser as a library
53
54 You can also use RE parser as a library by importing `re_parser`.
55
56         import re_parser
57
58         var re = "a(b|c)+d*"
59
60         # Parse re
61         var re_parser = new REParser
62         var node = re_parser.parse_re(re)
63
64         if node == null then
65                 print re_parser.last_error.as(not null)
66                 exit 1
67                 abort
68         end
69
70         # Build NFA and DFA
71         var nfa = re_parser.make_nfa(node)
72         print nfa.to_dot(false)
73         print nfa.to_dfa.to_dot(false)
74
75 Use `to_dot(true)` to merge transitions on characters.