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:
		
										
											
												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 NonTerminator(NonTerminatorType.IdentifierList), [
 | 
				
			||||||
                [
 | 
					                [
 | 
				
			||||||
 | 
					                    new Terminator(DelimiterType.Comma),
 | 
				
			||||||
                    Terminator.IdentifierTerminator,
 | 
					                    Terminator.IdentifierTerminator,
 | 
				
			||||||
 | 
					                    new NonTerminator(NonTerminatorType.IdentifierList),
 | 
				
			||||||
                ],
 | 
					                ],
 | 
				
			||||||
                [
 | 
					                [
 | 
				
			||||||
                    new NonTerminator(NonTerminatorType.IdentifierList),
 | 
					                    new Terminator(DelimiterType.Colon),
 | 
				
			||||||
                    new Terminator(DelimiterType.Comma),
 | 
					                    new NonTerminator(NonTerminatorType.Type)
 | 
				
			||||||
                    Terminator.IdentifierTerminator
 | 
					 | 
				
			||||||
                ]
 | 
					                ]
 | 
				
			||||||
            ]
 | 
					            ]
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
@@ -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.VarDeclaration), [
 | 
				
			||||||
                [
 | 
					                [
 | 
				
			||||||
                    new NonTerminator(NonTerminatorType.IdentifierList),
 | 
					                    Terminator.IdentifierTerminator,
 | 
				
			||||||
                    new Terminator(DelimiterType.Colon),
 | 
					                    new NonTerminator(NonTerminatorType.IdentifierList)
 | 
				
			||||||
                    new NonTerminator(NonTerminatorType.Type)
 | 
					 | 
				
			||||||
                ],
 | 
					                ],
 | 
				
			||||||
                [
 | 
					                [
 | 
				
			||||||
                    new NonTerminator(NonTerminatorType.VarDeclaration),
 | 
					                    new NonTerminator(NonTerminatorType.VarDeclaration),
 | 
				
			||||||
                    new Terminator(DelimiterType.Semicolon),
 | 
					                    new Terminator(DelimiterType.Semicolon),
 | 
				
			||||||
 | 
					                    Terminator.IdentifierTerminator,
 | 
				
			||||||
                    new NonTerminator(NonTerminatorType.IdentifierList),
 | 
					                    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.ValueParameter), [
 | 
				
			||||||
                [
 | 
					                [
 | 
				
			||||||
                    new NonTerminator(NonTerminatorType.IdentifierList),
 | 
					                    Terminator.IdentifierTerminator,
 | 
				
			||||||
                    new Terminator(DelimiterType.Colon),
 | 
					                    new NonTerminator(NonTerminatorType.IdentifierList)
 | 
				
			||||||
                    new NonTerminator(NonTerminatorType.BasicType)
 | 
					 | 
				
			||||||
                ]
 | 
					                ]
 | 
				
			||||||
            ]
 | 
					            ]
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -22,7 +22,7 @@ public class IdentifierList : NonTerminatedSyntaxNode
 | 
				
			|||||||
    {
 | 
					    {
 | 
				
			||||||
        bool isRecursive;
 | 
					        bool isRecursive;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (children.Count == 1)
 | 
					        if (children.Count == 2)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            isRecursive = false;
 | 
					            isRecursive = false;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -10,12 +10,12 @@ public class ValueParameter : NonTerminatedSyntaxNode
 | 
				
			|||||||
    /// <summary>
 | 
					    /// <summary>
 | 
				
			||||||
    /// 声明的变量列表
 | 
					    /// 声明的变量列表
 | 
				
			||||||
    /// </summary>
 | 
					    /// </summary>
 | 
				
			||||||
    public IdentifierList IdentifierList => Children[0].Convert<IdentifierList>();
 | 
					    // public IdentifierList IdentifierList => Children[1].Convert<IdentifierList>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// <summary>
 | 
					    /// <summary>
 | 
				
			||||||
    /// 声明的变量类型
 | 
					    /// 声明的变量类型
 | 
				
			||||||
    /// </summary>
 | 
					    /// </summary>
 | 
				
			||||||
    public BasicType BasicType => Children[2].Convert<BasicType>();
 | 
					    // public BasicType BasicType => Children[2].Convert<BasicType>();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public static ValueParameter Create(List<SyntaxNodeBase> children)
 | 
					    public static ValueParameter Create(List<SyntaxNodeBase> children)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -6,42 +6,42 @@ public class VarDeclaration : NonTerminatedSyntaxNode
 | 
				
			|||||||
{
 | 
					{
 | 
				
			||||||
    public override NonTerminatorType Type => NonTerminatorType.VarDeclaration;
 | 
					    public override NonTerminatorType Type => NonTerminatorType.VarDeclaration;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public bool IsRecursive { get; private init; }
 | 
					    // public bool IsRecursive { get; private init; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /// <summary>
 | 
					    // /// <summary>
 | 
				
			||||||
    /// 声明的变量
 | 
					    // /// 声明的变量
 | 
				
			||||||
    /// </summary>
 | 
					    // /// </summary>
 | 
				
			||||||
    public (IdentifierList, TypeSyntaxNode) Variable => GetVariable();
 | 
					    // public (IdentifierList, TypeSyntaxNode) Variable => GetVariable();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private (IdentifierList, TypeSyntaxNode) GetVariable()
 | 
					    // private (IdentifierList, TypeSyntaxNode) GetVariable()
 | 
				
			||||||
    {
 | 
					    // {
 | 
				
			||||||
        if (IsRecursive)
 | 
					    //     if (IsRecursive)
 | 
				
			||||||
        {
 | 
					    //     {
 | 
				
			||||||
            return (Children[2].Convert<IdentifierList>(), Children[4].Convert<TypeSyntaxNode>());
 | 
					    //         return (Children[2].Convert<IdentifierList>(), Children[4].Convert<TypeSyntaxNode>());
 | 
				
			||||||
        }
 | 
					    //     }
 | 
				
			||||||
        else
 | 
					    //     else
 | 
				
			||||||
        {
 | 
					    //     {
 | 
				
			||||||
            return (Children[0].Convert<IdentifierList>(), Children[2].Convert<TypeSyntaxNode>());
 | 
					    //         return (Children[0].Convert<IdentifierList>(), Children[2].Convert<TypeSyntaxNode>());
 | 
				
			||||||
        }
 | 
					    //     }
 | 
				
			||||||
    }
 | 
					    // }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public static VarDeclaration Create(List<SyntaxNodeBase> children)
 | 
					    public static VarDeclaration Create(List<SyntaxNodeBase> children)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        bool isRecursive;
 | 
					        /*bool isRecursive;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (children.Count == 3)
 | 
					        if (children.Count == 2)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            isRecursive = false;
 | 
					            isRecursive = false;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        else if (children.Count == 5)
 | 
					        else if (children.Count == 4)
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            isRecursive = true;
 | 
					            isRecursive = true;
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        else
 | 
					        else
 | 
				
			||||||
        {
 | 
					        {
 | 
				
			||||||
            throw new InvalidOperationException();
 | 
					            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>
 | 
				
			||||||
    /// 声明的变量列表
 | 
					    /// 声明的变量列表
 | 
				
			||||||
    /// </summary>
 | 
					    /// </summary>
 | 
				
			||||||
    public IEnumerable<(IdentifierList, TypeSyntaxNode)> Variables => EnumerateVariables();
 | 
					    // public IEnumerable<(IdentifierList, TypeSyntaxNode)> Variables => EnumerateVariables();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    public static VarDeclarations Create(List<SyntaxNodeBase> children)
 | 
					    public static VarDeclarations Create(List<SyntaxNodeBase> children)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        return new VarDeclarations { Children = children };
 | 
					        return new VarDeclarations { Children = children };
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private IEnumerable<(IdentifierList, TypeSyntaxNode)> EnumerateVariables()
 | 
					    // private IEnumerable<(IdentifierList, TypeSyntaxNode)> EnumerateVariables()
 | 
				
			||||||
    {
 | 
					    // {
 | 
				
			||||||
        if (Children.Count == 0)
 | 
					    //     if (Children.Count == 0)
 | 
				
			||||||
        {
 | 
					    //     {
 | 
				
			||||||
            yield break;
 | 
					    //         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>();
 | 
					    // public override void GenerateCCode(CCodeBuilder builder)
 | 
				
			||||||
 | 
					    // {
 | 
				
			||||||
        while (true)
 | 
					    //     foreach (var pair in Variables.Reverse())
 | 
				
			||||||
        {
 | 
					    //     {
 | 
				
			||||||
            yield return declaration.Variable;
 | 
					    //         //BasicType定义
 | 
				
			||||||
 | 
					    //         if (pair.Item2.Children.Count == 1)
 | 
				
			||||||
            if (declaration.IsRecursive)
 | 
					    //         {
 | 
				
			||||||
            {
 | 
					    //             //输出类型
 | 
				
			||||||
                declaration = declaration.Children[0].Convert<VarDeclaration>();
 | 
					    //             pair.Item2.GenerateCCode(builder);
 | 
				
			||||||
            }
 | 
					    //             //输出idList
 | 
				
			||||||
            else
 | 
					    //             pair.Item1.GenerateCCode(builder);
 | 
				
			||||||
            {
 | 
					    //             builder.AddString(";");
 | 
				
			||||||
                break;
 | 
					    //         }
 | 
				
			||||||
            }
 | 
					    //         //array定义
 | 
				
			||||||
        }
 | 
					    //         else
 | 
				
			||||||
    }
 | 
					    //         {
 | 
				
			||||||
 | 
					    //             //构造出C语言形式的数组下标定义
 | 
				
			||||||
    public override void GenerateCCode(CCodeBuilder builder)
 | 
					    //             string arrayPeriod = "";
 | 
				
			||||||
    {
 | 
					    //             var ranges = pair.Item2.Children[2]
 | 
				
			||||||
        foreach (var pair in Variables.Reverse())
 | 
					    //                 .Convert<Period>().Ranges;
 | 
				
			||||||
        {
 | 
					    //             PascalType pascalType = pair.Item2.Children[5].Convert<BasicType>().TryGetPascalType();
 | 
				
			||||||
            //BasicType定义
 | 
					    //
 | 
				
			||||||
            if (pair.Item2.Children.Count == 1)
 | 
					    //             foreach (var range in ranges)
 | 
				
			||||||
            {
 | 
					    //             {
 | 
				
			||||||
                //输出类型
 | 
					    //                 int low = int.Parse(range.Item1.LiteralValue);
 | 
				
			||||||
                pair.Item2.GenerateCCode(builder);
 | 
					    //                 int high = int.Parse(range.Item2.LiteralValue);
 | 
				
			||||||
                //输出idList
 | 
					    //                 arrayPeriod = "[" + System.Convert.ToString(high-low+1) + "]" + arrayPeriod;
 | 
				
			||||||
                pair.Item1.GenerateCCode(builder);
 | 
					    //                 pascalType = new PascalArrayType(pascalType, low, high); //嵌套地构造出多维数组
 | 
				
			||||||
                builder.AddString(";");
 | 
					    //             }
 | 
				
			||||||
            }
 | 
					    //
 | 
				
			||||||
            //array定义
 | 
					    //             //依次定义每一个符号
 | 
				
			||||||
            else
 | 
					    //             foreach (var id in pair.Item1.Identifiers.Reverse())
 | 
				
			||||||
            {
 | 
					    //             {
 | 
				
			||||||
                //构造出C语言形式的数组下标定义
 | 
					    //                 pair.Item2.Children[5].GenerateCCode(builder);
 | 
				
			||||||
                string arrayPeriod = "";
 | 
					    //                 builder.AddString(" " + id.IdentifierName + arrayPeriod + ";");
 | 
				
			||||||
                var ranges = pair.Item2.Children[2]
 | 
					    //                 //写入符号表
 | 
				
			||||||
                    .Convert<Period>().Ranges;
 | 
					    //                 builder.SymbolTable.TryAddSymbol(new Symbol()
 | 
				
			||||||
                PascalType pascalType = pair.Item2.Children[5].Convert<BasicType>().TryGetPascalType();
 | 
					    //                 {
 | 
				
			||||||
 | 
					    //                     SymbolName = id.IdentifierName, SymbolType = pascalType, Reference = false
 | 
				
			||||||
                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
 | 
					 | 
				
			||||||
                    });
 | 
					 | 
				
			||||||
                }
 | 
					 | 
				
			||||||
            }
 | 
					 | 
				
			||||||
        }
 | 
					 | 
				
			||||||
    }
 | 
					 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user