2024-03-13 16:41:44 +08:00
|
|
|
|
using Canon.Core.Abstractions;
|
2024-03-11 19:33:02 +08:00
|
|
|
|
|
|
|
|
|
namespace Canon.Core.GrammarParser;
|
2024-03-10 15:37:18 +08:00
|
|
|
|
|
2024-03-13 16:41:44 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 通过LR分析方法建立的语法
|
|
|
|
|
/// </summary>
|
2024-03-10 15:37:18 +08:00
|
|
|
|
public class Grammar
|
|
|
|
|
{
|
2024-03-13 16:41:44 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 起始符
|
|
|
|
|
/// </summary>
|
2024-03-10 15:37:18 +08:00
|
|
|
|
public required NonTerminator Begin { get; init; }
|
|
|
|
|
|
2024-03-13 16:41:44 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 语法中的DFA
|
|
|
|
|
/// </summary>
|
|
|
|
|
public required HashSet<LrState> Automation { get; init; }
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 起始状态
|
|
|
|
|
/// </summary>
|
2024-03-10 15:37:18 +08:00
|
|
|
|
public required LrState BeginState { get; init; }
|
2024-03-11 19:33:02 +08:00
|
|
|
|
|
2024-03-13 16:41:44 +08:00
|
|
|
|
public GrammarParserBase ToGrammarParser()
|
2024-03-11 19:33:02 +08:00
|
|
|
|
{
|
2024-03-13 16:41:44 +08:00
|
|
|
|
Dictionary<LrState, Transformer> transformers = [];
|
2024-03-11 19:33:02 +08:00
|
|
|
|
|
2024-03-13 16:41:44 +08:00
|
|
|
|
foreach (LrState state in Automation)
|
2024-03-11 19:33:02 +08:00
|
|
|
|
{
|
2024-03-13 16:41:44 +08:00
|
|
|
|
ITransformer transformer;
|
|
|
|
|
if (transformers.TryGetValue(state, out Transformer? oldTransformer))
|
2024-03-11 19:33:02 +08:00
|
|
|
|
{
|
2024-03-13 16:41:44 +08:00
|
|
|
|
transformer = oldTransformer;
|
2024-03-11 19:33:02 +08:00
|
|
|
|
}
|
2024-03-13 16:41:44 +08:00
|
|
|
|
else
|
2024-03-11 19:33:02 +08:00
|
|
|
|
{
|
2024-03-13 16:41:44 +08:00
|
|
|
|
Transformer newTransformer = new();
|
|
|
|
|
transformers.Add(state, newTransformer);
|
|
|
|
|
transformer = newTransformer;
|
2024-03-11 19:33:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-13 16:41:44 +08:00
|
|
|
|
// 生成归约的迁移表
|
|
|
|
|
foreach (Expression expression in state.Expressions)
|
2024-03-11 19:33:02 +08:00
|
|
|
|
{
|
2024-03-13 16:41:44 +08:00
|
|
|
|
if (expression.Pos == expression.Right.Count)
|
|
|
|
|
{
|
|
|
|
|
transformer.ReduceTable.TryAdd(expression.LookAhead, new ReduceInformation(
|
|
|
|
|
expression.Right.Count, expression.Left));
|
|
|
|
|
}
|
2024-03-11 19:33:02 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-03-13 16:41:44 +08:00
|
|
|
|
// 生成移进的迁移表
|
|
|
|
|
foreach (KeyValuePair<TerminatorBase,LrState> pair in state.Transformer)
|
2024-03-11 19:33:02 +08:00
|
|
|
|
{
|
2024-03-13 16:41:44 +08:00
|
|
|
|
ITransformer targetTransformer;
|
|
|
|
|
if (transformers.TryGetValue(pair.Value, out Transformer? oldTransformer2))
|
2024-03-11 19:33:02 +08:00
|
|
|
|
{
|
2024-03-13 16:41:44 +08:00
|
|
|
|
targetTransformer = oldTransformer2;
|
2024-03-11 19:33:02 +08:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2024-03-13 16:41:44 +08:00
|
|
|
|
Transformer newTransformer = new();
|
|
|
|
|
transformers.Add(pair.Value, newTransformer);
|
|
|
|
|
targetTransformer = newTransformer;
|
2024-03-11 19:33:02 +08:00
|
|
|
|
}
|
2024-03-13 16:41:44 +08:00
|
|
|
|
transformer.ShiftTable.TryAdd(pair.Key, targetTransformer);
|
2024-03-11 19:33:02 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
2024-03-13 16:41:44 +08:00
|
|
|
|
|
|
|
|
|
return new GrammarParser(transformers[BeginState], Begin);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class GrammarParser(ITransformer beginTransformer, NonTerminator begin) : GrammarParserBase
|
|
|
|
|
{
|
|
|
|
|
public override ITransformer BeginTransformer { get; } = beginTransformer;
|
|
|
|
|
public override NonTerminator Begin { get; } = begin;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private class Transformer : ITransformer
|
|
|
|
|
{
|
2024-04-08 19:46:24 +08:00
|
|
|
|
public string Name => string.Empty;
|
|
|
|
|
|
2024-03-13 16:41:44 +08:00
|
|
|
|
public IDictionary<TerminatorBase, ITransformer> ShiftTable { get; }
|
|
|
|
|
= new Dictionary<TerminatorBase, ITransformer>();
|
|
|
|
|
|
|
|
|
|
public IDictionary<Terminator, ReduceInformation> ReduceTable { get; }
|
|
|
|
|
= new Dictionary<Terminator, ReduceInformation>();
|
2024-03-11 19:33:02 +08:00
|
|
|
|
}
|
2024-03-10 15:37:18 +08:00
|
|
|
|
}
|