Token should build its own NFA

This commit is contained in:
Josh Holtrop 2021-06-06 14:09:28 -04:00
parent afea886ecb
commit 701903def2
3 changed files with 14 additions and 7 deletions

View File

@ -46,13 +46,9 @@ module Imbecile
end end
# Build NFA from each token expression. # Build NFA from each token expression.
i = 0
nfas = @tokens.map do |token| nfas = @tokens.map do |token|
regex = Regex.new(token.pattern) puts token.nfa
regex.nfa.end_state.accepts = "#{i}:#{token.name}" token.nfa
puts regex.nfa
i += 1
regex.nfa
end end
dfa = Regex::DFA.new(nfas) dfa = Regex::DFA.new(nfas)
puts dfa puts dfa

View File

@ -15,10 +15,21 @@ module Imbecile
# Token ID. # Token ID.
attr_reader :id attr_reader :id
# @return [Regex::NFA]
# Regex NFA for matching the token.
attr_reader :nfa
def initialize(name, pattern, id) def initialize(name, pattern, id)
@name = name @name = name
@pattern = pattern @pattern = pattern
@id = id @id = id
regex = Regex.new(pattern)
regex.nfa.end_state.accepts = self
@nfa = regex.nfa
end
def to_s
@name
end end
end end

View File

@ -38,7 +38,7 @@ module Imbecile
nfa_state_set.each do |nfa_state| nfa_state_set.each do |nfa_state|
if nfa_state.accepts if nfa_state.accepts
if state.accepts if state.accepts
if nfa_state.accepts.to_i < state.accepts.to_i if nfa_state.accepts.id < state.accepts.id
state.accepts = nfa_state.accepts state.accepts = nfa_state.accepts
end end
else else