1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Transformation of regular expression to NFA
22 # Build the NFA of the regular expression
23 fun make_rfa
: Automaton do
30 # The real value of the string
31 fun value
: String do return text
.substring
(1, text
.length-2
).unescape_nit
32 redef fun make_rfa
: Automaton
34 var a
= new Automaton.epsilon
36 for c
in self.value
do
37 var b
= new Automaton.atom
(c
.ascii
)
45 # The real value of the char
46 fun value
: String do return text
.substring_from
(1).to_i
.ascii
.to_s
47 redef fun make_rfa
: Automaton
49 var a
= new Automaton.atom
(self.value
.first
.ascii
)
55 redef fun make_rfa
: Automaton
57 assert children
.length
== 1 else print
"no make_rfa for {self}"
58 return children
.first
.make_rfa
65 var a
= children
[0].make_rfa
66 var b
= children
[2].make_rfa
75 var a
= children
[0].make_rfa
76 var b
= children
[2].make_rfa
.to_dfa
77 for t
in b
.start
.outs
do
78 if not t
.to
.outs
.is_empty
then
79 print
"Not Yet Implemented Error: '-' only works on single char"
82 a
.minus_sym
(t
.symbol
.as(not null))
91 var a
= children
[0].make_rfa
92 var b
= children
[1].make_rfa
101 var a
= children
[0].make_rfa
110 var a
= children
[0].make_rfa
119 var a
= children
[0].make_rfa
128 return children
[1].make_rfa
132 redef class Nre_class
133 redef fun make_rfa
: Automaton
135 var c1
= children
[0].as(Nstr).value
136 var c2
= children
[3].as(Nstr).value
137 if c1
.length
!= 1 or c2
.length
!= 1 then
138 print
"Classes only works on single char"
142 var a
= new Automaton.cla
(c1
.first
.ascii
, c2
.first
.ascii
)
148 redef fun make_rfa
: Automaton
150 var a
= new Automaton.cla
(0, 127)