feat:修改文法制导定义为S属性定义,以避免左递归 (#55)
Co-authored-by: jackfiled <xcrenchangjun@outlook.com> Reviewed-on: PostGuard/Canon#55 Co-authored-by: ichirinko <1621543655@qq.com> Co-committed-by: ichirinko <1621543655@qq.com>
This commit is contained in:
parent
b4a6632018
commit
b20c3234c5
File diff suppressed because it is too large
Load Diff
|
@ -54,15 +54,19 @@ public static class PascalGrammar
|
|||
]
|
||||
},
|
||||
{
|
||||
// IdList -> id | IdList , id
|
||||
// (deprecated)IdList -> id | IdList , id
|
||||
|
||||
// 更改语法制导定义为S属性定义
|
||||
// IdList -> , id IdList | : Type
|
||||
new NonTerminator(NonTerminatorType.IdentifierList), [
|
||||
[
|
||||
new Terminator(DelimiterType.Comma),
|
||||
Terminator.IdentifierTerminator,
|
||||
new NonTerminator(NonTerminatorType.IdentifierList),
|
||||
],
|
||||
[
|
||||
new NonTerminator(NonTerminatorType.IdentifierList),
|
||||
new Terminator(DelimiterType.Comma),
|
||||
Terminator.IdentifierTerminator
|
||||
new Terminator(DelimiterType.Colon),
|
||||
new NonTerminator(NonTerminatorType.Type)
|
||||
]
|
||||
]
|
||||
},
|
||||
|
@ -127,19 +131,20 @@ public static class PascalGrammar
|
|||
]
|
||||
},
|
||||
{
|
||||
// VarDeclaration -> IdList : Type | VarDeclaration ; IdList : Type
|
||||
// (deprecated) VarDeclaration -> IdList : Type | VarDeclaration ; IdList : Type
|
||||
|
||||
// VarDeclaration -> id IdList | VarDeclaration ; id IdList
|
||||
// 更改语法制导定义为S属性定义
|
||||
new NonTerminator(NonTerminatorType.VarDeclaration), [
|
||||
[
|
||||
new NonTerminator(NonTerminatorType.IdentifierList),
|
||||
new Terminator(DelimiterType.Colon),
|
||||
new NonTerminator(NonTerminatorType.Type)
|
||||
Terminator.IdentifierTerminator,
|
||||
new NonTerminator(NonTerminatorType.IdentifierList)
|
||||
],
|
||||
[
|
||||
new NonTerminator(NonTerminatorType.VarDeclaration),
|
||||
new Terminator(DelimiterType.Semicolon),
|
||||
Terminator.IdentifierTerminator,
|
||||
new NonTerminator(NonTerminatorType.IdentifierList),
|
||||
new Terminator(DelimiterType.Colon),
|
||||
new NonTerminator(NonTerminatorType.Type)
|
||||
]
|
||||
]
|
||||
},
|
||||
|
@ -281,12 +286,13 @@ public static class PascalGrammar
|
|||
]
|
||||
},
|
||||
{
|
||||
// ValueParameter -> IdList : BasicType
|
||||
// (deprecated)ValueParameter -> IdList : BasicType
|
||||
// 更改语法制导定义为S属性定义
|
||||
// ValueParameter -> id IdList
|
||||
new NonTerminator(NonTerminatorType.ValueParameter), [
|
||||
[
|
||||
new NonTerminator(NonTerminatorType.IdentifierList),
|
||||
new Terminator(DelimiterType.Colon),
|
||||
new NonTerminator(NonTerminatorType.BasicType)
|
||||
Terminator.IdentifierTerminator,
|
||||
new NonTerminator(NonTerminatorType.IdentifierList)
|
||||
]
|
||||
]
|
||||
},
|
||||
|
|
|
@ -22,7 +22,7 @@ public class IdentifierList : NonTerminatedSyntaxNode
|
|||
{
|
||||
bool isRecursive;
|
||||
|
||||
if (children.Count == 1)
|
||||
if (children.Count == 2)
|
||||
{
|
||||
isRecursive = false;
|
||||
}
|
||||
|
|
|
@ -10,12 +10,12 @@ public class ValueParameter : NonTerminatedSyntaxNode
|
|||
/// <summary>
|
||||
/// 声明的变量列表
|
||||
/// </summary>
|
||||
public IdentifierList IdentifierList => Children[0].Convert<IdentifierList>();
|
||||
// public IdentifierList IdentifierList => Children[1].Convert<IdentifierList>();
|
||||
|
||||
/// <summary>
|
||||
/// 声明的变量类型
|
||||
/// </summary>
|
||||
public BasicType BasicType => Children[2].Convert<BasicType>();
|
||||
// public BasicType BasicType => Children[2].Convert<BasicType>();
|
||||
|
||||
public static ValueParameter Create(List<SyntaxNodeBase> children)
|
||||
{
|
||||
|
|
|
@ -6,42 +6,42 @@ public class VarDeclaration : NonTerminatedSyntaxNode
|
|||
{
|
||||
public override NonTerminatorType Type => NonTerminatorType.VarDeclaration;
|
||||
|
||||
public bool IsRecursive { get; private init; }
|
||||
// public bool IsRecursive { get; private init; }
|
||||
|
||||
/// <summary>
|
||||
/// 声明的变量
|
||||
/// </summary>
|
||||
public (IdentifierList, TypeSyntaxNode) Variable => GetVariable();
|
||||
// /// <summary>
|
||||
// /// 声明的变量
|
||||
// /// </summary>
|
||||
// public (IdentifierList, TypeSyntaxNode) Variable => GetVariable();
|
||||
|
||||
private (IdentifierList, TypeSyntaxNode) GetVariable()
|
||||
{
|
||||
if (IsRecursive)
|
||||
{
|
||||
return (Children[2].Convert<IdentifierList>(), Children[4].Convert<TypeSyntaxNode>());
|
||||
}
|
||||
else
|
||||
{
|
||||
return (Children[0].Convert<IdentifierList>(), Children[2].Convert<TypeSyntaxNode>());
|
||||
}
|
||||
}
|
||||
// private (IdentifierList, TypeSyntaxNode) GetVariable()
|
||||
// {
|
||||
// if (IsRecursive)
|
||||
// {
|
||||
// return (Children[2].Convert<IdentifierList>(), Children[4].Convert<TypeSyntaxNode>());
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// return (Children[0].Convert<IdentifierList>(), Children[2].Convert<TypeSyntaxNode>());
|
||||
// }
|
||||
// }
|
||||
|
||||
public static VarDeclaration Create(List<SyntaxNodeBase> children)
|
||||
{
|
||||
bool isRecursive;
|
||||
/*bool isRecursive;
|
||||
|
||||
if (children.Count == 3)
|
||||
if (children.Count == 2)
|
||||
{
|
||||
isRecursive = false;
|
||||
}
|
||||
else if (children.Count == 5)
|
||||
else if (children.Count == 4)
|
||||
{
|
||||
isRecursive = true;
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
}*/
|
||||
|
||||
return new VarDeclaration { Children = children, IsRecursive = isRecursive };
|
||||
return new VarDeclaration {Children = children};
|
||||
}
|
||||
}
|
||||
|
|
|
@ -11,79 +11,79 @@ public class VarDeclarations : NonTerminatedSyntaxNode
|
|||
/// <summary>
|
||||
/// 声明的变量列表
|
||||
/// </summary>
|
||||
public IEnumerable<(IdentifierList, TypeSyntaxNode)> Variables => EnumerateVariables();
|
||||
// public IEnumerable<(IdentifierList, TypeSyntaxNode)> Variables => EnumerateVariables();
|
||||
|
||||
public static VarDeclarations Create(List<SyntaxNodeBase> children)
|
||||
{
|
||||
return new VarDeclarations { Children = children };
|
||||
}
|
||||
|
||||
private IEnumerable<(IdentifierList, TypeSyntaxNode)> EnumerateVariables()
|
||||
{
|
||||
if (Children.Count == 0)
|
||||
{
|
||||
yield break;
|
||||
}
|
||||
// private IEnumerable<(IdentifierList, TypeSyntaxNode)> EnumerateVariables()
|
||||
// {
|
||||
// if (Children.Count == 0)
|
||||
// {
|
||||
// yield break;
|
||||
// }
|
||||
//
|
||||
// VarDeclaration declaration = Children[1].Convert<VarDeclaration>();
|
||||
//
|
||||
// while (true)
|
||||
// {
|
||||
// yield return declaration.Variable;
|
||||
//
|
||||
// if (declaration.IsRecursive)
|
||||
// {
|
||||
// declaration = declaration.Children[0].Convert<VarDeclaration>();
|
||||
// }
|
||||
// else
|
||||
// {
|
||||
// break;
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
|
||||
VarDeclaration declaration = Children[1].Convert<VarDeclaration>();
|
||||
|
||||
while (true)
|
||||
{
|
||||
yield return declaration.Variable;
|
||||
|
||||
if (declaration.IsRecursive)
|
||||
{
|
||||
declaration = declaration.Children[0].Convert<VarDeclaration>();
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public override void GenerateCCode(CCodeBuilder builder)
|
||||
{
|
||||
foreach (var pair in Variables.Reverse())
|
||||
{
|
||||
//BasicType定义
|
||||
if (pair.Item2.Children.Count == 1)
|
||||
{
|
||||
//输出类型
|
||||
pair.Item2.GenerateCCode(builder);
|
||||
//输出idList
|
||||
pair.Item1.GenerateCCode(builder);
|
||||
builder.AddString(";");
|
||||
}
|
||||
//array定义
|
||||
else
|
||||
{
|
||||
//构造出C语言形式的数组下标定义
|
||||
string arrayPeriod = "";
|
||||
var ranges = pair.Item2.Children[2]
|
||||
.Convert<Period>().Ranges;
|
||||
PascalType pascalType = pair.Item2.Children[5].Convert<BasicType>().TryGetPascalType();
|
||||
|
||||
foreach (var range in ranges)
|
||||
{
|
||||
int low = int.Parse(range.Item1.LiteralValue);
|
||||
int high = int.Parse(range.Item2.LiteralValue);
|
||||
arrayPeriod = "[" + System.Convert.ToString(high-low+1) + "]" + arrayPeriod;
|
||||
pascalType = new PascalArrayType(pascalType, low, high); //嵌套地构造出多维数组
|
||||
}
|
||||
|
||||
//依次定义每一个符号
|
||||
foreach (var id in pair.Item1.Identifiers.Reverse())
|
||||
{
|
||||
pair.Item2.Children[5].GenerateCCode(builder);
|
||||
builder.AddString(" " + id.IdentifierName + arrayPeriod + ";");
|
||||
//写入符号表
|
||||
builder.SymbolTable.TryAddSymbol(new Symbol()
|
||||
{
|
||||
SymbolName = id.IdentifierName, SymbolType = pascalType, Reference = false
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
// public override void GenerateCCode(CCodeBuilder builder)
|
||||
// {
|
||||
// foreach (var pair in Variables.Reverse())
|
||||
// {
|
||||
// //BasicType定义
|
||||
// if (pair.Item2.Children.Count == 1)
|
||||
// {
|
||||
// //输出类型
|
||||
// pair.Item2.GenerateCCode(builder);
|
||||
// //输出idList
|
||||
// pair.Item1.GenerateCCode(builder);
|
||||
// builder.AddString(";");
|
||||
// }
|
||||
// //array定义
|
||||
// else
|
||||
// {
|
||||
// //构造出C语言形式的数组下标定义
|
||||
// string arrayPeriod = "";
|
||||
// var ranges = pair.Item2.Children[2]
|
||||
// .Convert<Period>().Ranges;
|
||||
// PascalType pascalType = pair.Item2.Children[5].Convert<BasicType>().TryGetPascalType();
|
||||
//
|
||||
// foreach (var range in ranges)
|
||||
// {
|
||||
// int low = int.Parse(range.Item1.LiteralValue);
|
||||
// int high = int.Parse(range.Item2.LiteralValue);
|
||||
// arrayPeriod = "[" + System.Convert.ToString(high-low+1) + "]" + arrayPeriod;
|
||||
// pascalType = new PascalArrayType(pascalType, low, high); //嵌套地构造出多维数组
|
||||
// }
|
||||
//
|
||||
// //依次定义每一个符号
|
||||
// foreach (var id in pair.Item1.Identifiers.Reverse())
|
||||
// {
|
||||
// pair.Item2.Children[5].GenerateCCode(builder);
|
||||
// builder.AddString(" " + id.IdentifierName + arrayPeriod + ";");
|
||||
// //写入符号表
|
||||
// builder.SymbolTable.TryAddSymbol(new Symbol()
|
||||
// {
|
||||
// SymbolName = id.IdentifierName, SymbolType = pascalType, Reference = false
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
// }
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user