Merge: contrib/re_parser
authorJean Privat <jean@pryen.org>
Mon, 27 Feb 2017 18:06:48 +0000 (13:06 -0500)
committerJean Privat <jean@pryen.org>
Mon, 27 Feb 2017 18:06:48 +0000 (13:06 -0500)
commit69d1a21891f2bd6824bf7c90f7c29597c2a7b3e8
tree320c6e887704722d87c283329ec82dbf2d3449cd
parent1fc94ddfe04ff56e12528dae607c7571649821f7
parent300cf18c6c6ef844d3b010e89c53e2126adcc8d7
Merge: contrib/re_parser

# 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:

~~~bash
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:

~~~bash
./re_parser "a(b|c)+d*"
~~~

Will produce the two files `nfa.dot` and `dfa.dot`.

These can be directly viwed with `xdot`:

~~~bash
xdot nfa.dot
xdot dfa.dot
~~~

Or translated to png images with `dot`:

~~~bash
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:

~~~bash
./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`.

~~~nit
import re_parser

var re = "a(b|c)+d*"

# Parse the expression
var l = new Lexer_re_parser(re)
var p = new Parser_re_parser
p.tokens.add_all l.lex
var node = p.parse

# Check errors
if not node isa NProd then
print "Error parsing the regular expression"
abort
end

# Get NFA and DFA
var v = new REVisitor
v.start(node)
print v.nfa.to_dot
print v.nfa.to_dfa.to_dot
~~~

Web app demo: http://re.moz-code.org/

Pull-Request: #2373
Reviewed-by: Jean Privat <jean@pryen.org>
Reviewed-by: Jean-Christophe Beaupré <jcbrinfo.public@gmail.com>
contrib/nitcc/src/autom.nit
contrib/nitcc/src/nitcc.nit