contrib/re_parser: add Makefile, README and .gitignore
authorAlexandre Terrasa <alexandre@moz-code.org>
Mon, 20 Feb 2017 19:33:02 +0000 (14:33 -0500)
committerAlexandre Terrasa <alexandre@moz-code.org>
Mon, 20 Feb 2017 19:33:39 +0000 (14:33 -0500)
Signed-off-by: Alexandre Terrasa <alexandre@moz-code.org>

contrib/re_parser/.gitignore [new file with mode: 0644]
contrib/re_parser/Makefile [new file with mode: 0644]
contrib/re_parser/README.md [new file with mode: 0644]

diff --git a/contrib/re_parser/.gitignore b/contrib/re_parser/.gitignore
new file mode 100644 (file)
index 0000000..ca56bb2
--- /dev/null
@@ -0,0 +1,8 @@
+*.dot
+*parser_parser.nit
+*parser_lexer.nit
+*_test_parser*
+*.out
+
+re_parser
+re_app
diff --git a/contrib/re_parser/Makefile b/contrib/re_parser/Makefile
new file mode 100644 (file)
index 0000000..4997227
--- /dev/null
@@ -0,0 +1,27 @@
+NITC=../../bin/nitc
+NITCC=../../nitcc/src/nitcc
+NITUNIT=../../bin/nitunit
+
+all: re_parser re_app
+
+nitcc:
+       cd ../nitcc && make nitcc
+
+grammar: nitcc
+       cd src/ && ${NITCC} re_parser.sablecc
+
+re_parser: grammar
+       ${NITC} src/re_parser.nit
+
+re_app: grammar
+       ${NITC} src/re_app.nit
+
+check:
+       ${NITUNIT} .
+
+clean:
+       rm re_parser re_app 2>/dev/null || true
+       cd src/ && rm -r \
+               *.dot *.out \
+               re_parser_lexer.nit re_parser_parser.nit re_parser_test_parser.nit re_parser_parser_gen \
+               2>/dev/null || true
diff --git a/contrib/re_parser/README.md b/contrib/re_parser/README.md
new file mode 100644 (file)
index 0000000..82ee878
--- /dev/null
@@ -0,0 +1,75 @@
+# RE Parser
+
+RE parser provides a simple API to regular expression parsing.
+It is also able to convert a regular expression into a NFA or a DFA and produce dot files from it.
+
+## Building RE parser
+
+From the `re_parser` directory:
+
+~~~raw
+make all
+~~~
+
+## RE parser in command line
+
+RE parser can be used as a command line tool to generate NFA and DFA dot files from a regular expression:
+
+~~~raw
+./re_parser "a(b|c)+d*"
+~~~
+
+Will produce the two files `nfa.dot` and `dfa.dot`.
+
+These can be directly viwed with `xdot`:
+
+~~~raw
+xdot nfa.dot
+xdot dfa.dot
+~~~
+
+Or translated to png images with `dot`:
+
+~~~raw
+dot -Tpng -onfa.png nfa.dot
+dot -Tpng -odfa.png dfa.dot
+~~~
+
+See `man dot` for available formats.
+
+## RE parser as a web app
+
+RE parser comes with a web app that allow users to submit regular expression and see the resulting NFA and DFA.
+
+To run the web app server:
+
+~~~raw
+./re_app --host localhost --port 3000
+~~~
+
+The server will be available at <a href='http://localhost:3000'>http://localhost:3000</a>.
+
+## RE parser as a library
+
+You can also use RE parser as a library by importing `re_parser`.
+
+       import re_parser
+
+       var re = "a(b|c)+d*"
+
+       # Parse re
+       var re_parser = new REParser
+       var node = re_parser.parse_re(re)
+
+       if node == null then
+               print re_parser.last_error.as(not null)
+               exit 1
+               abort
+       end
+
+       # Build NFA and DFA
+       var nfa = re_parser.make_nfa(node)
+       print nfa.to_dot(false)
+       print nfa.to_dfa.to_dot(false)
+
+Use `to_dot(true)` to merge transitions on characters.