Skip to content

Commit 706cbca

Browse files
committed
DfaInterpreter fix
2 parents ea7aefa + d7df452 commit 706cbca

File tree

5 files changed

+1907
-11
lines changed

5 files changed

+1907
-11
lines changed

Regex/Regex/Dfa/DfaBuilder.cs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ public DfaState Build(NfaState start)
1919
var initial = EpsilonClosure([start]);
2020
log?.Info($"Initial ε-closure: [{string.Join(", ", initial.Select(s => s.Id))}]");
2121

22-
bool isInitialFinal = initial.Any(s => s.IsFinal) && !IsEmptyPattern(start);
23-
var dfaStart = new DfaState(0, isInitialFinal);
22+
var dfaStart = new DfaState(0, initial.Any(s => s.IsFinal));
2423
mapping[initial] = dfaStart;
2524
queue.Enqueue(initial);
2625

@@ -66,12 +65,6 @@ public DfaState Build(NfaState start)
6665
return dfaStart;
6766
}
6867

69-
private bool IsEmptyPattern(NfaState start)
70-
{
71-
bool isEmpty = start.IsFinal && start.Transitions.Count == 0;
72-
log?.Info($"Checking empty pattern: {isEmpty}");
73-
return isEmpty;
74-
}
7568

7669
private Dictionary<RegexNode, HashSet<NfaState>> GetTransitions(HashSet<NfaState> states)
7770
{

Regex/Regex/Dfa/DfaInterpreter.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,21 @@ public static int TryMatch(DfaState start, string input, int startPos = 0, Log?
4848
}
4949
}
5050

51+
// Проверяем соответствие конца строки
52+
foreach (var transition in current.Transitions)
53+
{
54+
if (transition.Condition is RegexEndOfLine)
55+
{
56+
current = transition.Target;
57+
if (current.IsFinal)
58+
{
59+
maxLen = currentLen;
60+
log?.Info($"End of line matched, updating maxLen to {maxLen}");
61+
}
62+
break;
63+
}
64+
}
65+
5166
// Если не было ни одного совпадения, но начальное состояние финальное
5267
if (maxLen == -1 && start.IsFinal)
5368
{

Regex/Regex/Dot.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static void GenerateSvg(string dotFilePath, string outputSvgPath)
1919
{
2020
var processStartInfo = new ProcessStartInfo
2121
{
22-
FileName = "dot.exe",
22+
FileName = "dot",
2323
Arguments = $"-Tsvg \"{dotFilePath}\" -o \"{outputSvgPath}\"",
2424
RedirectStandardOutput = true,
2525
RedirectStandardError = true,

Tests/RegexTests/RegexTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public void Setup()
1414
Trace.AutoFlush = true;
1515
}
1616

17-
[TestMethod]
17+
[TestMethod]
1818
public void DfaConstruction_3()
1919
{
2020
var pattern = @"(//[^\n]*(\n|$)|\s)*";
@@ -165,7 +165,7 @@ public void DfaMatching()
165165
{
166166
var testCases = new[]
167167
{
168-
//(Start: 0, Pattern: @"(//[^\n]*(\n|$)|\s)*", Input: " // Top to Bottom", Expected: 18),
168+
(Start: 0, Pattern: @"(//[^\n]*(\n|$)|\s)*", Input: " // Top to Bottom", Expected: 18),
169169
(Start: 1, Pattern: @"(//[^\n]*(\n|$)|\s)*", Input: "; // Top to Bottom\r\n n", Expected: 24),
170170
//012345678901234567 8 901234567890
171171
// 10 20

0 commit comments

Comments
 (0)