2024-03-13 16:41:44 +08:00
|
|
|
|
using Canon.Core.Abstractions;
|
|
|
|
|
using Canon.Core.Enums;
|
2024-03-11 22:22:53 +08:00
|
|
|
|
using Canon.Core.GrammarParser;
|
|
|
|
|
using Xunit.Abstractions;
|
|
|
|
|
|
|
|
|
|
namespace Canon.Tests.GrammarParserTests;
|
|
|
|
|
|
|
|
|
|
public class SimpleGrammarWithEmptyTests(ITestOutputHelper testOutputHelper)
|
|
|
|
|
{
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 带有空产生式的简单语法(课后题4.18)
|
|
|
|
|
/// S -> A
|
|
|
|
|
/// A -> BA | ε
|
2024-03-12 14:52:42 +08:00
|
|
|
|
/// B -> aB | a
|
2024-03-11 22:22:53 +08:00
|
|
|
|
/// 为了方便测试指定
|
|
|
|
|
/// A ProgramStruct
|
|
|
|
|
/// B ProgramBody
|
|
|
|
|
/// a Identifier
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
|
|
|
|
private readonly ITestOutputHelper _testOutputHelper = testOutputHelper;
|
|
|
|
|
|
|
|
|
|
private static readonly Dictionary<NonTerminator, List<List<TerminatorBase>>> s_simpleGrammar = new()
|
|
|
|
|
{
|
|
|
|
|
{
|
|
|
|
|
new NonTerminator(NonTerminatorType.StartNonTerminator), [
|
|
|
|
|
[new NonTerminator(NonTerminatorType.ProgramStruct)]
|
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
new NonTerminator(NonTerminatorType.ProgramStruct), [
|
|
|
|
|
[
|
|
|
|
|
new NonTerminator(NonTerminatorType.ProgramBody),
|
|
|
|
|
new NonTerminator(NonTerminatorType.ProgramStruct)
|
|
|
|
|
],
|
2024-03-12 14:52:42 +08:00
|
|
|
|
[Terminator.EmptyTerminator]
|
2024-03-11 22:22:53 +08:00
|
|
|
|
]
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
new NonTerminator(NonTerminatorType.ProgramBody), [
|
|
|
|
|
[
|
|
|
|
|
Terminator.IdentifierTerminator,
|
|
|
|
|
new NonTerminator(NonTerminatorType.ProgramBody)
|
|
|
|
|
],
|
|
|
|
|
[Terminator.IdentifierTerminator]
|
|
|
|
|
]
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public void FirstSetTest()
|
|
|
|
|
{
|
|
|
|
|
GrammarBuilder builder = new()
|
|
|
|
|
{
|
|
|
|
|
Generators = s_simpleGrammar, Begin = new NonTerminator(NonTerminatorType.StartNonTerminator)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
builder.Build();
|
|
|
|
|
|
|
|
|
|
Assert.Contains(builder.FirstSet, pair =>
|
2024-03-12 14:52:42 +08:00
|
|
|
|
{
|
|
|
|
|
if (pair.Key == new NonTerminator(NonTerminatorType.StartNonTerminator))
|
|
|
|
|
{
|
|
|
|
|
Assert.Equal(2, pair.Value.Count);
|
|
|
|
|
Assert.Contains(Terminator.IdentifierTerminator, pair.Value);
|
|
|
|
|
Assert.Contains(Terminator.EmptyTerminator, pair.Value);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
});
|
|
|
|
|
|
2024-03-11 22:22:53 +08:00
|
|
|
|
Assert.Contains(builder.FirstSet, pair =>
|
2024-03-12 14:52:42 +08:00
|
|
|
|
{
|
|
|
|
|
if (pair.Key == new NonTerminator(NonTerminatorType.ProgramStruct))
|
|
|
|
|
{
|
|
|
|
|
Assert.Equal(2, pair.Value.Count);
|
|
|
|
|
Assert.Contains(Terminator.IdentifierTerminator, pair.Value);
|
|
|
|
|
Assert.Contains(Terminator.EmptyTerminator, pair.Value);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return true;
|
|
|
|
|
});
|
2024-03-11 22:22:53 +08:00
|
|
|
|
Assert.Contains(builder.FirstSet, pair =>
|
|
|
|
|
{
|
2024-03-12 14:52:42 +08:00
|
|
|
|
if (pair.Key == new NonTerminator(NonTerminatorType.ProgramBody))
|
|
|
|
|
{
|
|
|
|
|
Assert.Single(pair.Value);
|
|
|
|
|
Assert.Contains(Terminator.IdentifierTerminator, pair.Value);
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
});
|
2024-03-11 22:22:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
2024-03-12 14:52:42 +08:00
|
|
|
|
public void StatesTest()
|
2024-03-11 22:22:53 +08:00
|
|
|
|
{
|
|
|
|
|
GrammarBuilder builder = new()
|
|
|
|
|
{
|
|
|
|
|
Generators = s_simpleGrammar, Begin = new NonTerminator(NonTerminatorType.StartNonTerminator)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Grammar grammar = builder.Build();
|
|
|
|
|
|
|
|
|
|
Assert.Equal(6, builder.Automation.Count);
|
|
|
|
|
|
|
|
|
|
Assert.Contains(new NonTerminator(NonTerminatorType.ProgramStruct), grammar.BeginState.Transformer.Keys);
|
|
|
|
|
Assert.Contains(new NonTerminator(NonTerminatorType.ProgramBody), grammar.BeginState.Transformer.Keys);
|
|
|
|
|
Assert.Contains(Terminator.IdentifierTerminator, grammar.BeginState.Transformer.Keys);
|
|
|
|
|
Assert.Equal(7, grammar.BeginState.Expressions.Count);
|
|
|
|
|
_testOutputHelper.WriteLine("--- 0 ---");
|
|
|
|
|
_testOutputHelper.WriteLine(grammar.BeginState.ToString());
|
|
|
|
|
|
|
|
|
|
LrState state1 =
|
|
|
|
|
grammar.BeginState.Transformer[new NonTerminator(NonTerminatorType.ProgramStruct)];
|
|
|
|
|
Assert.Single(state1.Expressions);
|
|
|
|
|
_testOutputHelper.WriteLine("--- 1 ---");
|
|
|
|
|
_testOutputHelper.WriteLine(state1.ToString());
|
|
|
|
|
|
|
|
|
|
LrState state2 =
|
|
|
|
|
grammar.BeginState.Transformer[new NonTerminator(NonTerminatorType.ProgramBody)];
|
|
|
|
|
Assert.Contains(new NonTerminator(NonTerminatorType.ProgramStruct), state2.Transformer.Keys);
|
|
|
|
|
Assert.Contains(new NonTerminator(NonTerminatorType.ProgramBody), state2.Transformer.Keys);
|
|
|
|
|
Assert.Contains(Terminator.IdentifierTerminator, state2.Transformer.Keys);
|
|
|
|
|
Assert.Equal(7, state2.Expressions.Count);
|
|
|
|
|
_testOutputHelper.WriteLine("--- 2 ---");
|
|
|
|
|
_testOutputHelper.WriteLine(state2.ToString());
|
|
|
|
|
|
|
|
|
|
LrState state3 =
|
|
|
|
|
grammar.BeginState.Transformer[Terminator.IdentifierTerminator];
|
|
|
|
|
Assert.Contains(new NonTerminator(NonTerminatorType.ProgramBody), state3.Transformer.Keys);
|
|
|
|
|
Assert.Contains(Terminator.IdentifierTerminator, state3.Transformer.Keys);
|
|
|
|
|
Assert.Equal(8, state3.Expressions.Count);
|
|
|
|
|
_testOutputHelper.WriteLine("--- 3 ---");
|
|
|
|
|
_testOutputHelper.WriteLine(state3.ToString());
|
|
|
|
|
|
|
|
|
|
LrState state4 = state2.Transformer[new NonTerminator(NonTerminatorType.ProgramStruct)];
|
|
|
|
|
Assert.Empty(state4.Transformer);
|
|
|
|
|
Assert.Single(state4.Expressions);
|
|
|
|
|
_testOutputHelper.WriteLine("--- 4 ---");
|
|
|
|
|
_testOutputHelper.WriteLine(state4.ToString());
|
|
|
|
|
|
|
|
|
|
LrState state5 = state3.Transformer[new NonTerminator(NonTerminatorType.ProgramBody)];
|
|
|
|
|
Assert.Empty(state5.Transformer);
|
|
|
|
|
Assert.Equal(2, state5.Expressions.Count);
|
|
|
|
|
_testOutputHelper.WriteLine("--- 5 ---");
|
|
|
|
|
_testOutputHelper.WriteLine(state5.ToString());
|
|
|
|
|
}
|
2024-03-12 14:52:42 +08:00
|
|
|
|
|
2024-03-13 16:41:44 +08:00
|
|
|
|
[Fact]
|
|
|
|
|
public void ParserTest()
|
|
|
|
|
{
|
|
|
|
|
GrammarBuilder builder = new()
|
|
|
|
|
{
|
|
|
|
|
Generators = s_simpleGrammar, Begin = new NonTerminator(NonTerminatorType.StartNonTerminator)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
Grammar grammar = builder.Build();
|
2024-04-12 19:01:37 +08:00
|
|
|
|
IGrammarParser parser = grammar.ToGrammarParser();
|
2024-03-13 16:41:44 +08:00
|
|
|
|
|
|
|
|
|
ITransformer transformer1 = parser.BeginTransformer;
|
|
|
|
|
Assert.Equal(3, transformer1.ShiftTable.Count);
|
|
|
|
|
Assert.Single(transformer1.ReduceTable);
|
|
|
|
|
Assert.Contains(new NonTerminator(NonTerminatorType.ProgramStruct),transformer1.ShiftTable);
|
|
|
|
|
}
|
2024-03-11 22:22:53 +08:00
|
|
|
|
}
|