110 lines
3.6 KiB
C#
110 lines
3.6 KiB
C#
namespace CanonSharp.Common.LexicalAnalyzer;
|
|
|
|
public abstract class RegularExpression
|
|
{
|
|
public abstract NondeterministicFiniteAutomation Convert2Nfa();
|
|
|
|
public static RegularExpression Empty => new EmptyExpression();
|
|
|
|
public static RegularExpression Single(char c) => new SymbolExpression(c);
|
|
|
|
public static RegularExpression Alternate(RegularExpression left, RegularExpression right) =>
|
|
new AlternationExpression(left, right);
|
|
|
|
public static RegularExpression Concatenate(RegularExpression first, RegularExpression second) =>
|
|
new ConcatenationExpression(first, second);
|
|
|
|
public static RegularExpression Kleene(RegularExpression inner) => new KleeneExpression(inner);
|
|
}
|
|
|
|
public class EmptyExpression : RegularExpression
|
|
{
|
|
public override NondeterministicFiniteAutomation Convert2Nfa()
|
|
{
|
|
NondeterministicState final = new();
|
|
NondeterministicState start = new();
|
|
start.AddTransaction(EmptyChar.Empty, final);
|
|
|
|
return new NondeterministicFiniteAutomation(start, [final]);
|
|
}
|
|
}
|
|
|
|
public class SymbolExpression(char symbol) : RegularExpression
|
|
{
|
|
public char Symbol { get; } = symbol;
|
|
|
|
public override NondeterministicFiniteAutomation Convert2Nfa()
|
|
{
|
|
NondeterministicState final = new();
|
|
NondeterministicState start = new();
|
|
start.AddTransaction(new EmptyChar(Symbol), final);
|
|
|
|
return new NondeterministicFiniteAutomation(start, [final]);
|
|
}
|
|
}
|
|
|
|
public class AlternationExpression(RegularExpression left, RegularExpression right) : RegularExpression
|
|
{
|
|
public RegularExpression Left { get; } = left;
|
|
|
|
public RegularExpression Right { get; } = right;
|
|
|
|
public override NondeterministicFiniteAutomation Convert2Nfa()
|
|
{
|
|
NondeterministicFiniteAutomation left = Left.Convert2Nfa();
|
|
NondeterministicFiniteAutomation right = Right.Convert2Nfa();
|
|
|
|
NondeterministicState final = new();
|
|
foreach (NondeterministicState state in left.FinalStates.Concat(right.FinalStates))
|
|
{
|
|
state.AddTransaction(EmptyChar.Empty, final);
|
|
}
|
|
|
|
NondeterministicState start = new();
|
|
start.AddTransaction(EmptyChar.Empty, left.Start);
|
|
start.AddTransaction(EmptyChar.Empty, right.Start);
|
|
|
|
return new NondeterministicFiniteAutomation(start, [final]);
|
|
}
|
|
}
|
|
|
|
public class ConcatenationExpression(RegularExpression first, RegularExpression second) : RegularExpression
|
|
{
|
|
public RegularExpression First { get; } = first;
|
|
|
|
public RegularExpression Second { get; } = second;
|
|
|
|
public override NondeterministicFiniteAutomation Convert2Nfa()
|
|
{
|
|
NondeterministicFiniteAutomation first = First.Convert2Nfa();
|
|
NondeterministicFiniteAutomation second = Second.Convert2Nfa();
|
|
|
|
foreach (NondeterministicState state in first.FinalStates)
|
|
{
|
|
state.AddTransaction(EmptyChar.Empty, second.Start);
|
|
}
|
|
|
|
return new NondeterministicFiniteAutomation(first.Start, second.FinalStates);
|
|
}
|
|
}
|
|
|
|
public class KleeneExpression(RegularExpression inner) : RegularExpression
|
|
{
|
|
public RegularExpression Inner { get; } = inner;
|
|
|
|
public override NondeterministicFiniteAutomation Convert2Nfa()
|
|
{
|
|
NondeterministicFiniteAutomation inner = Inner.Convert2Nfa();
|
|
|
|
NondeterministicState final = new();
|
|
final.AddTransaction(EmptyChar.Empty, inner.Start);
|
|
|
|
foreach (NondeterministicState state in inner.FinalStates)
|
|
{
|
|
state.AddTransaction(EmptyChar.Empty, final);
|
|
}
|
|
|
|
return new NondeterministicFiniteAutomation(final, [final]);
|
|
}
|
|
}
|