diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000000000000000000000000000000000000..751af81eeb8162d53fe9a81dab728db4b6a52bf3 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,19 @@ +language: csharp +solution: Compilers.sln +mono: + - latest + +install: + - sudo apt-get install nunit-console + - nuget restore Compilers.sln + +script: + - xbuild /p:TargetFrameworkVersion="v4.0" + - nunit-console -labels TestSimpleLexer/bin/Debug/TestSimpleLexer.dll + - nunit-console -labels TestLexer/bin/Debug/TestLexer.dll + - nunit-console -labels TestGeneratedLexer/bin/Debug/TestGeneratedLexer.dll + - nunit-console -labels TestDescentParser/bin/Debug/TestDescentParser.dll + - nunit-console -labels TestGeneratedParser/bin/Debug/TestGeneratedParser.dll + - nunit-console -labels TestASTParser/bin/Debug/TestASTParser.dll + - nunit-console -labels TestVisitors/bin/Debug/TestVisitors.dll + - nunit-console -labels TestCodeGenerator/bin/Debug/TestCodeGenerator.dll diff --git a/Compilers.sln b/Compilers.sln new file mode 100644 index 0000000000000000000000000000000000000000..16545cce4869245cbe1c90a4824a7127f5e97fcb --- /dev/null +++ b/Compilers.sln @@ -0,0 +1,118 @@ +п»ї +Microsoft Visual Studio Solution File, Format Version 12.00 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lexer", "Module1\Lexer.csproj", "{9715D879-F06E-4A65-ADA0-A4DF3619119D}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleLexer", "Module2\SimpleLangLexer\SimpleLexer.csproj", "{28C0284B-2F43-45D6-A77F-AB08F919717D}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleLexerDemo", "Module2\SimpleLexerDemo\SimpleLexerDemo.csproj", "{9EE96E14-A541-4EAD-AD34-2A9011ED12C1}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestSimpleLexer", "TestSimpleLexer\TestSimpleLexer.csproj", "{53767749-BCA6-4A3A-A4E6-D23519008305}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestLexer", "TestLexer\TestLexer.csproj", "{144BB4B2-AC18-4A38-9BEC-98BB22EB3382}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RecursiveDescentParser", "Module4\SimpleLangParser\RecursiveDescentParser.csproj", "{837BC9FE-DAA9-416E-BCCE-0235E5C38D4B}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RecursiveDescentParserDemo", "Module4\SimpleLangParserTest\RecursiveDescentParserDemo.csproj", "{5DD15A1A-760A-4A3F-BA1B-A23ACDF5CC87}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GeneratedParser", "Module5\GeneratedParser.csproj", "{0ED50D29-F6C3-44F3-BF13-BC1A433F6119}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ParserAST", "Module6\ParserAST.csproj", "{9815F576-CAB5-4D52-9963-F9B8277E340C}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ParserVistors", "Module7\ParserVistors.csproj", "{5F9F534D-DDE0-4B4A-9BC2-2AF8C9ED83FA}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ParserGenerator", "Module8\ParserGenerator.csproj", "{F8734914-3677-4A80-A87D-C274F52576FD}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GeneratedLexer", "Module3\GeneratedLexer.csproj", "{4DD13462-DCB4-4C3E-B777-7FEA90CEA9D1}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestGeneratedLexer", "TestGeneratedLexer\TestGeneratedLexer.csproj", "{24413F87-3568-4BDF-ADC8-760086CEE37A}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestDescentParser", "TestDescentParser\TestDescentParser.csproj", "{F6EABC41-B147-44EB-B6EF-13C3183C9961}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestGeneratedParser", "TestGeneratedParser\TestGeneratedParser.csproj", "{76382857-9D1F-4098-8376-48EF4A702445}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestASTParser", "TestASTParser\TestASTParser.csproj", "{71D0BFF1-8F45-450E-A78F-153391124637}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestVisitors", "TestVisitors\TestVisitors.csproj", "{365C36ED-B8E4-428A-9AF4-A3142E06FE1E}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TestCodeGenerator", "TestCodeGenerator\TestCodeGenerator.csproj", "{186EE1C7-7F34-4428-8A82-F63C8D4368E8}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {9715D879-F06E-4A65-ADA0-A4DF3619119D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9715D879-F06E-4A65-ADA0-A4DF3619119D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9715D879-F06E-4A65-ADA0-A4DF3619119D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9715D879-F06E-4A65-ADA0-A4DF3619119D}.Release|Any CPU.Build.0 = Release|Any CPU + {28C0284B-2F43-45D6-A77F-AB08F919717D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {28C0284B-2F43-45D6-A77F-AB08F919717D}.Debug|Any CPU.Build.0 = Debug|Any CPU + {28C0284B-2F43-45D6-A77F-AB08F919717D}.Release|Any CPU.ActiveCfg = Release|Any CPU + {28C0284B-2F43-45D6-A77F-AB08F919717D}.Release|Any CPU.Build.0 = Release|Any CPU + {9EE96E14-A541-4EAD-AD34-2A9011ED12C1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {9EE96E14-A541-4EAD-AD34-2A9011ED12C1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {9EE96E14-A541-4EAD-AD34-2A9011ED12C1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {9EE96E14-A541-4EAD-AD34-2A9011ED12C1}.Release|Any CPU.Build.0 = Release|Any CPU + {53767749-BCA6-4A3A-A4E6-D23519008305}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {53767749-BCA6-4A3A-A4E6-D23519008305}.Debug|Any CPU.Build.0 = Debug|Any CPU + {53767749-BCA6-4A3A-A4E6-D23519008305}.Release|Any CPU.ActiveCfg = Release|Any CPU + {53767749-BCA6-4A3A-A4E6-D23519008305}.Release|Any CPU.Build.0 = Release|Any CPU + {144BB4B2-AC18-4A38-9BEC-98BB22EB3382}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {144BB4B2-AC18-4A38-9BEC-98BB22EB3382}.Debug|Any CPU.Build.0 = Debug|Any CPU + {144BB4B2-AC18-4A38-9BEC-98BB22EB3382}.Release|Any CPU.ActiveCfg = Release|Any CPU + {144BB4B2-AC18-4A38-9BEC-98BB22EB3382}.Release|Any CPU.Build.0 = Release|Any CPU + {837BC9FE-DAA9-416E-BCCE-0235E5C38D4B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {837BC9FE-DAA9-416E-BCCE-0235E5C38D4B}.Debug|Any CPU.Build.0 = Debug|Any CPU + {837BC9FE-DAA9-416E-BCCE-0235E5C38D4B}.Release|Any CPU.ActiveCfg = Release|Any CPU + {837BC9FE-DAA9-416E-BCCE-0235E5C38D4B}.Release|Any CPU.Build.0 = Release|Any CPU + {5DD15A1A-760A-4A3F-BA1B-A23ACDF5CC87}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {5DD15A1A-760A-4A3F-BA1B-A23ACDF5CC87}.Debug|Any CPU.Build.0 = Debug|Any CPU + {5DD15A1A-760A-4A3F-BA1B-A23ACDF5CC87}.Release|Any CPU.ActiveCfg = Release|Any CPU + {5DD15A1A-760A-4A3F-BA1B-A23ACDF5CC87}.Release|Any CPU.Build.0 = Release|Any CPU + {0ED50D29-F6C3-44F3-BF13-BC1A433F6119}.Debug|Any CPU.ActiveCfg = Debug|x86 + {0ED50D29-F6C3-44F3-BF13-BC1A433F6119}.Debug|Any CPU.Build.0 = Debug|x86 + {0ED50D29-F6C3-44F3-BF13-BC1A433F6119}.Release|Any CPU.ActiveCfg = Release|x86 + {0ED50D29-F6C3-44F3-BF13-BC1A433F6119}.Release|Any CPU.Build.0 = Release|x86 + {9815F576-CAB5-4D52-9963-F9B8277E340C}.Debug|Any CPU.ActiveCfg = Debug|x86 + {9815F576-CAB5-4D52-9963-F9B8277E340C}.Debug|Any CPU.Build.0 = Debug|x86 + {9815F576-CAB5-4D52-9963-F9B8277E340C}.Release|Any CPU.ActiveCfg = Release|x86 + {9815F576-CAB5-4D52-9963-F9B8277E340C}.Release|Any CPU.Build.0 = Release|x86 + {5F9F534D-DDE0-4B4A-9BC2-2AF8C9ED83FA}.Debug|Any CPU.ActiveCfg = Debug|x86 + {5F9F534D-DDE0-4B4A-9BC2-2AF8C9ED83FA}.Debug|Any CPU.Build.0 = Debug|x86 + {5F9F534D-DDE0-4B4A-9BC2-2AF8C9ED83FA}.Release|Any CPU.ActiveCfg = Release|x86 + {5F9F534D-DDE0-4B4A-9BC2-2AF8C9ED83FA}.Release|Any CPU.Build.0 = Release|x86 + {F8734914-3677-4A80-A87D-C274F52576FD}.Debug|Any CPU.ActiveCfg = Debug|x86 + {F8734914-3677-4A80-A87D-C274F52576FD}.Debug|Any CPU.Build.0 = Debug|x86 + {F8734914-3677-4A80-A87D-C274F52576FD}.Release|Any CPU.ActiveCfg = Release|x86 + {F8734914-3677-4A80-A87D-C274F52576FD}.Release|Any CPU.Build.0 = Release|x86 + {4DD13462-DCB4-4C3E-B777-7FEA90CEA9D1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {4DD13462-DCB4-4C3E-B777-7FEA90CEA9D1}.Debug|Any CPU.Build.0 = Debug|Any CPU + {4DD13462-DCB4-4C3E-B777-7FEA90CEA9D1}.Release|Any CPU.ActiveCfg = Release|Any CPU + {4DD13462-DCB4-4C3E-B777-7FEA90CEA9D1}.Release|Any CPU.Build.0 = Release|Any CPU + {24413F87-3568-4BDF-ADC8-760086CEE37A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {24413F87-3568-4BDF-ADC8-760086CEE37A}.Debug|Any CPU.Build.0 = Debug|Any CPU + {24413F87-3568-4BDF-ADC8-760086CEE37A}.Release|Any CPU.ActiveCfg = Release|Any CPU + {24413F87-3568-4BDF-ADC8-760086CEE37A}.Release|Any CPU.Build.0 = Release|Any CPU + {F6EABC41-B147-44EB-B6EF-13C3183C9961}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {F6EABC41-B147-44EB-B6EF-13C3183C9961}.Debug|Any CPU.Build.0 = Debug|Any CPU + {F6EABC41-B147-44EB-B6EF-13C3183C9961}.Release|Any CPU.ActiveCfg = Release|Any CPU + {F6EABC41-B147-44EB-B6EF-13C3183C9961}.Release|Any CPU.Build.0 = Release|Any CPU + {76382857-9D1F-4098-8376-48EF4A702445}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {76382857-9D1F-4098-8376-48EF4A702445}.Debug|Any CPU.Build.0 = Debug|Any CPU + {76382857-9D1F-4098-8376-48EF4A702445}.Release|Any CPU.ActiveCfg = Release|Any CPU + {76382857-9D1F-4098-8376-48EF4A702445}.Release|Any CPU.Build.0 = Release|Any CPU + {71D0BFF1-8F45-450E-A78F-153391124637}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {71D0BFF1-8F45-450E-A78F-153391124637}.Debug|Any CPU.Build.0 = Debug|Any CPU + {71D0BFF1-8F45-450E-A78F-153391124637}.Release|Any CPU.ActiveCfg = Release|Any CPU + {71D0BFF1-8F45-450E-A78F-153391124637}.Release|Any CPU.Build.0 = Release|Any CPU + {365C36ED-B8E4-428A-9AF4-A3142E06FE1E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {365C36ED-B8E4-428A-9AF4-A3142E06FE1E}.Debug|Any CPU.Build.0 = Debug|Any CPU + {365C36ED-B8E4-428A-9AF4-A3142E06FE1E}.Release|Any CPU.ActiveCfg = Release|Any CPU + {365C36ED-B8E4-428A-9AF4-A3142E06FE1E}.Release|Any CPU.Build.0 = Release|Any CPU + {186EE1C7-7F34-4428-8A82-F63C8D4368E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {186EE1C7-7F34-4428-8A82-F63C8D4368E8}.Debug|Any CPU.Build.0 = Debug|Any CPU + {186EE1C7-7F34-4428-8A82-F63C8D4368E8}.Release|Any CPU.ActiveCfg = Release|Any CPU + {186EE1C7-7F34-4428-8A82-F63C8D4368E8}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection +EndGlobal diff --git a/Module1/Lexer.cs b/Module1/Lexer.cs index 8c201ea762698b29913b24e41f6386e0aaab38ad..4b1146367f9e97ec612e3335bcfc6f6d3de3132a 100644 --- a/Module1/Lexer.cs +++ b/Module1/Lexer.cs @@ -1,108 +1,335 @@ -public class LexerException : System.Exception +using System; +using System.Text; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; + +namespace Lexer { - public LexerException(string msg) - : base(msg) + + public class LexerException : System.Exception { + public LexerException(string msg) + : base(msg) + { + } + } -} + public class Lexer + { -public class Lexer -{ + protected int position; + protected char currentCh; // очередной считанный символ + protected int currentCharValue; // целое значение очередного считанного символа + protected System.IO.StringReader inputReader; + protected string inputString; - protected int position; - protected char currentCh; // очередной считанный символ - protected int currentCharValue; // целое значение очередного считанного символа - protected System.IO.StringReader inputReader; - protected string inputString; + public Lexer(string input) + { + inputReader = new System.IO.StringReader(input); + inputString = input; + } - public Lexer(string input) - { - inputReader = new System.IO.StringReader(input); - inputString = input; + public void Error() + { + System.Text.StringBuilder o = new System.Text.StringBuilder(); + o.Append(inputString + '\n'); + o.Append(new System.String(' ', position - 1) + "^\n"); + o.AppendFormat("Error in symbol {0}", currentCh); + throw new LexerException(o.ToString()); + } + + protected void NextCh() + { + this.currentCharValue = this.inputReader.Read(); + this.currentCh = (char) currentCharValue; + this.position += 1; + } + + public virtual bool Parse() + { + return true; + } } - public void Error() + public class IntLexer : Lexer { - System.Text.StringBuilder o = new System.Text.StringBuilder(); - o.Append(inputString + '\n'); - o.Append(new System.String(' ', position - 1) + "^\n"); - o.AppendFormat("Error in symbol {0}", currentCh); - throw new LexerException(o.ToString()); + + protected System.Text.StringBuilder intString; + public int parseResult = 0; + + public IntLexer(string input) + : base(input) + { + intString = new System.Text.StringBuilder(); + } + + public override bool Parse() + { + NextCh(); + if (currentCh == '+' || currentCh == '-') + { + NextCh(); + } + + if (char.IsDigit(currentCh)) + { + NextCh(); + } + else + { + Error(); + } + + while (char.IsDigit(currentCh)) + { + NextCh(); + } + + + if (currentCharValue != -1) + { + Error(); + } + + return true; + + } } + + public class IdentLexer : Lexer + { + private string parseResult; + protected StringBuilder builder; + + public string ParseResult + { + get { return parseResult; } + } + + public IdentLexer(string input) : base(input) + { + builder = new StringBuilder(); + } - protected void NextCh() + public override bool Parse() + { + throw new NotImplementedException(); + } + + } + + public class IntNoZeroLexer : IntLexer { - this.currentCharValue = this.inputReader.Read(); - this.currentCh = (char)currentCharValue; - this.position += 1; + public IntNoZeroLexer(string input) + : base(input) + { + } + + public override bool Parse() + { + throw new NotImplementedException(); + } } - public virtual void Parse() + public class LetterDigitLexer : Lexer { + protected StringBuilder builder; + protected string parseResult; + public string ParseResult + { + get { return parseResult; } + } + + public LetterDigitLexer(string input) + : base(input) + { + builder = new StringBuilder(); + } + + public override bool Parse() + { + throw new NotImplementedException(); + } + } -} -public class IntLexer : Lexer -{ + public class LetterListLexer : Lexer + { + protected List<char> parseResult; - protected System.Text.StringBuilder intString; + public List<char> ParseResult + { + get { return parseResult; } + } + + public LetterListLexer(string input) + : base(input) + { + parseResult = new List<char>(); + } - public IntLexer(string input) - : base(input) + public override bool Parse() + { + throw new NotImplementedException(); + } + } + + public class DigitListLexer : Lexer { - intString = new System.Text.StringBuilder(); + protected List<int> parseResult; + + public List<int> ParseResult + { + get { return parseResult; } + } + + public DigitListLexer(string input) + : base(input) + { + parseResult = new List<int>(); + } + + public override bool Parse() + { + throw new NotImplementedException(); + } } - public override void Parse() + public class LetterDigitGroupLexer : Lexer { - NextCh(); - if (currentCh == '+' || currentCh == '-') + protected StringBuilder builder; + protected string parseResult; + + public string ParseResult { - NextCh(); + get { return parseResult; } + } + + public LetterDigitGroupLexer(string input) + : base(input) + { + builder = new StringBuilder(); } - if (char.IsDigit(currentCh)) + public override bool Parse() { - NextCh(); + throw new NotImplementedException(); + } + + } + + public class DoubleLexer : Lexer + { + private StringBuilder builder; + private double parseResult; + + public double ParseResult + { + get { return parseResult; } + } - else + + public DoubleLexer(string input) + : base(input) { - Error(); + builder = new StringBuilder(); } - while (char.IsDigit(currentCh)) + public override bool Parse() { - NextCh(); + throw new NotImplementedException(); } + + } + public class StringLexer : Lexer + { + private StringBuilder builder; + private string parseResult; - if (currentCharValue != -1) // StringReader вернет -1 в конце строки + public string ParseResult { - Error(); + get { return parseResult; } + } - System.Console.WriteLine("Integer is recognized"); + public StringLexer(string input) + : base(input) + { + builder = new StringBuilder(); + } + public override bool Parse() + { + throw new NotImplementedException(); + } } -} + public class CommentLexer : Lexer + { + private StringBuilder builder; + private string parseResult; -public class Program -{ - public static void Main() + public string ParseResult + { + get { return parseResult; } + + } + + public CommentLexer(string input) + : base(input) + { + builder = new StringBuilder(); + } + + public override bool Parse() + { + throw new NotImplementedException(); + } + } + + public class IdentChainLexer : Lexer { - string input = "154216"; - Lexer L = new IntLexer(input); - try + private StringBuilder builder; + private List<string> parseResult; + + public List<string> ParseResult + { + get { return parseResult; } + + } + + public IdentChainLexer(string input) + : base(input) { - L.Parse(); + builder = new StringBuilder(); + parseResult = new List<string>(); } - catch (LexerException e) + + public override bool Parse() { - System.Console.WriteLine(e.Message); + throw new NotImplementedException(); } + } + public class Program + { + public static void Main() + { + string input = "154216"; + Lexer L = new IntLexer(input); + try + { + L.Parse(); + } + catch (LexerException e) + { + System.Console.WriteLine(e.Message); + } + + } } } \ No newline at end of file diff --git a/Module1/Lexer.csproj b/Module1/Lexer.csproj index b6c91240fb335a87ff2f606e4405f2bdc23e10a4..dad95bf8bff3bc2e350cc01e8d984f6e06820633 100644 --- a/Module1/Lexer.csproj +++ b/Module1/Lexer.csproj @@ -9,7 +9,7 @@ <AppDesignerFolder>Properties</AppDesignerFolder> <RootNamespace>Lexer</RootNamespace> <AssemblyName>Lexer</AssemblyName> - <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion> + <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> <FileAlignment>512</FileAlignment> <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> </PropertyGroup> @@ -33,13 +33,14 @@ <WarningLevel>4</WarningLevel> </PropertyGroup> <ItemGroup> + <Reference Include="Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> + <HintPath>C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.CSharp.dll</HintPath> + </Reference> <Reference Include="System" /> <Reference Include="System.Core" /> <Reference Include="System.Xml.Linq" /> <Reference Include="System.Data.DataSetExtensions" /> - <Reference Include="Microsoft.CSharp" /> <Reference Include="System.Data" /> - <Reference Include="System.Net.Http" /> <Reference Include="System.Xml" /> </ItemGroup> <ItemGroup> diff --git a/Module1/Lexer.sln b/Module1/Lexer.sln deleted file mode 100644 index 1b3acaa3165c4ec8a6b1d213e1b222d6e8e7d931..0000000000000000000000000000000000000000 --- a/Module1/Lexer.sln +++ /dev/null @@ -1,22 +0,0 @@ -п»ї -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 14 -VisualStudioVersion = 14.0.25420.1 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lexer", "Lexer.csproj", "{9715D879-F06E-4A65-ADA0-A4DF3619119D}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {9715D879-F06E-4A65-ADA0-A4DF3619119D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {9715D879-F06E-4A65-ADA0-A4DF3619119D}.Debug|Any CPU.Build.0 = Debug|Any CPU - {9715D879-F06E-4A65-ADA0-A4DF3619119D}.Release|Any CPU.ActiveCfg = Release|Any CPU - {9715D879-F06E-4A65-ADA0-A4DF3619119D}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection -EndGlobal diff --git a/Module2/SimpleLangLexer/SimpleLangLexer.sln b/Module2/SimpleLangLexer/SimpleLangLexer.sln deleted file mode 100644 index 95e7cf8f92303feb64ad061e89555526544442c8..0000000000000000000000000000000000000000 --- a/Module2/SimpleLangLexer/SimpleLangLexer.sln +++ /dev/null @@ -1,29 +0,0 @@ -п»ї -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2012 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleLangLexer", "SimpleLangLexer.csproj", "{28C0284B-2F43-45D6-A77F-AB08F919717D}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleLangLexerTest", "..\SimpleLangLexerTest\SimpleLangLexerTest.csproj", "{9EE96E14-A541-4EAD-AD34-2A9011ED12C1}" - ProjectSection(ProjectDependencies) = postProject - {28C0284B-2F43-45D6-A77F-AB08F919717D} = {28C0284B-2F43-45D6-A77F-AB08F919717D} - EndProjectSection -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {28C0284B-2F43-45D6-A77F-AB08F919717D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {28C0284B-2F43-45D6-A77F-AB08F919717D}.Debug|Any CPU.Build.0 = Debug|Any CPU - {28C0284B-2F43-45D6-A77F-AB08F919717D}.Release|Any CPU.ActiveCfg = Release|Any CPU - {28C0284B-2F43-45D6-A77F-AB08F919717D}.Release|Any CPU.Build.0 = Release|Any CPU - {9EE96E14-A541-4EAD-AD34-2A9011ED12C1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {9EE96E14-A541-4EAD-AD34-2A9011ED12C1}.Debug|Any CPU.Build.0 = Debug|Any CPU - {9EE96E14-A541-4EAD-AD34-2A9011ED12C1}.Release|Any CPU.ActiveCfg = Release|Any CPU - {9EE96E14-A541-4EAD-AD34-2A9011ED12C1}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection -EndGlobal diff --git a/Module2/SimpleLangLexer/SimpleLangLexer.cs b/Module2/SimpleLangLexer/SimpleLexer.cs similarity index 91% rename from Module2/SimpleLangLexer/SimpleLangLexer.cs rename to Module2/SimpleLangLexer/SimpleLexer.cs index 107066b5b50ee0cd939b8374955f7ff2c22211e3..7bd0af4e65a57a95a208e01d8242bf4f0d50d410 100644 --- a/Module2/SimpleLangLexer/SimpleLangLexer.cs +++ b/Module2/SimpleLangLexer/SimpleLexer.cs @@ -1,9 +1,9 @@ -п»їusing System; +п»їп»їusing System; using System.Collections; using System.Collections.Generic; using System.IO; -namespace SimpleLangLexer +namespace SimpleLexer { public class LexerException : System.Exception @@ -25,10 +25,39 @@ namespace SimpleLangLexer ASSIGN, BEGIN, END, - CYCLE + CYCLE, + COMMA, + PLUS, + MINUS, + MULT, + DIVISION, + MOD, + DIV, + AND, + OR, + NOT, + MULTASSIGN, + DIVASSIGN, + PLUSASSIGN, + MINUSASSIGN, + LT, //lesser + GT, //greater + LEQ, //less or equal + GEQ, //greater or equal + EQ, //equal + NEQ, //not equal + WHILE, + DO, + FOR, + TO, + IF, + THEN, + ELSE, + LEFT_BRACKET, + RIGHT_BRACKET, } - public class Lexer + public class Lexer { private int position; private char currentCh; // Текущий СЃРёРјРІРѕР» @@ -200,4 +229,4 @@ namespace SimpleLangLexer return result; } } -} +} \ No newline at end of file diff --git a/Module2/SimpleLangLexer/SimpleLangLexer.csproj b/Module2/SimpleLangLexer/SimpleLexer.csproj similarity index 84% rename from Module2/SimpleLangLexer/SimpleLangLexer.csproj rename to Module2/SimpleLangLexer/SimpleLexer.csproj index 6f0ed34f2475c6890800ea455ca6d342ad9ac0cb..4849a7dfbab8ed08e12e233b389dbac0746af41c 100644 --- a/Module2/SimpleLangLexer/SimpleLangLexer.csproj +++ b/Module2/SimpleLangLexer/SimpleLexer.csproj @@ -7,9 +7,9 @@ <ProjectGuid>{28C0284B-2F43-45D6-A77F-AB08F919717D}</ProjectGuid> <OutputType>Library</OutputType> <AppDesignerFolder>Properties</AppDesignerFolder> - <RootNamespace>SimpleLangLexer</RootNamespace> - <AssemblyName>SimpleLangLexer</AssemblyName> - <TargetFrameworkVersion>v4.0</TargetFrameworkVersion> + <RootNamespace>SimpleLexer</RootNamespace> + <AssemblyName>SimpleLexer</AssemblyName> + <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> <FileAlignment>512</FileAlignment> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> @@ -35,16 +35,18 @@ <StartupObject /> </PropertyGroup> <ItemGroup> + <Reference Include="Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> + <HintPath>C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.CSharp.dll</HintPath> + </Reference> <Reference Include="System" /> <Reference Include="System.Core" /> <Reference Include="System.Xml.Linq" /> <Reference Include="System.Data.DataSetExtensions" /> - <Reference Include="Microsoft.CSharp" /> <Reference Include="System.Data" /> <Reference Include="System.Xml" /> </ItemGroup> <ItemGroup> - <Compile Include="SimpleLangLexer.cs" /> + <Compile Include="SimpleLexer.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> diff --git a/Module2/SimpleLangLexerTest/Program.cs b/Module2/SimpleLexerDemo/Program.cs similarity index 97% rename from Module2/SimpleLangLexerTest/Program.cs rename to Module2/SimpleLexerDemo/Program.cs index 453e91ddd10f873fa621bbf83bfd3c90122c18fa..805615ad208442a882e0edcaf105a6a7886db41f 100644 --- a/Module2/SimpleLangLexerTest/Program.cs +++ b/Module2/SimpleLexerDemo/Program.cs @@ -3,7 +3,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; -using SimpleLangLexer; +using SimpleLexer; namespace SimpleLangLexerTest { diff --git a/Module2/SimpleLangLexerTest/Properties/AssemblyInfo.cs b/Module2/SimpleLexerDemo/Properties/AssemblyInfo.cs similarity index 95% rename from Module2/SimpleLangLexerTest/Properties/AssemblyInfo.cs rename to Module2/SimpleLexerDemo/Properties/AssemblyInfo.cs index 296801534d4ec4e2549499502b249a3c1ef83d24..1f5ccb2a6b42d3b8d824117eca528271741d1f1d 100644 --- a/Module2/SimpleLangLexerTest/Properties/AssemblyInfo.cs +++ b/Module2/SimpleLexerDemo/Properties/AssemblyInfo.cs @@ -5,11 +5,11 @@ using System.Runtime.InteropServices; // Управление общими сведениями Рѕ СЃР±РѕСЂРєРµ осуществляется СЃ помощью // набора атрибутов. Рзмените значения этих атрибутов, чтобы изменить сведения, // связанные СЃРѕ СЃР±РѕСЂРєРѕР№. -[assembly: AssemblyTitle("SimpleLangLexerTest")] +[assembly: AssemblyTitle("SimpleLexerDemo")] [assembly: AssemblyDescription("")] [assembly: AssemblyConfiguration("")] [assembly: AssemblyCompany("")] -[assembly: AssemblyProduct("SimpleLangLexerTest")] +[assembly: AssemblyProduct("SimpleLexerDemo")] [assembly: AssemblyCopyright("Copyright В© 2017")] [assembly: AssemblyTrademark("")] [assembly: AssemblyCulture("")] diff --git a/Module2/SimpleLangLexerTest/SimpleLangLexerTest.csproj b/Module2/SimpleLexerDemo/SimpleLexerDemo.csproj similarity index 83% rename from Module2/SimpleLangLexerTest/SimpleLangLexerTest.csproj rename to Module2/SimpleLexerDemo/SimpleLexerDemo.csproj index 2eb26ad3eb73dd0daa780d8db4b77c44bcdf05b0..4cfa0840b5b41abb10e4a0747e0eb3b4ebed797f 100644 --- a/Module2/SimpleLangLexerTest/SimpleLangLexerTest.csproj +++ b/Module2/SimpleLexerDemo/SimpleLexerDemo.csproj @@ -7,9 +7,9 @@ <ProjectGuid>{9EE96E14-A541-4EAD-AD34-2A9011ED12C1}</ProjectGuid> <OutputType>Exe</OutputType> <AppDesignerFolder>Properties</AppDesignerFolder> - <RootNamespace>SimpleLangLexerTest</RootNamespace> - <AssemblyName>SimpleLangLexerTest</AssemblyName> - <TargetFrameworkVersion>v4.0</TargetFrameworkVersion> + <RootNamespace>SimpleLexerDemo</RootNamespace> + <AssemblyName>SimpleLexerDemo</AssemblyName> + <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> <FileAlignment>512</FileAlignment> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> @@ -32,11 +32,13 @@ <WarningLevel>4</WarningLevel> </PropertyGroup> <ItemGroup> + <Reference Include="Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> + <HintPath>C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.CSharp.dll</HintPath> + </Reference> <Reference Include="System" /> <Reference Include="System.Core" /> <Reference Include="System.Xml.Linq" /> <Reference Include="System.Data.DataSetExtensions" /> - <Reference Include="Microsoft.CSharp" /> <Reference Include="System.Data" /> <Reference Include="System.Xml" /> </ItemGroup> @@ -45,9 +47,9 @@ <Compile Include="Properties\AssemblyInfo.cs" /> </ItemGroup> <ItemGroup> - <ProjectReference Include="..\SimpleLangLexer\SimpleLangLexer.csproj"> + <ProjectReference Include="..\SimpleLangLexer\SimpleLexer.csproj"> <Project>{28c0284b-2f43-45d6-a77f-ab08f919717d}</Project> - <Name>SimpleLangLexer</Name> + <Name>SimpleLexer</Name> </ProjectReference> </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> diff --git a/Module3/GPLexProject/LexProjects.sln b/Module3/GPLexProject/LexProjects.sln deleted file mode 100644 index c1a22729c0e9ace8602cd2b40ec6a56beee0e531..0000000000000000000000000000000000000000 --- a/Module3/GPLexProject/LexProjects.sln +++ /dev/null @@ -1,22 +0,0 @@ -п»ї -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2013 -VisualStudioVersion = 12.0.21005.1 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Lex1", "Lex1.csproj", "{4DD13462-DCB4-4C3E-B777-7FEA90CEA9D1}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {4DD13462-DCB4-4C3E-B777-7FEA90CEA9D1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {4DD13462-DCB4-4C3E-B777-7FEA90CEA9D1}.Debug|Any CPU.Build.0 = Debug|Any CPU - {4DD13462-DCB4-4C3E-B777-7FEA90CEA9D1}.Release|Any CPU.ActiveCfg = Release|Any CPU - {4DD13462-DCB4-4C3E-B777-7FEA90CEA9D1}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection -EndGlobal diff --git a/Module3/GPLexProject/mymain.cs b/Module3/GPLexProject/mymain.cs deleted file mode 100644 index e00ca8bcf2445f3cd6d5b802d561b70654980bd0..0000000000000000000000000000000000000000 --- a/Module3/GPLexProject/mymain.cs +++ /dev/null @@ -1,32 +0,0 @@ -using System; -using System.IO; -using SimpleScanner; -using ScannerHelper; - -namespace Main -{ - class mymain - { - static void Main(string[] args) - { - // Чтобы вещественные числа распознавались и отображались в формате 3.14 (а не 3,14 как в русской Culture) - System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); - - var fname = @"..\..\a.txt"; - Console.WriteLine(File.ReadAllText(fname)); - Console.WriteLine("-------------------------"); - - Scanner scanner = new Scanner(new FileStream(fname, FileMode.Open)); - - int tok = 0; - do { - tok = scanner.yylex(); - if (tok == (int)Tok.EOF) - break; - Console.WriteLine(scanner.TokToString((Tok)tok)); - } while (true); - - Console.ReadKey(); - } - } -} diff --git a/Module3/GPLexProject/Lex1.csproj b/Module3/GeneratedLexer.csproj similarity index 89% rename from Module3/GPLexProject/Lex1.csproj rename to Module3/GeneratedLexer.csproj index 208f5f9f6f9250b1b6922db2dbc033afa40ffbb0..dd458fdde7286290992ca5094f119d3ef2036132 100644 --- a/Module3/GPLexProject/Lex1.csproj +++ b/Module3/GeneratedLexer.csproj @@ -6,8 +6,10 @@ <ProjectGuid>{4DD13462-DCB4-4C3E-B777-7FEA90CEA9D1}</ProjectGuid> <OutputType>Exe</OutputType> <NoStandardLibraries>false</NoStandardLibraries> - <AssemblyName>sss</AssemblyName> - <RootNamespace>sss</RootNamespace> + <AssemblyName> + </AssemblyName> + <RootNamespace> + </RootNamespace> <RunPostBuildEvent>OnBuildSuccess</RunPostBuildEvent> <FileUpgradeFlags> </FileUpgradeFlags> @@ -15,7 +17,7 @@ </UpgradeBackupLocation> <OldToolsVersion>3.5</OldToolsVersion> <IsWebBootstrapper>true</IsWebBootstrapper> - <TargetFrameworkVersion>v2.0</TargetFrameworkVersion> + <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> <PublishUrl>http://localhost/sss/</PublishUrl> <Install>true</Install> <InstallFrom>Web</InstallFrom> @@ -30,6 +32,7 @@ <ApplicationVersion>1.0.0.%2a</ApplicationVersion> <UseApplicationTrust>false</UseApplicationTrust> <BootstrapperEnabled>true</BootstrapperEnabled> + <LangVersion>5</LangVersion> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <DebugSymbols>true</DebugSymbols> @@ -45,11 +48,15 @@ <DefineConstants>TRACE</DefineConstants> </PropertyGroup> <ItemGroup> + <Reference Include="Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> + <HintPath>C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.CSharp.dll</HintPath> + </Reference> <Reference Include="System" /> <Reference Include="System.Data" /> <Reference Include="System.Xml" /> </ItemGroup> <ItemGroup> + <Compile Include="LexerAddon.cs" /> <Compile Include="ScannerHelper.cs" /> <Compile Include="SimpleLex.cs" /> <Compile Include="mymain.cs" /> diff --git a/Module3/LexerAddon.cs b/Module3/LexerAddon.cs new file mode 100644 index 0000000000000000000000000000000000000000..c2467796d99e34b2906c362b66cf925504fe57e5 --- /dev/null +++ b/Module3/LexerAddon.cs @@ -0,0 +1,55 @@ +using System; +using System.IO; +using SimpleScanner; +using ScannerHelper; +using System.Collections.Generic; + +namespace GeneratedLexer +{ + + public class LexerAddon + { + public Scanner myScanner; + private byte[] inputText = new byte[255]; + + public int idCount = 0; + public int minIdLength = Int32.MaxValue; + public double avgIdLength = 0; + public int maxIdLength = 0; + public int sumInt = 0; + public double sumDouble = 0.0; + public List<string> idsInComment = new List<string>(); + + + public LexerAddon(string programText) + { + + using (StreamWriter writer = new StreamWriter(new MemoryStream(inputText))) + { + writer.Write(programText); + writer.Flush(); + } + + MemoryStream inputStream = new MemoryStream(inputText); + + myScanner = new Scanner(inputStream); + } + + public void Lex() + { + // Чтобы вещественные числа распознавались и отображались в формате 3.14 (а не 3,14 как в русской Culture) + System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); + + int tok = 0; + do { + tok = myScanner.yylex(); + + if (tok == (int)Tok.EOF) + { + break; + } + } while (true); + } + } +} + diff --git a/Module3/GPLexProject/ScannerHelper.cs b/Module3/ScannerHelper.cs similarity index 60% rename from Module3/GPLexProject/ScannerHelper.cs rename to Module3/ScannerHelper.cs index 37f58951a0255e30192d743dcb30bbf240b9d5ed..f56ce42317aee3e997677e0a7c630c59717626ee 100644 --- a/Module3/GPLexProject/ScannerHelper.cs +++ b/Module3/ScannerHelper.cs @@ -1,4 +1,4 @@ п»їnamespace ScannerHelper { - public enum Tok { EOF = 0, ID, INUM, RNUM, COLON, SEMICOLON, ASSIGN, BEGIN, END, CYCLE }; + public enum Tok { EOF = 0, ID, INUM, RNUM, COLON, SEMICOLON, ASSIGN, BEGIN, END, CYCLE, COMMENT, STRINGAP, LONGCOMMENT }; } \ No newline at end of file diff --git a/Module3/GPLexProject/ShiftReduceParserCode.cs b/Module3/ShiftReduceParserCode.cs similarity index 100% rename from Module3/GPLexProject/ShiftReduceParserCode.cs rename to Module3/ShiftReduceParserCode.cs diff --git a/Module3/GPLexProject/SimpleLex.cs b/Module3/SimpleLex.cs similarity index 94% rename from Module3/GPLexProject/SimpleLex.cs rename to Module3/SimpleLex.cs index a5496f598bc3bea178b2d4cc29636ede0c5eccbe..d8f9df81a5460e11bf2e732ea49905ce52e6f7fa 100644 --- a/Module3/GPLexProject/SimpleLex.cs +++ b/Module3/SimpleLex.cs @@ -1,10 +1,14 @@ // // This CSharp output file generated by Gardens Point LEX -// Version: 1.1.3.301 -// Machine: HUB -// DateTime: 21.09.2017 20:07:39 +// Gardens Point LEX (GPLEX) is Copyright (c) John Gough, QUT 2006-2014. +// Output produced by GPLEX is the property of the user. +// See accompanying file GPLEXcopyright.rtf. +// +// GPLEX Version: 1.2.2 +// Machine: MAINHOMEPC2 +// DateTime: 30.09.2018 17:57:38 // UserName: someone -// GPLEX input file <SimpleLex.lex> +// GPLEX input file <SimpleLex.lex - 30.09.2018 17:56:56> // GPLEX frame file <embedded resource> // // Option settings: noParser, minimize @@ -12,8 +16,8 @@ // // -// Experimental embedded frame -// Version 1.1.3 of 18-April-2010 +// Revised backup code +// Version 1.2.1 of 24-June-2013 // // #define BACKUP @@ -352,8 +356,7 @@ int NextState() { #if !NOFILES public Scanner(Stream file) { SetSource(file); // no unicode option - } - + } #endif // !NOFILES public Scanner() { } @@ -381,7 +384,7 @@ int NextState() { if (next < 0xDC00 || next > 0xDFFF) code = ScanBuff.UnicodeReplacementChar; else - code = (0x10000 + (code & 0x3FF << 10) + (next & 0x3FF)); + code = (0x10000 + ((code & 0x3FF) << 10) + (next & 0x3FF)); } #endif cCol++; @@ -434,9 +437,7 @@ int NextState() { GetCode(); } -#if !NOFILES // ================ LineBuffer Initialization =================== - /// <summary> /// Create and initialize a LineBuff buffer object for this scanner /// </summary> @@ -450,6 +451,7 @@ int NextState() { GetCode(); } +#if !NOFILES // =============== StreamBuffer Initialization ================== /// <summary> @@ -551,6 +553,12 @@ int NextState() { } } + /// <summary> + /// Discards all but the first "n" codepoints in the recognized pattern. + /// Resets the buffer position so that only n codepoints have been consumed; + /// yytext is also re-evaluated. + /// </summary> + /// <param name="n">The number of codepoints to consume</param> [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] void yyless(int n) { @@ -570,6 +578,10 @@ int NextState() { // but it does not seem possible to re-establish // the correct column counts except by going forward. // + /// <summary> + /// Removes the last "n" code points from the pattern. + /// </summary> + /// <param name="n">The number to remove</param> [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] void _yytrunc(int n) { yyless(yyleng - n); } @@ -582,18 +594,23 @@ int NextState() { // can't use (tokEPos - tokPos) because of the // possibility of surrogate pairs in the token. // + /// <summary> + /// The length of the pattern in codepoints (not the same as + /// string-length if the pattern contains any surrogate pairs). + /// </summary> [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] [SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "yyleng")] [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "yyleng")] public int yyleng { get { -#if BYTEMODE - return tokEPos - tokPos; -#else if (tokELin == tokLin) return tokECol - tokCol; - else { + else +#if BYTEMODE + return tokEPos - tokPos; +#else + { int ch; int count = 0; int save = buffer.Pos; @@ -602,7 +619,7 @@ int NextState() { ch = buffer.Read(); if (!char.IsHighSurrogate((char)ch)) count++; } while (buffer.Pos < tokEPos && ch != ScanBuff.EndOfFile); - buffer.Pos = save; + buffer.Pos = save; return count; } #endif // BYTEMODE @@ -627,64 +644,55 @@ int NextState() { // ============== The main tokenizer code ================= - int Scan() - { - for (; ; ) - { - int next; // next state to enter -#if BACKUP - Result rslt = Result.noMatch; -#endif // BACKUP + int Scan() { + for (; ; ) { + int next; // next state to enter #if LEFTANCHORS - for (;;) - { + for (;;) { // Discard characters that do not start any pattern. // Must check the left anchor condition after *every* GetCode! state = ((cCol == 0) ? anchorState[currentScOrd] : currentStart); - if ((next = NextState()) != goStart) - break; // LOOP EXIT HERE... + if ((next = NextState()) != goStart) break; // LOOP EXIT HERE... GetCode(); } #else // !LEFTANCHORS state = currentStart; - while ((next = NextState()) == goStart) + while ((next = NextState()) == goStart) { // At this point, the current character has no // transition from the current state. We discard // the "no-match" char. In traditional LEX such // characters are echoed to the console. GetCode(); + } #endif // LEFTANCHORS // At last, a valid transition ... MarkToken(); state = next; - GetCode(); - - while ((next = NextState()) > eofNum) // Exit for goStart AND for eofNum + GetCode(); #if BACKUP - if (state <= maxAccept && next > maxAccept) // need to prepare backup data - { - // ctx is an object. The fields may be - // mutated by the call to Recurse2. - // On return the data in ctx is the - // *latest* accept state that was found. - - rslt = Recurse2(ref ctx, next); - if (rslt == Result.noMatch) - RestoreStateAndPos(ref ctx); - break; + bool contextSaved = false; + while ((next = NextState()) > eofNum) { // Exit for goStart AND for eofNum + if (state <= maxAccept && next > maxAccept) { // need to prepare backup data + // Store data for the *latest* accept state that was found. + SaveStateAndPos( ref ctx ); + contextSaved = true; } - else + state = next; + GetCode(); + } + if (state > maxAccept && contextSaved) + RestoreStateAndPos( ref ctx ); +#else // BACKUP + while ((next = NextState()) > eofNum) { // Exit for goStart AND for eofNum + state = next; + GetCode(); + } #endif // BACKUP - { - state = next; - GetCode(); - } - if (state <= maxAccept) - { + if (state <= maxAccept) { MarkEnd(); #region ActionSwitch -#pragma warning disable 162 +#pragma warning disable 162, 1522 switch (state) { case eofNum: @@ -737,36 +745,14 @@ LexValueDouble = double.Parse(yytext); default: break; } -#pragma warning restore 162 +#pragma warning restore 162, 1522 #endregion } } } #if BACKUP - Result Recurse2(ref Context ctx, int next) - { - // Assert: at entry "state" is an accept state AND - // NextState(state, code) != goStart AND - // NextState(state, code) is not an accept state. - // - SaveStateAndPos(ref ctx); - state = next; - GetCode(); - - while ((next = NextState()) > eofNum) - { - if (state <= maxAccept && next > maxAccept) // need to update backup data - SaveStateAndPos(ref ctx); - state = next; - if (state == eofNum) return Result.accept; - GetCode(); - } - return (state <= maxAccept ? Result.accept : Result.noMatch); - } - - void SaveStateAndPos(ref Context ctx) - { + void SaveStateAndPos(ref Context ctx) { ctx.bPos = buffer.Pos; ctx.rPos = readPos; ctx.cCol = cCol; @@ -775,8 +761,7 @@ LexValueDouble = double.Parse(yytext); ctx.cChr = code; } - void RestoreStateAndPos(ref Context ctx) - { + void RestoreStateAndPos(ref Context ctx) { buffer.Pos = ctx.bPos; readPos = ctx.rPos; cCol = ctx.cCol; @@ -784,8 +769,7 @@ LexValueDouble = double.Parse(yytext); state = ctx.state; code = ctx.cChr; } - -#endif // BACKUP +#endif // BACKUP // ============= End of the tokenizer code ================ @@ -819,7 +803,7 @@ LexValueDouble = double.Parse(yytext); public void LexError() { - Console.WriteLine("({0},{1}): Неизвестный символ {2}", yyline, yycol, yytext); + Console.WriteLine("({0},{1}): ÍåèçâåñòГûé ñèìâîë {2}", yyline, yycol, yytext); } public string TokToString(Tok tok) @@ -886,6 +870,7 @@ public string TokToString(Tok tok) return new LineBuffer(source); } +#if (!NOFILES) public static ScanBuff GetBuffer(Stream source) { return new BuildBuffer(source); @@ -896,7 +881,8 @@ public string TokToString(Tok tok) { return new BuildBuffer(source, fallbackCodePage); } -#endif +#endif // !BYTEMODE +#endif // !NOFILES } #region Buffer classes @@ -1019,7 +1005,7 @@ public string TokToString(Tok tok) { ix = lstart = 0; } - for (; ; ) + while (ix < numLines) { int len = line[ix].Length + 1; if (pos < lstart + len) break; @@ -1077,7 +1063,8 @@ public string TokToString(Tok tok) { cPos = value; findIndex(cPos, out cLine, out curLineStart); - curLine = line[cLine]; + // cLine should be the *next* line after curLine. + curLine = (cLine < numLines ? line[cLine++] : ""); curLineEnd = curLineStart + curLine.Length; } } @@ -1085,7 +1072,7 @@ public string TokToString(Tok tok) public override string ToString() { return "LineBuffer"; } } - +#if (!NOFILES) // ============================================================== // ===== class BuildBuff : for unicode text files ======== // ============================================================== @@ -1326,12 +1313,13 @@ public string TokToString(Tok tok) } #endif // !BYTEMODE } +#endif // !NOFILES #endregion Buffer classes // ============================================================== // ============ class CodePageHandling ============= // ============================================================== - +#if (!NOFILES) public static class CodePageHandling { public static int GetCodePage(string option) @@ -1542,6 +1530,7 @@ public string TokToString(Tok tok) #endif // !BYTEMODE #endregion +#endif // !NOFILES // End of code copied from embedded resource diff --git a/Module3/GPLexProject/SimpleLex.lex b/Module3/SimpleLex.lex similarity index 100% rename from Module3/GPLexProject/SimpleLex.lex rename to Module3/SimpleLex.lex diff --git a/Module3/GPLexProject/a.txt b/Module3/a.txt similarity index 100% rename from Module3/GPLexProject/a.txt rename to Module3/a.txt diff --git a/Module3/GPLexProject/generateScanner.bat b/Module3/generateScanner.bat similarity index 100% rename from Module3/GPLexProject/generateScanner.bat rename to Module3/generateScanner.bat diff --git a/Module3/mymain.cs b/Module3/mymain.cs new file mode 100644 index 0000000000000000000000000000000000000000..9a33c597556eaecf3a6908bf931d847f7ed6c273 --- /dev/null +++ b/Module3/mymain.cs @@ -0,0 +1,56 @@ +using System; +using System.IO; +using SimpleScanner; +using ScannerHelper; + +namespace GeneratedLexer +{ + + class mymain + { + static void Main(string[] args) + { + int cnt_id = 0;//кол-во идентификаторов + int min_id_len = Int32.MaxValue, max_id_len = 0; //минимальная, максимальная длины идентификаторов + double avg_id_len = 0; //средняя длина идентификатора + + int sum_int = 0; //сумма всех целых + double sum_d = 0; //сумма всех вещественных + + // Чтобы вещественные числа распознавались и отображались в формате 3.14 (а не 3,14 как в русской Culture) + System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US"); + + var fname = @"..\..\a.txt"; + Console.WriteLine(File.ReadAllText(fname)); + Console.WriteLine("-------------------------"); + + Scanner scanner = new Scanner(new FileStream(fname, FileMode.Open)); + + int tok = 0; + do { + tok = scanner.yylex(); + + if (tok == (int)Tok.EOF) + { + Console.WriteLine(); + Console.WriteLine("number of id: {0:D}", cnt_id); + Console.WriteLine("average length of the id: {0:N}", avg_id_len / cnt_id); + Console.WriteLine("min length of the id: {0:D}", min_id_len); + Console.WriteLine("min length of the id: {0:D}", max_id_len); + + Console.WriteLine(); + Console.WriteLine("sum of int: {0:D}", sum_int); + Console.WriteLine("sum of double: {0:N}", sum_d); + + Console.WriteLine(); + + break; + } + + Console.WriteLine(scanner.TokToString((Tok)tok)); + } while (true); + + Console.ReadKey(); + } + } +} diff --git a/Module4/SimpleLangParser/SimpleLangParser.csproj b/Module4/SimpleLangParser/RecursiveDescentParser.csproj similarity index 83% rename from Module4/SimpleLangParser/SimpleLangParser.csproj rename to Module4/SimpleLangParser/RecursiveDescentParser.csproj index 4f878b7077c8a5d7a624fee96b27d2a814d37821..92d241ea68f78d0d90e96f365f9a55affc3fe255 100644 --- a/Module4/SimpleLangParser/SimpleLangParser.csproj +++ b/Module4/SimpleLangParser/RecursiveDescentParser.csproj @@ -9,7 +9,7 @@ <AppDesignerFolder>Properties</AppDesignerFolder> <RootNamespace>SimpleLangParser</RootNamespace> <AssemblyName>SimpleLangParser</AssemblyName> - <TargetFrameworkVersion>v4.0</TargetFrameworkVersion> + <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> <FileAlignment>512</FileAlignment> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> @@ -35,15 +35,13 @@ <StartupObject /> </PropertyGroup> <ItemGroup> - <Reference Include="SimpleLangLexer, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL"> - <SpecificVersion>False</SpecificVersion> - <HintPath>\\192.168.0.110\work\SFedU\РљСѓСЂСЃС‹2017\Методы_построения_компиляторов\Тема2\SimpleLangLexer\bin\Debug\SimpleLangLexer.dll</HintPath> + <Reference Include="Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> + <HintPath>C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.CSharp.dll</HintPath> </Reference> <Reference Include="System" /> <Reference Include="System.Core" /> <Reference Include="System.Xml.Linq" /> <Reference Include="System.Data.DataSetExtensions" /> - <Reference Include="Microsoft.CSharp" /> <Reference Include="System.Data" /> <Reference Include="System.Xml" /> </ItemGroup> @@ -51,6 +49,12 @@ <Compile Include="SimpleLangParser.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\..\Module2\SimpleLangLexer\SimpleLexer.csproj"> + <Project>{28c0284b-2f43-45d6-a77f-ab08f919717d}</Project> + <Name>SimpleLexer</Name> + </ProjectReference> + </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <!-- To modify your build process, add your task inside one of the targets below and uncomment it. Other similar extension points exist, see Microsoft.Common.targets. diff --git a/Module4/SimpleLangParser/SimpleLangParser.cs b/Module4/SimpleLangParser/SimpleLangParser.cs index 8103d34ddf532900bf96bbfe85f211662c4ae983..246db0b398caf6b3e0ecd83364d3832a912b8882 100644 --- a/Module4/SimpleLangParser/SimpleLangParser.cs +++ b/Module4/SimpleLangParser/SimpleLangParser.cs @@ -1,9 +1,7 @@ -п»їusing System; +п»їп»їusing System; using System.Collections.Generic; -using System.Linq; using System.Text; -using SimpleLangLexer; - +using SimpleLexer; namespace SimpleLangParser { public class ParserException : System.Exception @@ -17,9 +15,9 @@ namespace SimpleLangParser public class Parser { - private SimpleLangLexer.Lexer l; + private SimpleLexer.Lexer l; - public Parser(SimpleLangLexer.Lexer lexer) + public Parser(SimpleLexer.Lexer lexer) { l = lexer; } diff --git a/Module4/SimpleLangParser/SimpleLangParser.sln b/Module4/SimpleLangParser/SimpleLangParser.sln deleted file mode 100644 index 97ce736a52a4b0c849e4c1c8f479b0dc711f6437..0000000000000000000000000000000000000000 --- a/Module4/SimpleLangParser/SimpleLangParser.sln +++ /dev/null @@ -1,26 +0,0 @@ -п»ї -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2012 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleLangParser", "SimpleLangParser.csproj", "{837BC9FE-DAA9-416E-BCCE-0235E5C38D4B}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleLangParserTest", "..\SimpleLangParserTest\SimpleLangParserTest.csproj", "{5DD15A1A-760A-4A3F-BA1B-A23ACDF5CC87}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {837BC9FE-DAA9-416E-BCCE-0235E5C38D4B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {837BC9FE-DAA9-416E-BCCE-0235E5C38D4B}.Debug|Any CPU.Build.0 = Debug|Any CPU - {837BC9FE-DAA9-416E-BCCE-0235E5C38D4B}.Release|Any CPU.ActiveCfg = Release|Any CPU - {837BC9FE-DAA9-416E-BCCE-0235E5C38D4B}.Release|Any CPU.Build.0 = Release|Any CPU - {5DD15A1A-760A-4A3F-BA1B-A23ACDF5CC87}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {5DD15A1A-760A-4A3F-BA1B-A23ACDF5CC87}.Debug|Any CPU.Build.0 = Debug|Any CPU - {5DD15A1A-760A-4A3F-BA1B-A23ACDF5CC87}.Release|Any CPU.ActiveCfg = Release|Any CPU - {5DD15A1A-760A-4A3F-BA1B-A23ACDF5CC87}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection -EndGlobal diff --git a/Module4/SimpleLangParserTest/Program.cs b/Module4/SimpleLangParserTest/Program.cs index d2895a68b135ff15e3e0337275867eeaa002ad9a..1ceeeeab686b3e9a2054c82dfb367080305bbdf8 100644 --- a/Module4/SimpleLangParserTest/Program.cs +++ b/Module4/SimpleLangParserTest/Program.cs @@ -4,7 +4,7 @@ using System.Linq; using System.Text; using System.IO; using SimpleLangParser; -using SimpleLangLexer; +using SimpleLexer; namespace SimpleLangParserTest { @@ -16,6 +16,9 @@ namespace SimpleLangParserTest a := 2; cycle a begin + while 2 do begin + a:=2 +end; b := a; c := 234 end diff --git a/Module4/SimpleLangParserTest/SimpleLangParserTest.csproj b/Module4/SimpleLangParserTest/RecursiveDescentParserDemo.csproj similarity index 80% rename from Module4/SimpleLangParserTest/SimpleLangParserTest.csproj rename to Module4/SimpleLangParserTest/RecursiveDescentParserDemo.csproj index 92492224d435d2209e823351ec0f83c3f91de1d0..368bfbbeedf7fa5cabbaf42278344a42410b0dbc 100644 --- a/Module4/SimpleLangParserTest/SimpleLangParserTest.csproj +++ b/Module4/SimpleLangParserTest/RecursiveDescentParserDemo.csproj @@ -9,7 +9,7 @@ <AppDesignerFolder>Properties</AppDesignerFolder> <RootNamespace>SimpleLangParserTest</RootNamespace> <AssemblyName>SimpleLangParserTest</AssemblyName> - <TargetFrameworkVersion>v4.0</TargetFrameworkVersion> + <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> <FileAlignment>512</FileAlignment> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> @@ -32,15 +32,13 @@ <WarningLevel>4</WarningLevel> </PropertyGroup> <ItemGroup> - <Reference Include="SimpleLangLexer, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL"> - <SpecificVersion>False</SpecificVersion> - <HintPath>\\192.168.0.110\work\SFedU\РљСѓСЂСЃС‹2017\Методы_построения_компиляторов\Тема2\SimpleLangLexer\bin\Debug\SimpleLangLexer.dll</HintPath> + <Reference Include="Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> + <HintPath>C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.CSharp.dll</HintPath> </Reference> <Reference Include="System" /> <Reference Include="System.Core" /> <Reference Include="System.Xml.Linq" /> <Reference Include="System.Data.DataSetExtensions" /> - <Reference Include="Microsoft.CSharp" /> <Reference Include="System.Data" /> <Reference Include="System.Xml" /> </ItemGroup> @@ -49,9 +47,13 @@ <Compile Include="Properties\AssemblyInfo.cs" /> </ItemGroup> <ItemGroup> - <ProjectReference Include="..\SimpleLangParser\SimpleLangParser.csproj"> + <ProjectReference Include="..\..\Module2\SimpleLangLexer\SimpleLexer.csproj"> + <Project>{28c0284b-2f43-45d6-a77f-ab08f919717d}</Project> + <Name>SimpleLexer</Name> + </ProjectReference> + <ProjectReference Include="..\SimpleLangParser\RecursiveDescentParser.csproj"> <Project>{837bc9fe-daa9-416e-bcce-0235e5c38d4b}</Project> - <Name>SimpleLangParser</Name> + <Name>RecursiveDescentParser</Name> </ProjectReference> </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> diff --git a/Module6/SimpleLanguage1/SimpleLang1.csproj b/Module5/GeneratedParser.csproj similarity index 82% rename from Module6/SimpleLanguage1/SimpleLang1.csproj rename to Module5/GeneratedParser.csproj index 540156697d3853fd99623071b881edc5c5a26d1d..b678967302c3fb8b120da5e3dffd1e49cce207ed 100644 --- a/Module6/SimpleLanguage1/SimpleLang1.csproj +++ b/Module5/GeneratedParser.csproj @@ -5,12 +5,12 @@ <Platform Condition=" '$(Platform)' == '' ">x86</Platform> <ProductVersion>8.0.30703</ProductVersion> <SchemaVersion>2.0</SchemaVersion> - <ProjectGuid>{12B9D996-7B4A-4EE4-9AD8-2E24EAF3F574}</ProjectGuid> + <ProjectGuid>{0ED50D29-F6C3-44F3-BF13-BC1A433F6119}</ProjectGuid> <OutputType>Exe</OutputType> <AppDesignerFolder>Properties</AppDesignerFolder> <RootNamespace>SimpleLang</RootNamespace> <AssemblyName>SimpleLang</AssemblyName> - <TargetFrameworkVersion>v4.0</TargetFrameworkVersion> + <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> <TargetFrameworkProfile>Client</TargetFrameworkProfile> <FileAlignment>512</FileAlignment> </PropertyGroup> @@ -34,26 +34,30 @@ <WarningLevel>4</WarningLevel> </PropertyGroup> <ItemGroup> - <Reference Include="Newtonsoft.Json"> - <HintPath>..\..\..\..\Students\МирзоевДенис\Task6\SimpleLanguage1\Newtonsoft.Json.dll</HintPath> + <Reference Include="Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> + <HintPath>C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.CSharp.dll</HintPath> </Reference> <Reference Include="System" /> <Reference Include="System.Core" /> <Reference Include="System.Xml.Linq" /> <Reference Include="System.Data.DataSetExtensions" /> - <Reference Include="Microsoft.CSharp" /> <Reference Include="System.Data" /> <Reference Include="System.Xml" /> </ItemGroup> <ItemGroup> <Compile Include="Main.cs" /> <Compile Include="ParserHelper.cs" /> - <Compile Include="ProgramTree.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="ShiftReduceParserCode.cs" /> <Compile Include="SimpleLex.cs" /> <Compile Include="SimpleYacc.cs" /> </ItemGroup> + <ItemGroup> + <Content Include="a.txt" /> + <Content Include="generateParserScanner.bat" /> + <Content Include="SimpleLex.lex" /> + <Content Include="SimpleYacc.y" /> + </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <!-- To modify your build process, add your task inside one of the targets below and uncomment it. Other similar extension points exist, see Microsoft.Common.targets. diff --git a/Module5/SimpleLanguage0/Main.cs b/Module5/Main.cs similarity index 100% rename from Module5/SimpleLanguage0/Main.cs rename to Module5/Main.cs diff --git a/Module5/SimpleLanguage0/ParserHelper.cs b/Module5/ParserHelper.cs similarity index 100% rename from Module5/SimpleLanguage0/ParserHelper.cs rename to Module5/ParserHelper.cs diff --git a/Module5/SimpleLanguage0/Properties/AssemblyInfo.cs b/Module5/Properties/AssemblyInfo.cs similarity index 100% rename from Module5/SimpleLanguage0/Properties/AssemblyInfo.cs rename to Module5/Properties/AssemblyInfo.cs diff --git a/Module5/SimpleLanguage0/ShiftReduceParserCode.cs b/Module5/ShiftReduceParserCode.cs similarity index 100% rename from Module5/SimpleLanguage0/ShiftReduceParserCode.cs rename to Module5/ShiftReduceParserCode.cs diff --git a/Module5/SimpleLanguage0/SimpleLang.sln b/Module5/SimpleLanguage0/SimpleLang.sln deleted file mode 100644 index 0f586d1db23158b6305ee65f974934a75e2043ec..0000000000000000000000000000000000000000 --- a/Module5/SimpleLanguage0/SimpleLang.sln +++ /dev/null @@ -1,20 +0,0 @@ -п»ї -Microsoft Visual Studio Solution File, Format Version 11.00 -# Visual Studio 2010 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleLang", "SimpleLang.csproj", "{12B9D996-7B4A-4EE4-9AD8-2E24EAF3F574}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|x86 = Debug|x86 - Release|x86 = Release|x86 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {12B9D996-7B4A-4EE4-9AD8-2E24EAF3F574}.Debug|x86.ActiveCfg = Debug|x86 - {12B9D996-7B4A-4EE4-9AD8-2E24EAF3F574}.Debug|x86.Build.0 = Debug|x86 - {12B9D996-7B4A-4EE4-9AD8-2E24EAF3F574}.Release|x86.ActiveCfg = Release|x86 - {12B9D996-7B4A-4EE4-9AD8-2E24EAF3F574}.Release|x86.Build.0 = Release|x86 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection -EndGlobal diff --git a/Module5/SimpleLanguage0/SimpleLex.cs b/Module5/SimpleLex.cs similarity index 93% rename from Module5/SimpleLanguage0/SimpleLex.cs rename to Module5/SimpleLex.cs index 775228177d43bf7df61367aa7cfb3819738b6a92..af473da5e86ca6d0a581b5a1a65f18662ef3ba88 100644 --- a/Module5/SimpleLanguage0/SimpleLex.cs +++ b/Module5/SimpleLex.cs @@ -1,10 +1,14 @@ // // This CSharp output file generated by Gardens Point LEX -// Version: 1.1.3.301 -// Machine: HUB -// DateTime: 21.09.2017 20:15:13 +// Gardens Point LEX (GPLEX) is Copyright (c) John Gough, QUT 2006-2014. +// Output produced by GPLEX is the property of the user. +// See accompanying file GPLEXcopyright.rtf. +// +// GPLEX Version: 1.2.2 +// Machine: MAINHOMEPC2 +// DateTime: 30.09.2018 18:11:00 // UserName: someone -// GPLEX input file <SimpleLex.lex> +// GPLEX input file <SimpleLex.lex - 24.09.2018 23:47:02> // GPLEX frame file <embedded resource> // // Option settings: unicode, parser, minimize @@ -13,8 +17,8 @@ // // -// Experimental embedded frame -// Version 1.1.3 of 18-April-2010 +// Revised backup code +// Version 1.2.1 of 24-June-2013 // // #define BACKUP @@ -291,8 +295,7 @@ int NextState() { public Scanner(Stream file, string codepage) { SetSource(file, CodePageHandling.GetCodePage(codepage)); - } - + } #endif // !NOFILES public Scanner() { } @@ -320,7 +323,7 @@ int NextState() { if (next < 0xDC00 || next > 0xDFFF) code = ScanBuff.UnicodeReplacementChar; else - code = (0x10000 + (code & 0x3FF << 10) + (next & 0x3FF)); + code = (0x10000 + ((code & 0x3FF) << 10) + (next & 0x3FF)); } #endif cCol++; @@ -373,9 +376,7 @@ int NextState() { GetCode(); } -#if !NOFILES // ================ LineBuffer Initialization =================== - /// <summary> /// Create and initialize a LineBuff buffer object for this scanner /// </summary> @@ -389,6 +390,7 @@ int NextState() { GetCode(); } +#if !NOFILES // =============== StreamBuffer Initialization ================== /// <summary> @@ -490,6 +492,12 @@ int NextState() { } } + /// <summary> + /// Discards all but the first "n" codepoints in the recognized pattern. + /// Resets the buffer position so that only n codepoints have been consumed; + /// yytext is also re-evaluated. + /// </summary> + /// <param name="n">The number of codepoints to consume</param> [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] void yyless(int n) { @@ -509,6 +517,10 @@ int NextState() { // but it does not seem possible to re-establish // the correct column counts except by going forward. // + /// <summary> + /// Removes the last "n" code points from the pattern. + /// </summary> + /// <param name="n">The number to remove</param> [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] void _yytrunc(int n) { yyless(yyleng - n); } @@ -521,18 +533,23 @@ int NextState() { // can't use (tokEPos - tokPos) because of the // possibility of surrogate pairs in the token. // + /// <summary> + /// The length of the pattern in codepoints (not the same as + /// string-length if the pattern contains any surrogate pairs). + /// </summary> [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] [SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "yyleng")] [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "yyleng")] public int yyleng { get { -#if BYTEMODE - return tokEPos - tokPos; -#else if (tokELin == tokLin) return tokECol - tokCol; - else { + else +#if BYTEMODE + return tokEPos - tokPos; +#else + { int ch; int count = 0; int save = buffer.Pos; @@ -541,7 +558,7 @@ int NextState() { ch = buffer.Read(); if (!char.IsHighSurrogate((char)ch)) count++; } while (buffer.Pos < tokEPos && ch != ScanBuff.EndOfFile); - buffer.Pos = save; + buffer.Pos = save; return count; } #endif // BYTEMODE @@ -566,65 +583,56 @@ int NextState() { // ============== The main tokenizer code ================= - int Scan() - { + int Scan() { try { - for (; ; ) - { - int next; // next state to enter -#if BACKUP - Result rslt = Result.noMatch; -#endif // BACKUP + for (; ; ) { + int next; // next state to enter #if LEFTANCHORS - for (;;) - { + for (;;) { // Discard characters that do not start any pattern. // Must check the left anchor condition after *every* GetCode! state = ((cCol == 0) ? anchorState[currentScOrd] : currentStart); - if ((next = NextState()) != goStart) - break; // LOOP EXIT HERE... + if ((next = NextState()) != goStart) break; // LOOP EXIT HERE... GetCode(); } #else // !LEFTANCHORS state = currentStart; - while ((next = NextState()) == goStart) + while ((next = NextState()) == goStart) { // At this point, the current character has no // transition from the current state. We discard // the "no-match" char. In traditional LEX such // characters are echoed to the console. GetCode(); + } #endif // LEFTANCHORS // At last, a valid transition ... MarkToken(); state = next; - GetCode(); - - while ((next = NextState()) > eofNum) // Exit for goStart AND for eofNum + GetCode(); #if BACKUP - if (state <= maxAccept && next > maxAccept) // need to prepare backup data - { - // ctx is an object. The fields may be - // mutated by the call to Recurse2. - // On return the data in ctx is the - // *latest* accept state that was found. - - rslt = Recurse2(ref ctx, next); - if (rslt == Result.noMatch) - RestoreStateAndPos(ref ctx); - break; + bool contextSaved = false; + while ((next = NextState()) > eofNum) { // Exit for goStart AND for eofNum + if (state <= maxAccept && next > maxAccept) { // need to prepare backup data + // Store data for the *latest* accept state that was found. + SaveStateAndPos( ref ctx ); + contextSaved = true; } - else + state = next; + GetCode(); + } + if (state > maxAccept && contextSaved) + RestoreStateAndPos( ref ctx ); +#else // BACKUP + while ((next = NextState()) > eofNum) { // Exit for goStart AND for eofNum + state = next; + GetCode(); + } #endif // BACKUP - { - state = next; - GetCode(); - } - if (state <= maxAccept) - { + if (state <= maxAccept) { MarkEnd(); #region ActionSwitch -#pragma warning disable 162 +#pragma warning disable 162, 1522 switch (state) { case eofNum: @@ -655,7 +663,7 @@ return (int)Tokens.RNUM; default: break; } -#pragma warning restore 162 +#pragma warning restore 162, 1522 #endregion } } @@ -668,29 +676,7 @@ yylloc = new LexLocation(tokLin, tokCol, tokELin, tokECol); } #if BACKUP - Result Recurse2(ref Context ctx, int next) - { - // Assert: at entry "state" is an accept state AND - // NextState(state, code) != goStart AND - // NextState(state, code) is not an accept state. - // - SaveStateAndPos(ref ctx); - state = next; - GetCode(); - - while ((next = NextState()) > eofNum) - { - if (state <= maxAccept && next > maxAccept) // need to update backup data - SaveStateAndPos(ref ctx); - state = next; - if (state == eofNum) return Result.accept; - GetCode(); - } - return (state <= maxAccept ? Result.accept : Result.noMatch); - } - - void SaveStateAndPos(ref Context ctx) - { + void SaveStateAndPos(ref Context ctx) { ctx.bPos = buffer.Pos; ctx.rPos = readPos; ctx.cCol = cCol; @@ -699,8 +685,7 @@ yylloc = new LexLocation(tokLin, tokCol, tokELin, tokECol); ctx.cChr = code; } - void RestoreStateAndPos(ref Context ctx) - { + void RestoreStateAndPos(ref Context ctx) { buffer.Pos = ctx.bPos; readPos = ctx.rPos; cCol = ctx.cCol; @@ -708,8 +693,7 @@ yylloc = new LexLocation(tokLin, tokCol, tokELin, tokECol); state = ctx.state; code = ctx.cChr; } - -#endif // BACKUP +#endif // BACKUP // ============= End of the tokenizer code ================ @@ -741,16 +725,16 @@ yylloc = new LexLocation(tokLin, tokCol, tokELin, tokECol); #region UserCodeSection -public override void yyerror(string format, params object[] args) // обработка синтаксических ошибок +public override void yyerror(string format, params object[] args) // îáðà áîòêà ñèГГІГ ГЄГ±ГЁГ·ГҐГ±ГЄГЁГµ îøèáîê { var ww = args.Skip(1).Cast<string>().ToArray(); - string errorMsg = string.Format("({0},{1}): Встречено {2}, а ожидалось {3}", yyline, yycol, args[0], string.Join(" или ", ww)); + string errorMsg = string.Format("({0},{1}): Âñòðå÷åГГ® {2}, à îæèäà ëîñü {3}", yyline, yycol, args[0], string.Join(" èëè ", ww)); throw new SyntaxException(errorMsg); } public void LexError() { - string errorMsg = string.Format("({0},{1}): Неизвестный символ {2}", yyline, yycol, yytext); + string errorMsg = string.Format("({0},{1}): ÍåèçâåñòГûé ñèìâîë {2}", yyline, yycol, yytext); throw new LexException(errorMsg); } @@ -767,7 +751,7 @@ class ScannerHelper } public static int GetIDToken(string s) { - if (keywords.ContainsKey(s.ToLower())) // язык нечувствителен к регистру + if (keywords.ContainsKey(s.ToLower())) // ÿçûê Гå÷óâñòâèòåëåà ê ðåãèñòðó return keywords[s]; else return (int)Tokens.ID; @@ -823,6 +807,7 @@ class ScannerHelper return new LineBuffer(source); } +#if (!NOFILES) public static ScanBuff GetBuffer(Stream source) { return new BuildBuffer(source); @@ -833,7 +818,8 @@ class ScannerHelper { return new BuildBuffer(source, fallbackCodePage); } -#endif +#endif // !BYTEMODE +#endif // !NOFILES } #region Buffer classes @@ -956,7 +942,7 @@ class ScannerHelper { ix = lstart = 0; } - for (; ; ) + while (ix < numLines) { int len = line[ix].Length + 1; if (pos < lstart + len) break; @@ -1014,7 +1000,8 @@ class ScannerHelper { cPos = value; findIndex(cPos, out cLine, out curLineStart); - curLine = line[cLine]; + // cLine should be the *next* line after curLine. + curLine = (cLine < numLines ? line[cLine++] : ""); curLineEnd = curLineStart + curLine.Length; } } @@ -1022,7 +1009,7 @@ class ScannerHelper public override string ToString() { return "LineBuffer"; } } - +#if (!NOFILES) // ============================================================== // ===== class BuildBuff : for unicode text files ======== // ============================================================== @@ -1263,12 +1250,13 @@ class ScannerHelper } #endif // !BYTEMODE } +#endif // !NOFILES #endregion Buffer classes // ============================================================== // ============ class CodePageHandling ============= // ============================================================== - +#if (!NOFILES) public static class CodePageHandling { public static int GetCodePage(string option) @@ -1479,6 +1467,7 @@ class ScannerHelper #endif // !BYTEMODE #endregion +#endif // !NOFILES // End of code copied from embedded resource diff --git a/Module5/SimpleLanguage0/SimpleLex.lex b/Module5/SimpleLex.lex similarity index 100% rename from Module5/SimpleLanguage0/SimpleLex.lex rename to Module5/SimpleLex.lex diff --git a/Module5/SimpleLanguage0/SimpleYacc.cs b/Module5/SimpleYacc.cs similarity index 51% rename from Module5/SimpleLanguage0/SimpleYacc.cs rename to Module5/SimpleYacc.cs index d5c22890aa7bb450ca623a1dc4572229c762546b..0be791437a683b1c6147f0ca5e9e36fc68941431 100644 --- a/Module5/SimpleLanguage0/SimpleYacc.cs +++ b/Module5/SimpleYacc.cs @@ -1,43 +1,56 @@ // This code was generated by the Gardens Point Parser Generator -// Copyright (c) Wayne Kelly, QUT 2005-2010 +// Copyright (c) Wayne Kelly, John Gough, QUT 2005-2014 // (see accompanying GPPGcopyright.rtf) -// GPPG version 1.3.6 -// Machine: HUB -// DateTime: 21.09.2017 20:15:14 +// GPPG version 1.5.2 +// Machine: MAINHOMEPC2 +// DateTime: 30.09.2018 18:11:19 // UserName: someone -// Input file <SimpleYacc.y> +// Input file <SimpleYacc.y - 24.09.2018 23:47:02> // options: no-lines gplex using System; using System.Collections.Generic; +using System.CodeDom.Compiler; using System.Globalization; using System.Text; using QUT.Gppg; namespace SimpleParser { -public enum Tokens { - error=1,EOF=2,BEGIN=3,END=4,CYCLE=5,INUM=6, - RNUM=7,ID=8,ASSIGN=9,SEMICOLON=10}; +public enum Tokens {error=2,EOF=3,BEGIN=4,END=5,CYCLE=6, + INUM=7,RNUM=8,ID=9,ASSIGN=10,SEMICOLON=11}; // Abstract base class for GPLEX scanners +[GeneratedCodeAttribute( "Gardens Point Parser Generator", "1.5.2")] public abstract class ScanBase : AbstractScanner<int,LexLocation> { private LexLocation __yylloc = new LexLocation(); public override LexLocation yylloc { get { return __yylloc; } set { __yylloc = value; } } protected virtual bool yywrap() { return true; } } +// Utility class for encapsulating token information +[GeneratedCodeAttribute( "Gardens Point Parser Generator", "1.5.2")] +public class ScanObj { + public int token; + public int yylval; + public LexLocation yylloc; + public ScanObj( int t, int val, LexLocation loc ) { + this.token = t; this.yylval = val; this.yylloc = loc; + } +} + +[GeneratedCodeAttribute( "Gardens Point Parser Generator", "1.5.2")] public class Parser: ShiftReduceParser<int, LexLocation> { - // Verbatim content from SimpleYacc.y -// Эти объявления добавляются в класс GPPGParser, представляющий собой парсер, генерируемый системой gppg + // Verbatim content from SimpleYacc.y - 24.09.2018 23:47:02 +// ГќГІГЁ îáúÿâëåГГЁГї äîáà âëÿþòñÿ Гў êëà ññ GPPGParser, ïðåäñòà âëÿþùèé ñîáîé ïà ðñåð, ГЈГҐГåðèðóåìûé ñèñòåìîé gppg public Parser(AbstractScanner<int, LexLocation> scanner) : base(scanner) { } - // End verbatim content from SimpleYacc.y + // End verbatim content from SimpleYacc.y - 24.09.2018 23:47:02 #pragma warning disable 649 - private static Dictionary<int, string> aliasses; + private static Dictionary<int, string> aliases; #pragma warning restore 649 private static Rule[] rules = new Rule[14]; private static State[] states = new State[22]; @@ -46,42 +59,44 @@ public class Parser: ShiftReduceParser<int, LexLocation> "ident", "expr", }; static Parser() { - states[0] = new State(new int[]{3,4},new int[]{-1,1,-3,3}); - states[1] = new State(new int[]{2,2}); + states[0] = new State(new int[]{4,4},new int[]{-1,1,-3,3}); + states[1] = new State(new int[]{3,2}); states[2] = new State(-1); states[3] = new State(-2); - states[4] = new State(new int[]{8,14,3,4,5,18},new int[]{-4,5,-5,21,-6,9,-8,10,-3,16,-7,17}); - states[5] = new State(new int[]{4,6,10,7}); + states[4] = new State(new int[]{9,14,4,4,6,18},new int[]{-4,5,-5,21,-6,9,-8,10,-3,16,-7,17}); + states[5] = new State(new int[]{5,6,11,7}); states[6] = new State(-12); - states[7] = new State(new int[]{8,14,3,4,5,18},new int[]{-5,8,-6,9,-8,10,-3,16,-7,17}); + states[7] = new State(new int[]{9,14,4,4,6,18},new int[]{-5,8,-6,9,-8,10,-3,16,-7,17}); states[8] = new State(-4); states[9] = new State(-5); - states[10] = new State(new int[]{9,11}); - states[11] = new State(new int[]{8,14,6,15},new int[]{-9,12,-8,13}); + states[10] = new State(new int[]{10,11}); + states[11] = new State(new int[]{9,14,7,15},new int[]{-9,12,-8,13}); states[12] = new State(-9); states[13] = new State(-10); states[14] = new State(-8); states[15] = new State(-11); states[16] = new State(-6); states[17] = new State(-7); - states[18] = new State(new int[]{8,14,6,15},new int[]{-9,19,-8,13}); - states[19] = new State(new int[]{8,14,3,4,5,18},new int[]{-5,20,-6,9,-8,10,-3,16,-7,17}); + states[18] = new State(new int[]{9,14,7,15},new int[]{-9,19,-8,13}); + states[19] = new State(new int[]{9,14,4,4,6,18},new int[]{-5,20,-6,9,-8,10,-3,16,-7,17}); states[20] = new State(-13); states[21] = new State(-3); - rules[1] = new Rule(-2, new int[]{-1,2}); + for (int sNo = 0; sNo < states.Length; sNo++) states[sNo].number = sNo; + + rules[1] = new Rule(-2, new int[]{-1,3}); rules[2] = new Rule(-1, new int[]{-3}); rules[3] = new Rule(-4, new int[]{-5}); - rules[4] = new Rule(-4, new int[]{-4,10,-5}); + rules[4] = new Rule(-4, new int[]{-4,11,-5}); rules[5] = new Rule(-5, new int[]{-6}); rules[6] = new Rule(-5, new int[]{-3}); rules[7] = new Rule(-5, new int[]{-7}); - rules[8] = new Rule(-8, new int[]{8}); - rules[9] = new Rule(-6, new int[]{-8,9,-9}); + rules[8] = new Rule(-8, new int[]{9}); + rules[9] = new Rule(-6, new int[]{-8,10,-9}); rules[10] = new Rule(-9, new int[]{-8}); - rules[11] = new Rule(-9, new int[]{6}); - rules[12] = new Rule(-3, new int[]{3,-4,4}); - rules[13] = new Rule(-7, new int[]{5,-9,-5}); + rules[11] = new Rule(-9, new int[]{7}); + rules[12] = new Rule(-3, new int[]{4,-4,5}); + rules[13] = new Rule(-7, new int[]{6,-9,-5}); } protected override void Initialize() { @@ -93,15 +108,17 @@ public class Parser: ShiftReduceParser<int, LexLocation> protected override void DoAction(int action) { +#pragma warning disable 162, 1522 switch (action) { } +#pragma warning restore 162, 1522 } protected override string TerminalToString(int terminal) { - if (aliasses != null && aliasses.ContainsKey(terminal)) - return aliasses[terminal]; + if (aliases != null && aliases.ContainsKey(terminal)) + return aliases[terminal]; else if (((Tokens)terminal).ToString() != terminal.ToString(CultureInfo.InvariantCulture)) return ((Tokens)terminal).ToString(); else diff --git a/Module5/SimpleLanguage0/SimpleYacc.lst b/Module5/SimpleYacc.lst similarity index 100% rename from Module5/SimpleLanguage0/SimpleYacc.lst rename to Module5/SimpleYacc.lst diff --git a/Module5/SimpleLanguage0/SimpleYacc.y b/Module5/SimpleYacc.y similarity index 100% rename from Module5/SimpleLanguage0/SimpleYacc.y rename to Module5/SimpleYacc.y diff --git a/Module5/SimpleLanguage0/a.txt b/Module5/a.txt similarity index 100% rename from Module5/SimpleLanguage0/a.txt rename to Module5/a.txt diff --git a/Module5/SimpleLanguage0/generateParserScanner.bat b/Module5/generateParserScanner.bat similarity index 100% rename from Module5/SimpleLanguage0/generateParserScanner.bat rename to Module5/generateParserScanner.bat diff --git a/Module6/SimpleLanguage1/Main.cs b/Module6/Main.cs similarity index 100% rename from Module6/SimpleLanguage1/Main.cs rename to Module6/Main.cs diff --git a/Module6/SimpleLanguage1/Newtonsoft.Json.dll b/Module6/Newtonsoft.Json.dll similarity index 100% rename from Module6/SimpleLanguage1/Newtonsoft.Json.dll rename to Module6/Newtonsoft.Json.dll diff --git a/Module5/SimpleLanguage0/SimpleLang.csproj b/Module6/ParserAST.csproj similarity index 73% rename from Module5/SimpleLanguage0/SimpleLang.csproj rename to Module6/ParserAST.csproj index a45c1763a8da85524b966fe6106925d10337090e..f0ff2f67ab00c91b19f358d7c1bbfb68a8df0805 100644 --- a/Module5/SimpleLanguage0/SimpleLang.csproj +++ b/Module6/ParserAST.csproj @@ -5,12 +5,12 @@ <Platform Condition=" '$(Platform)' == '' ">x86</Platform> <ProductVersion>8.0.30703</ProductVersion> <SchemaVersion>2.0</SchemaVersion> - <ProjectGuid>{12B9D996-7B4A-4EE4-9AD8-2E24EAF3F574}</ProjectGuid> + <ProjectGuid>{9815F576-CAB5-4D52-9963-F9B8277E340C}</ProjectGuid> <OutputType>Exe</OutputType> <AppDesignerFolder>Properties</AppDesignerFolder> <RootNamespace>SimpleLang</RootNamespace> <AssemblyName>SimpleLang</AssemblyName> - <TargetFrameworkVersion>v4.0</TargetFrameworkVersion> + <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> <TargetFrameworkProfile>Client</TargetFrameworkProfile> <FileAlignment>512</FileAlignment> </PropertyGroup> @@ -34,22 +34,37 @@ <WarningLevel>4</WarningLevel> </PropertyGroup> <ItemGroup> + <Reference Include="Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> + <HintPath>C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.CSharp.dll</HintPath> + </Reference> + <Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed"> + <HintPath>..\packages\Newtonsoft.Json.11.0.2\lib\net35\Newtonsoft.Json.dll</HintPath> + <Private>True</Private> + </Reference> <Reference Include="System" /> <Reference Include="System.Core" /> <Reference Include="System.Xml.Linq" /> <Reference Include="System.Data.DataSetExtensions" /> - <Reference Include="Microsoft.CSharp" /> <Reference Include="System.Data" /> <Reference Include="System.Xml" /> </ItemGroup> <ItemGroup> <Compile Include="Main.cs" /> <Compile Include="ParserHelper.cs" /> + <Compile Include="ProgramTree.cs" /> <Compile Include="Properties\AssemblyInfo.cs" /> <Compile Include="ShiftReduceParserCode.cs" /> <Compile Include="SimpleLex.cs" /> <Compile Include="SimpleYacc.cs" /> </ItemGroup> + <ItemGroup> + <None Include="packages.config" /> + </ItemGroup> + <ItemGroup> + <Content Include="a.txt" /> + <Content Include="SimpleLex.lex" /> + <Content Include="SimpleYacc.y" /> + </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <!-- To modify your build process, add your task inside one of the targets below and uncomment it. Other similar extension points exist, see Microsoft.Common.targets. diff --git a/Module6/SimpleLanguage1/ParserHelper.cs b/Module6/ParserHelper.cs similarity index 100% rename from Module6/SimpleLanguage1/ParserHelper.cs rename to Module6/ParserHelper.cs diff --git a/Module6/SimpleLanguage1/ProgramTree.cs b/Module6/ProgramTree.cs similarity index 100% rename from Module6/SimpleLanguage1/ProgramTree.cs rename to Module6/ProgramTree.cs diff --git a/Module6/SimpleLanguage1/Properties/AssemblyInfo.cs b/Module6/Properties/AssemblyInfo.cs similarity index 100% rename from Module6/SimpleLanguage1/Properties/AssemblyInfo.cs rename to Module6/Properties/AssemblyInfo.cs diff --git a/Module6/SimpleLanguage1/ShiftReduceParserCode.cs b/Module6/ShiftReduceParserCode.cs similarity index 100% rename from Module6/SimpleLanguage1/ShiftReduceParserCode.cs rename to Module6/ShiftReduceParserCode.cs diff --git a/Module6/SimpleLanguage1/SimpleLang1.sln b/Module6/SimpleLanguage1/SimpleLang1.sln deleted file mode 100644 index adda773e0cc812398329b4af8b9d580e2e7b7c81..0000000000000000000000000000000000000000 --- a/Module6/SimpleLanguage1/SimpleLang1.sln +++ /dev/null @@ -1,22 +0,0 @@ -п»ї -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2013 -VisualStudioVersion = 12.0.21005.1 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleLang1", "SimpleLang1.csproj", "{12B9D996-7B4A-4EE4-9AD8-2E24EAF3F574}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|x86 = Debug|x86 - Release|x86 = Release|x86 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {12B9D996-7B4A-4EE4-9AD8-2E24EAF3F574}.Debug|x86.ActiveCfg = Debug|x86 - {12B9D996-7B4A-4EE4-9AD8-2E24EAF3F574}.Debug|x86.Build.0 = Debug|x86 - {12B9D996-7B4A-4EE4-9AD8-2E24EAF3F574}.Release|x86.ActiveCfg = Release|x86 - {12B9D996-7B4A-4EE4-9AD8-2E24EAF3F574}.Release|x86.Build.0 = Release|x86 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection -EndGlobal diff --git a/Module6/SimpleLanguage1/SimpleLex.cs b/Module6/SimpleLex.cs similarity index 93% rename from Module6/SimpleLanguage1/SimpleLex.cs rename to Module6/SimpleLex.cs index ceaf75d426e9155e3dbb08f1d852e51f527414fe..c518c8b3443fd6ed15300f8e5f3eea6733bd0974 100644 --- a/Module6/SimpleLanguage1/SimpleLex.cs +++ b/Module6/SimpleLex.cs @@ -1,10 +1,14 @@ // // This CSharp output file generated by Gardens Point LEX -// Version: 1.1.3.301 -// Machine: HUB -// DateTime: 21.09.2017 20:37:24 +// Gardens Point LEX (GPLEX) is Copyright (c) John Gough, QUT 2006-2014. +// Output produced by GPLEX is the property of the user. +// See accompanying file GPLEXcopyright.rtf. +// +// GPLEX Version: 1.2.2 +// Machine: MAINHOMEPC2 +// DateTime: 30.09.2018 18:14:30 // UserName: someone -// GPLEX input file <SimpleLex.lex> +// GPLEX input file <SimpleLex.lex - 24.09.2018 23:47:03> // GPLEX frame file <embedded resource> // // Option settings: unicode, parser, minimize @@ -13,8 +17,8 @@ // // -// Experimental embedded frame -// Version 1.1.3 of 18-April-2010 +// Revised backup code +// Version 1.2.1 of 24-June-2013 // // #define BACKUP @@ -291,8 +295,7 @@ int NextState() { public Scanner(Stream file, string codepage) { SetSource(file, CodePageHandling.GetCodePage(codepage)); - } - + } #endif // !NOFILES public Scanner() { } @@ -320,7 +323,7 @@ int NextState() { if (next < 0xDC00 || next > 0xDFFF) code = ScanBuff.UnicodeReplacementChar; else - code = (0x10000 + (code & 0x3FF << 10) + (next & 0x3FF)); + code = (0x10000 + ((code & 0x3FF) << 10) + (next & 0x3FF)); } #endif cCol++; @@ -373,9 +376,7 @@ int NextState() { GetCode(); } -#if !NOFILES // ================ LineBuffer Initialization =================== - /// <summary> /// Create and initialize a LineBuff buffer object for this scanner /// </summary> @@ -389,6 +390,7 @@ int NextState() { GetCode(); } +#if !NOFILES // =============== StreamBuffer Initialization ================== /// <summary> @@ -490,6 +492,12 @@ int NextState() { } } + /// <summary> + /// Discards all but the first "n" codepoints in the recognized pattern. + /// Resets the buffer position so that only n codepoints have been consumed; + /// yytext is also re-evaluated. + /// </summary> + /// <param name="n">The number of codepoints to consume</param> [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] void yyless(int n) { @@ -509,6 +517,10 @@ int NextState() { // but it does not seem possible to re-establish // the correct column counts except by going forward. // + /// <summary> + /// Removes the last "n" code points from the pattern. + /// </summary> + /// <param name="n">The number to remove</param> [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] void _yytrunc(int n) { yyless(yyleng - n); } @@ -521,18 +533,23 @@ int NextState() { // can't use (tokEPos - tokPos) because of the // possibility of surrogate pairs in the token. // + /// <summary> + /// The length of the pattern in codepoints (not the same as + /// string-length if the pattern contains any surrogate pairs). + /// </summary> [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] [SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "yyleng")] [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "yyleng")] public int yyleng { get { -#if BYTEMODE - return tokEPos - tokPos; -#else if (tokELin == tokLin) return tokECol - tokCol; - else { + else +#if BYTEMODE + return tokEPos - tokPos; +#else + { int ch; int count = 0; int save = buffer.Pos; @@ -541,7 +558,7 @@ int NextState() { ch = buffer.Read(); if (!char.IsHighSurrogate((char)ch)) count++; } while (buffer.Pos < tokEPos && ch != ScanBuff.EndOfFile); - buffer.Pos = save; + buffer.Pos = save; return count; } #endif // BYTEMODE @@ -566,65 +583,56 @@ int NextState() { // ============== The main tokenizer code ================= - int Scan() - { + int Scan() { try { - for (; ; ) - { - int next; // next state to enter -#if BACKUP - Result rslt = Result.noMatch; -#endif // BACKUP + for (; ; ) { + int next; // next state to enter #if LEFTANCHORS - for (;;) - { + for (;;) { // Discard characters that do not start any pattern. // Must check the left anchor condition after *every* GetCode! state = ((cCol == 0) ? anchorState[currentScOrd] : currentStart); - if ((next = NextState()) != goStart) - break; // LOOP EXIT HERE... + if ((next = NextState()) != goStart) break; // LOOP EXIT HERE... GetCode(); } #else // !LEFTANCHORS state = currentStart; - while ((next = NextState()) == goStart) + while ((next = NextState()) == goStart) { // At this point, the current character has no // transition from the current state. We discard // the "no-match" char. In traditional LEX such // characters are echoed to the console. GetCode(); + } #endif // LEFTANCHORS // At last, a valid transition ... MarkToken(); state = next; - GetCode(); - - while ((next = NextState()) > eofNum) // Exit for goStart AND for eofNum + GetCode(); #if BACKUP - if (state <= maxAccept && next > maxAccept) // need to prepare backup data - { - // ctx is an object. The fields may be - // mutated by the call to Recurse2. - // On return the data in ctx is the - // *latest* accept state that was found. - - rslt = Recurse2(ref ctx, next); - if (rslt == Result.noMatch) - RestoreStateAndPos(ref ctx); - break; + bool contextSaved = false; + while ((next = NextState()) > eofNum) { // Exit for goStart AND for eofNum + if (state <= maxAccept && next > maxAccept) { // need to prepare backup data + // Store data for the *latest* accept state that was found. + SaveStateAndPos( ref ctx ); + contextSaved = true; } - else + state = next; + GetCode(); + } + if (state > maxAccept && contextSaved) + RestoreStateAndPos( ref ctx ); +#else // BACKUP + while ((next = NextState()) > eofNum) { // Exit for goStart AND for eofNum + state = next; + GetCode(); + } #endif // BACKUP - { - state = next; - GetCode(); - } - if (state <= maxAccept) - { + if (state <= maxAccept) { MarkEnd(); #region ActionSwitch -#pragma warning disable 162 +#pragma warning disable 162, 1522 switch (state) { case eofNum: @@ -658,7 +666,7 @@ yylval.dVal = double.Parse(yytext); default: break; } -#pragma warning restore 162 +#pragma warning restore 162, 1522 #endregion } } @@ -671,29 +679,7 @@ yylloc = new LexLocation(tokLin, tokCol, tokELin, tokECol); } #if BACKUP - Result Recurse2(ref Context ctx, int next) - { - // Assert: at entry "state" is an accept state AND - // NextState(state, code) != goStart AND - // NextState(state, code) is not an accept state. - // - SaveStateAndPos(ref ctx); - state = next; - GetCode(); - - while ((next = NextState()) > eofNum) - { - if (state <= maxAccept && next > maxAccept) // need to update backup data - SaveStateAndPos(ref ctx); - state = next; - if (state == eofNum) return Result.accept; - GetCode(); - } - return (state <= maxAccept ? Result.accept : Result.noMatch); - } - - void SaveStateAndPos(ref Context ctx) - { + void SaveStateAndPos(ref Context ctx) { ctx.bPos = buffer.Pos; ctx.rPos = readPos; ctx.cCol = cCol; @@ -702,8 +688,7 @@ yylloc = new LexLocation(tokLin, tokCol, tokELin, tokECol); ctx.cChr = code; } - void RestoreStateAndPos(ref Context ctx) - { + void RestoreStateAndPos(ref Context ctx) { buffer.Pos = ctx.bPos; readPos = ctx.rPos; cCol = ctx.cCol; @@ -711,8 +696,7 @@ yylloc = new LexLocation(tokLin, tokCol, tokELin, tokECol); state = ctx.state; code = ctx.cChr; } - -#endif // BACKUP +#endif // BACKUP // ============= End of the tokenizer code ================ @@ -744,16 +728,16 @@ yylloc = new LexLocation(tokLin, tokCol, tokELin, tokECol); #region UserCodeSection -public override void yyerror(string format, params object[] args) // обработка синтаксических ошибок +public override void yyerror(string format, params object[] args) // îáðà áîòêà ñèГГІГ ГЄГ±ГЁГ·ГҐГ±ГЄГЁГµ îøèáîê { var ww = args.Skip(1).Cast<string>().ToArray(); - string errorMsg = string.Format("({0},{1}): Встречено {2}, а ожидалось {3}", yyline, yycol, args[0], string.Join(" или ", ww)); + string errorMsg = string.Format("({0},{1}): Âñòðå÷åГГ® {2}, à îæèäà ëîñü {3}", yyline, yycol, args[0], string.Join(" èëè ", ww)); throw new SyntaxException(errorMsg); } public void LexError() { - string errorMsg = string.Format("({0},{1}): Неизвестный символ {2}", yyline, yycol, yytext); + string errorMsg = string.Format("({0},{1}): ÍåèçâåñòГûé ñèìâîë {2}", yyline, yycol, yytext); throw new LexException(errorMsg); } @@ -827,6 +811,7 @@ class ScannerHelper return new LineBuffer(source); } +#if (!NOFILES) public static ScanBuff GetBuffer(Stream source) { return new BuildBuffer(source); @@ -837,7 +822,8 @@ class ScannerHelper { return new BuildBuffer(source, fallbackCodePage); } -#endif +#endif // !BYTEMODE +#endif // !NOFILES } #region Buffer classes @@ -960,7 +946,7 @@ class ScannerHelper { ix = lstart = 0; } - for (; ; ) + while (ix < numLines) { int len = line[ix].Length + 1; if (pos < lstart + len) break; @@ -1018,7 +1004,8 @@ class ScannerHelper { cPos = value; findIndex(cPos, out cLine, out curLineStart); - curLine = line[cLine]; + // cLine should be the *next* line after curLine. + curLine = (cLine < numLines ? line[cLine++] : ""); curLineEnd = curLineStart + curLine.Length; } } @@ -1026,7 +1013,7 @@ class ScannerHelper public override string ToString() { return "LineBuffer"; } } - +#if (!NOFILES) // ============================================================== // ===== class BuildBuff : for unicode text files ======== // ============================================================== @@ -1267,12 +1254,13 @@ class ScannerHelper } #endif // !BYTEMODE } +#endif // !NOFILES #endregion Buffer classes // ============================================================== // ============ class CodePageHandling ============= // ============================================================== - +#if (!NOFILES) public static class CodePageHandling { public static int GetCodePage(string option) @@ -1483,6 +1471,7 @@ class ScannerHelper #endif // !BYTEMODE #endregion +#endif // !NOFILES // End of code copied from embedded resource diff --git a/Module6/SimpleLanguage1/SimpleLex.lex b/Module6/SimpleLex.lex similarity index 100% rename from Module6/SimpleLanguage1/SimpleLex.lex rename to Module6/SimpleLex.lex diff --git a/Module6/SimpleLanguage1/SimpleYacc.cs b/Module6/SimpleYacc.cs similarity index 65% rename from Module6/SimpleLanguage1/SimpleYacc.cs rename to Module6/SimpleYacc.cs index 5666e24cbec642055dd97950339289be9ed18809..b13b1e9abfb58162aa20d280d3fec11345fdc298 100644 --- a/Module6/SimpleLanguage1/SimpleYacc.cs +++ b/Module6/SimpleYacc.cs @@ -1,17 +1,18 @@ // This code was generated by the Gardens Point Parser Generator -// Copyright (c) Wayne Kelly, QUT 2005-2010 +// Copyright (c) Wayne Kelly, John Gough, QUT 2005-2014 // (see accompanying GPPGcopyright.rtf) -// GPPG version 1.3.6 -// Machine: HUB -// DateTime: 21.09.2017 20:37:24 +// GPPG version 1.5.2 +// Machine: MAINHOMEPC2 +// DateTime: 30.09.2018 18:14:41 // UserName: someone -// Input file <SimpleYacc.y> +// Input file <SimpleYacc.y - 24.09.2018 23:47:03> // options: no-lines gplex using System; using System.Collections.Generic; +using System.CodeDom.Compiler; using System.Globalization; using System.Text; using QUT.Gppg; @@ -19,9 +20,8 @@ using ProgramTree; namespace SimpleParser { -public enum Tokens { - error=1,EOF=2,BEGIN=3,END=4,CYCLE=5,ASSIGN=6, - SEMICOLON=7,INUM=8,RNUM=9,ID=10}; +public enum Tokens {error=2,EOF=3,BEGIN=4,END=5,CYCLE=6, + ASSIGN=7,SEMICOLON=8,INUM=9,RNUM=10,ID=11}; public struct ValueType { @@ -34,22 +34,35 @@ public struct ValueType public BlockNode blVal; } // Abstract base class for GPLEX scanners +[GeneratedCodeAttribute( "Gardens Point Parser Generator", "1.5.2")] public abstract class ScanBase : AbstractScanner<ValueType,LexLocation> { private LexLocation __yylloc = new LexLocation(); public override LexLocation yylloc { get { return __yylloc; } set { __yylloc = value; } } protected virtual bool yywrap() { return true; } } +// Utility class for encapsulating token information +[GeneratedCodeAttribute( "Gardens Point Parser Generator", "1.5.2")] +public class ScanObj { + public int token; + public ValueType yylval; + public LexLocation yylloc; + public ScanObj( int t, ValueType val, LexLocation loc ) { + this.token = t; this.yylval = val; this.yylloc = loc; + } +} + +[GeneratedCodeAttribute( "Gardens Point Parser Generator", "1.5.2")] public class Parser: ShiftReduceParser<ValueType, LexLocation> { - // Verbatim content from SimpleYacc.y -// Эти объявления добавляются в класс GPPGParser, представляющий собой парсер, генерируемый системой gppg - public BlockNode root; // Корневой узел синтаксического дерева + // Verbatim content from SimpleYacc.y - 24.09.2018 23:47:03 +// ГќГІГЁ îáúÿâëåГГЁГї äîáà âëÿþòñÿ Гў êëà ññ GPPGParser, ïðåäñòà âëÿþùèé ñîáîé ïà ðñåð, ГЈГҐГåðèðóåìûé ñèñòåìîé gppg + public BlockNode root; // ÊîðГåâîé óçåë Г±ГЁГòà êñè÷åñêîãî äåðåâà public Parser(AbstractScanner<ValueType, LexLocation> scanner) : base(scanner) { } - // End verbatim content from SimpleYacc.y + // End verbatim content from SimpleYacc.y - 24.09.2018 23:47:03 #pragma warning disable 649 - private static Dictionary<int, string> aliasses; + private static Dictionary<int, string> aliases; #pragma warning restore 649 private static Rule[] rules = new Rule[14]; private static State[] states = new State[22]; @@ -58,42 +71,44 @@ public class Parser: ShiftReduceParser<ValueType, LexLocation> "$accept", }; static Parser() { - states[0] = new State(new int[]{3,4},new int[]{-8,1,-7,3}); - states[1] = new State(new int[]{2,2}); + states[0] = new State(new int[]{4,4},new int[]{-8,1,-7,3}); + states[1] = new State(new int[]{3,2}); states[2] = new State(-1); states[3] = new State(-2); - states[4] = new State(new int[]{10,14,3,4,5,18},new int[]{-6,5,-4,21,-3,9,-2,10,-7,16,-5,17}); - states[5] = new State(new int[]{4,6,7,7}); + states[4] = new State(new int[]{11,14,4,4,6,18},new int[]{-6,5,-4,21,-3,9,-2,10,-7,16,-5,17}); + states[5] = new State(new int[]{5,6,8,7}); states[6] = new State(-12); - states[7] = new State(new int[]{10,14,3,4,5,18},new int[]{-4,8,-3,9,-2,10,-7,16,-5,17}); + states[7] = new State(new int[]{11,14,4,4,6,18},new int[]{-4,8,-3,9,-2,10,-7,16,-5,17}); states[8] = new State(-4); states[9] = new State(-5); - states[10] = new State(new int[]{6,11}); - states[11] = new State(new int[]{10,14,8,15},new int[]{-1,12,-2,13}); + states[10] = new State(new int[]{7,11}); + states[11] = new State(new int[]{11,14,9,15},new int[]{-1,12,-2,13}); states[12] = new State(-9); states[13] = new State(-10); states[14] = new State(-8); states[15] = new State(-11); states[16] = new State(-6); states[17] = new State(-7); - states[18] = new State(new int[]{10,14,8,15},new int[]{-1,19,-2,13}); - states[19] = new State(new int[]{10,14,3,4,5,18},new int[]{-4,20,-3,9,-2,10,-7,16,-5,17}); + states[18] = new State(new int[]{11,14,9,15},new int[]{-1,19,-2,13}); + states[19] = new State(new int[]{11,14,4,4,6,18},new int[]{-4,20,-3,9,-2,10,-7,16,-5,17}); states[20] = new State(-13); states[21] = new State(-3); - rules[1] = new Rule(-9, new int[]{-8,2}); + for (int sNo = 0; sNo < states.Length; sNo++) states[sNo].number = sNo; + + rules[1] = new Rule(-9, new int[]{-8,3}); rules[2] = new Rule(-8, new int[]{-7}); rules[3] = new Rule(-6, new int[]{-4}); - rules[4] = new Rule(-6, new int[]{-6,7,-4}); + rules[4] = new Rule(-6, new int[]{-6,8,-4}); rules[5] = new Rule(-4, new int[]{-3}); rules[6] = new Rule(-4, new int[]{-7}); rules[7] = new Rule(-4, new int[]{-5}); - rules[8] = new Rule(-2, new int[]{10}); - rules[9] = new Rule(-3, new int[]{-2,6,-1}); + rules[8] = new Rule(-2, new int[]{11}); + rules[9] = new Rule(-3, new int[]{-2,7,-1}); rules[10] = new Rule(-1, new int[]{-2}); - rules[11] = new Rule(-1, new int[]{8}); - rules[12] = new Rule(-7, new int[]{3,-6,4}); - rules[13] = new Rule(-5, new int[]{5,-1,-4}); + rules[11] = new Rule(-1, new int[]{9}); + rules[12] = new Rule(-7, new int[]{4,-6,5}); + rules[13] = new Rule(-5, new int[]{6,-1,-4}); } protected override void Initialize() { @@ -105,6 +120,7 @@ public class Parser: ShiftReduceParser<ValueType, LexLocation> protected override void DoAction(int action) { +#pragma warning disable 162, 1522 switch (action) { case 2: // progr -> block @@ -149,12 +165,13 @@ public class Parser: ShiftReduceParser<ValueType, LexLocation> { CurrentSemanticValue.stVal = new CycleNode(ValueStack[ValueStack.Depth-2].eVal, ValueStack[ValueStack.Depth-1].stVal); } break; } +#pragma warning restore 162, 1522 } protected override string TerminalToString(int terminal) { - if (aliasses != null && aliasses.ContainsKey(terminal)) - return aliasses[terminal]; + if (aliases != null && aliases.ContainsKey(terminal)) + return aliases[terminal]; else if (((Tokens)terminal).ToString() != terminal.ToString(CultureInfo.InvariantCulture)) return ((Tokens)terminal).ToString(); else diff --git a/Module6/SimpleLanguage1/SimpleYacc.y b/Module6/SimpleYacc.y similarity index 100% rename from Module6/SimpleLanguage1/SimpleYacc.y rename to Module6/SimpleYacc.y diff --git a/Module6/SimpleLanguage1/a.txt b/Module6/a.txt similarity index 100% rename from Module6/SimpleLanguage1/a.txt rename to Module6/a.txt diff --git a/Module6/SimpleLanguage1/generateParserScanner.bat b/Module6/generateParserScanner.bat similarity index 100% rename from Module6/SimpleLanguage1/generateParserScanner.bat rename to Module6/generateParserScanner.bat diff --git a/Module6/packages.config b/Module6/packages.config new file mode 100644 index 0000000000000000000000000000000000000000..7a0d545fef78dffb45e66ad6c4bf9598a681c3bf --- /dev/null +++ b/Module6/packages.config @@ -0,0 +1,4 @@ +п»ї<?xml version="1.0" encoding="utf-8"?> +<packages> + <package id="Newtonsoft.Json" version="11.0.2" targetFramework="net35-client" /> +</packages> \ No newline at end of file diff --git a/Module7/SimpleLanguage2/Main.cs b/Module7/Main.cs similarity index 52% rename from Module7/SimpleLanguage2/Main.cs rename to Module7/Main.cs index 3734ff4626f06dc4d5e55a950e7c16830ff8d5b7..26d43a10e7ea3ee23f92673438329a115b9f17db 100644 --- a/Module7/SimpleLanguage2/Main.cs +++ b/Module7/Main.cs @@ -1,4 +1,4 @@ -п»їusing System; +using System; using System.IO; using System.Text; using System.Reflection; @@ -30,14 +30,38 @@ namespace SimpleCompiler { Console.WriteLine("Синтаксическое дерево построено"); + var pp = new PrettyPrintVisitor(); + parser.root.Visit(pp); + Console.WriteLine(pp.Text); + Console.WriteLine("-------------------------------"); + var avis = new AssignCountVisitor(); parser.root.Visit(avis); Console.WriteLine("Количество присваиваний = {0}", avis.Count); Console.WriteLine("-------------------------------"); - var pp = new PrettyPrintVisitor(); + var midCount = new CountCyclesOpVisitor(); + parser.root.Visit(midCount); + Console.WriteLine("Среднее количество операторов = {0}", midCount.MidCount()); + Console.WriteLine("-------------------------------"); + + var cuv = new CommonlyUsedVarVisitor(); + parser.root.Visit(cuv); + Console.WriteLine("Наиболее часто используемая переменная = {0}", cuv.mostCommonlyUsedVar()); + Console.WriteLine("-------------------------------"); + + var cviv = new ChangeVarIdVisitor("a", "d"); + parser.root.Visit(cviv); + Console.WriteLine("Переименование переменной a РЅР° d:"); + pp = new PrettyPrintVisitor(); parser.root.Visit(pp); Console.WriteLine(pp.Text); + Console.WriteLine("-------------------------------"); + + var mncv = new MaxNestCyclesVisitor(); + parser.root.Visit(mncv); + Console.WriteLine("Максимальная вложенность циклов = {0}", mncv.MaxNest); + Console.WriteLine("-------------------------------"); } } catch (FileNotFoundException) diff --git a/Module7/SimpleLanguage2/ParserHelper.cs b/Module7/ParserHelper.cs similarity index 100% rename from Module7/SimpleLanguage2/ParserHelper.cs rename to Module7/ParserHelper.cs diff --git a/Module7/SimpleLanguage2/SimpleLang2.csproj b/Module7/ParserVistors.csproj similarity index 75% rename from Module7/SimpleLanguage2/SimpleLang2.csproj rename to Module7/ParserVistors.csproj index e83a899a768f901c77a1b4acae71a66093166596..e63c34acc004fa062fc380e9830afc873b9e3803 100644 --- a/Module7/SimpleLanguage2/SimpleLang2.csproj +++ b/Module7/ParserVistors.csproj @@ -5,12 +5,12 @@ <Platform Condition=" '$(Platform)' == '' ">x86</Platform> <ProductVersion>8.0.30703</ProductVersion> <SchemaVersion>2.0</SchemaVersion> - <ProjectGuid>{12B9D996-7B4A-4EE4-9AD8-2E24EAF3F574}</ProjectGuid> + <ProjectGuid>{5F9F534D-DDE0-4B4A-9BC2-2AF8C9ED83FA}</ProjectGuid> <OutputType>Exe</OutputType> <AppDesignerFolder>Properties</AppDesignerFolder> <RootNamespace>SimpleLang</RootNamespace> <AssemblyName>SimpleLang</AssemblyName> - <TargetFrameworkVersion>v4.0</TargetFrameworkVersion> + <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> <TargetFrameworkProfile>Client</TargetFrameworkProfile> <FileAlignment>512</FileAlignment> </PropertyGroup> @@ -34,19 +34,27 @@ <WarningLevel>4</WarningLevel> </PropertyGroup> <ItemGroup> + <Reference Include="Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> + <HintPath>C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.CSharp.dll</HintPath> + </Reference> <Reference Include="System" /> <Reference Include="System.Core" /> <Reference Include="System.Xml.Linq" /> <Reference Include="System.Data.DataSetExtensions" /> - <Reference Include="Microsoft.CSharp" /> <Reference Include="System.Data" /> <Reference Include="System.Xml" /> </ItemGroup> <ItemGroup> <Compile Include="Visitors\AssignCountVisitor.cs" /> <Compile Include="Visitors\AutoVisitor.cs" /> + <Compile Include="Visitors\MaxIfCycleNestVisitor.cs" /> <Compile Include="Visitors\PrettyPrintVisitor.cs" /> <Compile Include="Visitors\Visitor.cs" /> + <Compile Include="Visitors\ChangeVarIdVisitor.cs" /> + <Compile Include="Visitors\CommonlyUsedVarVisitor.cs" /> + <Compile Include="Visitors\CountCyclesOpVisitor.cs" /> + <Compile Include="Visitors\ExprComplexityVisitor.cs" /> + <Compile Include="Visitors\MaxNestCyclesVisitor.cs" /> <Compile Include="Main.cs" /> <Compile Include="ParserHelper.cs" /> <Compile Include="ProgramTree.cs" /> @@ -55,6 +63,11 @@ <Compile Include="SimpleLex.cs" /> <Compile Include="SimpleYacc.cs" /> </ItemGroup> + <ItemGroup> + <Content Include="a.txt" /> + <Content Include="SimpleLex.lex" /> + <Content Include="SimpleYacc.y" /> + </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <!-- To modify your build process, add your task inside one of the targets below and uncomment it. Other similar extension points exist, see Microsoft.Common.targets. diff --git a/Module7/SimpleLanguage2/ProgramTree.cs b/Module7/ProgramTree.cs similarity index 100% rename from Module7/SimpleLanguage2/ProgramTree.cs rename to Module7/ProgramTree.cs diff --git a/Module7/SimpleLanguage2/Properties/AssemblyInfo.cs b/Module7/Properties/AssemblyInfo.cs similarity index 100% rename from Module7/SimpleLanguage2/Properties/AssemblyInfo.cs rename to Module7/Properties/AssemblyInfo.cs diff --git a/Module7/SimpleLanguage2/ShiftReduceParserCode.cs b/Module7/ShiftReduceParserCode.cs similarity index 100% rename from Module7/SimpleLanguage2/ShiftReduceParserCode.cs rename to Module7/ShiftReduceParserCode.cs diff --git a/Module7/SimpleLanguage2/SimpleLang2.sln b/Module7/SimpleLanguage2/SimpleLang2.sln deleted file mode 100644 index 6983d32d40dc6cf461a28782034851c63e8188d9..0000000000000000000000000000000000000000 --- a/Module7/SimpleLanguage2/SimpleLang2.sln +++ /dev/null @@ -1,22 +0,0 @@ -п»ї -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2013 -VisualStudioVersion = 12.0.21005.1 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleLang2", "SimpleLang2.csproj", "{12B9D996-7B4A-4EE4-9AD8-2E24EAF3F574}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|x86 = Debug|x86 - Release|x86 = Release|x86 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {12B9D996-7B4A-4EE4-9AD8-2E24EAF3F574}.Debug|x86.ActiveCfg = Debug|x86 - {12B9D996-7B4A-4EE4-9AD8-2E24EAF3F574}.Debug|x86.Build.0 = Debug|x86 - {12B9D996-7B4A-4EE4-9AD8-2E24EAF3F574}.Release|x86.ActiveCfg = Release|x86 - {12B9D996-7B4A-4EE4-9AD8-2E24EAF3F574}.Release|x86.Build.0 = Release|x86 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection -EndGlobal diff --git a/Module8/SimpleLanguage3/SimpleLex.cs b/Module7/SimpleLex.cs similarity index 93% rename from Module8/SimpleLanguage3/SimpleLex.cs rename to Module7/SimpleLex.cs index e20b4779446708cd24302ae9f049b09f59f0ccaf..98388ef125489f1e1cc850a1341825c4971cf640 100644 --- a/Module8/SimpleLanguage3/SimpleLex.cs +++ b/Module7/SimpleLex.cs @@ -1,10 +1,14 @@ // // This CSharp output file generated by Gardens Point LEX -// Version: 1.1.3.301 -// Machine: HUB -// DateTime: 21.09.2017 21:02:17 +// Gardens Point LEX (GPLEX) is Copyright (c) John Gough, QUT 2006-2014. +// Output produced by GPLEX is the property of the user. +// See accompanying file GPLEXcopyright.rtf. +// +// GPLEX Version: 1.2.2 +// Machine: MAINHOMEPC2 +// DateTime: 30.09.2018 18:17:27 // UserName: someone -// GPLEX input file <SimpleLex.lex> +// GPLEX input file <SimpleLex.lex - 24.09.2018 23:47:03> // GPLEX frame file <embedded resource> // // Option settings: unicode, parser, minimize @@ -13,8 +17,8 @@ // // -// Experimental embedded frame -// Version 1.1.3 of 18-April-2010 +// Revised backup code +// Version 1.2.1 of 24-June-2013 // // #define BACKUP @@ -301,8 +305,7 @@ int NextState() { public Scanner(Stream file, string codepage) { SetSource(file, CodePageHandling.GetCodePage(codepage)); - } - + } #endif // !NOFILES public Scanner() { } @@ -330,7 +333,7 @@ int NextState() { if (next < 0xDC00 || next > 0xDFFF) code = ScanBuff.UnicodeReplacementChar; else - code = (0x10000 + (code & 0x3FF << 10) + (next & 0x3FF)); + code = (0x10000 + ((code & 0x3FF) << 10) + (next & 0x3FF)); } #endif cCol++; @@ -383,9 +386,7 @@ int NextState() { GetCode(); } -#if !NOFILES // ================ LineBuffer Initialization =================== - /// <summary> /// Create and initialize a LineBuff buffer object for this scanner /// </summary> @@ -399,6 +400,7 @@ int NextState() { GetCode(); } +#if !NOFILES // =============== StreamBuffer Initialization ================== /// <summary> @@ -500,6 +502,12 @@ int NextState() { } } + /// <summary> + /// Discards all but the first "n" codepoints in the recognized pattern. + /// Resets the buffer position so that only n codepoints have been consumed; + /// yytext is also re-evaluated. + /// </summary> + /// <param name="n">The number of codepoints to consume</param> [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] void yyless(int n) { @@ -519,6 +527,10 @@ int NextState() { // but it does not seem possible to re-establish // the correct column counts except by going forward. // + /// <summary> + /// Removes the last "n" code points from the pattern. + /// </summary> + /// <param name="n">The number to remove</param> [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] void _yytrunc(int n) { yyless(yyleng - n); } @@ -531,18 +543,23 @@ int NextState() { // can't use (tokEPos - tokPos) because of the // possibility of surrogate pairs in the token. // + /// <summary> + /// The length of the pattern in codepoints (not the same as + /// string-length if the pattern contains any surrogate pairs). + /// </summary> [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] [SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "yyleng")] [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "yyleng")] public int yyleng { get { -#if BYTEMODE - return tokEPos - tokPos; -#else if (tokELin == tokLin) return tokECol - tokCol; - else { + else +#if BYTEMODE + return tokEPos - tokPos; +#else + { int ch; int count = 0; int save = buffer.Pos; @@ -551,7 +568,7 @@ int NextState() { ch = buffer.Read(); if (!char.IsHighSurrogate((char)ch)) count++; } while (buffer.Pos < tokEPos && ch != ScanBuff.EndOfFile); - buffer.Pos = save; + buffer.Pos = save; return count; } #endif // BYTEMODE @@ -576,65 +593,56 @@ int NextState() { // ============== The main tokenizer code ================= - int Scan() - { + int Scan() { try { - for (; ; ) - { - int next; // next state to enter -#if BACKUP - Result rslt = Result.noMatch; -#endif // BACKUP + for (; ; ) { + int next; // next state to enter #if LEFTANCHORS - for (;;) - { + for (;;) { // Discard characters that do not start any pattern. // Must check the left anchor condition after *every* GetCode! state = ((cCol == 0) ? anchorState[currentScOrd] : currentStart); - if ((next = NextState()) != goStart) - break; // LOOP EXIT HERE... + if ((next = NextState()) != goStart) break; // LOOP EXIT HERE... GetCode(); } #else // !LEFTANCHORS state = currentStart; - while ((next = NextState()) == goStart) + while ((next = NextState()) == goStart) { // At this point, the current character has no // transition from the current state. We discard // the "no-match" char. In traditional LEX such // characters are echoed to the console. GetCode(); + } #endif // LEFTANCHORS // At last, a valid transition ... MarkToken(); state = next; - GetCode(); - - while ((next = NextState()) > eofNum) // Exit for goStart AND for eofNum + GetCode(); #if BACKUP - if (state <= maxAccept && next > maxAccept) // need to prepare backup data - { - // ctx is an object. The fields may be - // mutated by the call to Recurse2. - // On return the data in ctx is the - // *latest* accept state that was found. - - rslt = Recurse2(ref ctx, next); - if (rslt == Result.noMatch) - RestoreStateAndPos(ref ctx); - break; + bool contextSaved = false; + while ((next = NextState()) > eofNum) { // Exit for goStart AND for eofNum + if (state <= maxAccept && next > maxAccept) { // need to prepare backup data + // Store data for the *latest* accept state that was found. + SaveStateAndPos( ref ctx ); + contextSaved = true; } - else + state = next; + GetCode(); + } + if (state > maxAccept && contextSaved) + RestoreStateAndPos( ref ctx ); +#else // BACKUP + while ((next = NextState()) > eofNum) { // Exit for goStart AND for eofNum + state = next; + GetCode(); + } #endif // BACKUP - { - state = next; - GetCode(); - } - if (state <= maxAccept) - { + if (state <= maxAccept) { MarkEnd(); #region ActionSwitch -#pragma warning disable 162 +#pragma warning disable 162, 1522 switch (state) { case eofNum: @@ -698,7 +706,7 @@ yylval.dVal = double.Parse(yytext); default: break; } -#pragma warning restore 162 +#pragma warning restore 162, 1522 #endregion } } @@ -711,29 +719,7 @@ yylloc = new LexLocation(tokLin, tokCol, tokELin, tokECol); } #if BACKUP - Result Recurse2(ref Context ctx, int next) - { - // Assert: at entry "state" is an accept state AND - // NextState(state, code) != goStart AND - // NextState(state, code) is not an accept state. - // - SaveStateAndPos(ref ctx); - state = next; - GetCode(); - - while ((next = NextState()) > eofNum) - { - if (state <= maxAccept && next > maxAccept) // need to update backup data - SaveStateAndPos(ref ctx); - state = next; - if (state == eofNum) return Result.accept; - GetCode(); - } - return (state <= maxAccept ? Result.accept : Result.noMatch); - } - - void SaveStateAndPos(ref Context ctx) - { + void SaveStateAndPos(ref Context ctx) { ctx.bPos = buffer.Pos; ctx.rPos = readPos; ctx.cCol = cCol; @@ -742,8 +728,7 @@ yylloc = new LexLocation(tokLin, tokCol, tokELin, tokECol); ctx.cChr = code; } - void RestoreStateAndPos(ref Context ctx) - { + void RestoreStateAndPos(ref Context ctx) { buffer.Pos = ctx.bPos; readPos = ctx.rPos; cCol = ctx.cCol; @@ -751,8 +736,7 @@ yylloc = new LexLocation(tokLin, tokCol, tokELin, tokECol); state = ctx.state; code = ctx.cChr; } - -#endif // BACKUP +#endif // BACKUP // ============= End of the tokenizer code ================ @@ -784,16 +768,16 @@ yylloc = new LexLocation(tokLin, tokCol, tokELin, tokECol); #region UserCodeSection -public override void yyerror(string format, params object[] args) // обработка синтаксических ошибок +public override void yyerror(string format, params object[] args) // îáðà áîòêà ñèГГІГ ГЄГ±ГЁГ·ГҐГ±ГЄГЁГµ îøèáîê { var ww = args.Skip(1).Cast<string>().ToArray(); - string errorMsg = string.Format("({0},{1}): Встречено {2}, а ожидалось {3}", yyline, yycol, args[0], string.Join(" или ", ww)); + string errorMsg = string.Format("({0},{1}): Âñòðå÷åГГ® {2}, à îæèäà ëîñü {3}", yyline, yycol, args[0], string.Join(" èëè ", ww)); throw new SyntaxException(errorMsg); } public void LexError() { - string errorMsg = string.Format("({0},{1}): Неизвестный символ {2}", yyline, yycol, yytext); + string errorMsg = string.Format("({0},{1}): ÍåèçâåñòГûé ñèìâîë {2}", yyline, yycol, yytext); throw new LexException(errorMsg); } @@ -869,6 +853,7 @@ class ScannerHelper return new LineBuffer(source); } +#if (!NOFILES) public static ScanBuff GetBuffer(Stream source) { return new BuildBuffer(source); @@ -879,7 +864,8 @@ class ScannerHelper { return new BuildBuffer(source, fallbackCodePage); } -#endif +#endif // !BYTEMODE +#endif // !NOFILES } #region Buffer classes @@ -1002,7 +988,7 @@ class ScannerHelper { ix = lstart = 0; } - for (; ; ) + while (ix < numLines) { int len = line[ix].Length + 1; if (pos < lstart + len) break; @@ -1060,7 +1046,8 @@ class ScannerHelper { cPos = value; findIndex(cPos, out cLine, out curLineStart); - curLine = line[cLine]; + // cLine should be the *next* line after curLine. + curLine = (cLine < numLines ? line[cLine++] : ""); curLineEnd = curLineStart + curLine.Length; } } @@ -1068,7 +1055,7 @@ class ScannerHelper public override string ToString() { return "LineBuffer"; } } - +#if (!NOFILES) // ============================================================== // ===== class BuildBuff : for unicode text files ======== // ============================================================== @@ -1309,12 +1296,13 @@ class ScannerHelper } #endif // !BYTEMODE } +#endif // !NOFILES #endregion Buffer classes // ============================================================== // ============ class CodePageHandling ============= // ============================================================== - +#if (!NOFILES) public static class CodePageHandling { public static int GetCodePage(string option) @@ -1525,6 +1513,7 @@ class ScannerHelper #endif // !BYTEMODE #endregion +#endif // !NOFILES // End of code copied from embedded resource diff --git a/Module7/SimpleLanguage2/SimpleLex.lex b/Module7/SimpleLex.lex similarity index 100% rename from Module7/SimpleLanguage2/SimpleLex.lex rename to Module7/SimpleLex.lex diff --git a/Module7/SimpleLanguage2/SimpleYacc.cs b/Module7/SimpleYacc.cs similarity index 64% rename from Module7/SimpleLanguage2/SimpleYacc.cs rename to Module7/SimpleYacc.cs index 10d52e6d8f3c51482220bb692ab16660007eb020..e0ec2ad6ff794b912c8c7509d7cc14a3103253c0 100644 --- a/Module7/SimpleLanguage2/SimpleYacc.cs +++ b/Module7/SimpleYacc.cs @@ -1,17 +1,18 @@ // This code was generated by the Gardens Point Parser Generator -// Copyright (c) Wayne Kelly, QUT 2005-2010 +// Copyright (c) Wayne Kelly, John Gough, QUT 2005-2014 // (see accompanying GPPGcopyright.rtf) -// GPPG version 1.3.6 -// Machine: SSM -// DateTime: 19.08.2014 13:38:37 -// UserName: ????????? -// Input file <SimpleYacc.y> +// GPPG version 1.5.2 +// Machine: MAINHOMEPC2 +// DateTime: 30.09.2018 18:17:29 +// UserName: someone +// Input file <SimpleYacc.y - 24.09.2018 23:47:03> // options: no-lines gplex using System; using System.Collections.Generic; +using System.CodeDom.Compiler; using System.Globalization; using System.Text; using QUT.Gppg; @@ -20,11 +21,10 @@ using ProgramTree; namespace SimpleParser { -public enum Tokens { - error=1,EOF=2,BEGIN=3,END=4,CYCLE=5,ASSIGN=6, - ASSIGNPLUS=7,ASSIGNMINUS=8,ASSIGNMULT=9,SEMICOLON=10,WRITE=11,VAR=12, - PLUS=13,MINUS=14,MULT=15,DIV=16,LPAREN=17,RPAREN=18, - COLUMN=19,INUM=20,RNUM=21,ID=22}; +public enum Tokens {error=2,EOF=3,BEGIN=4,END=5,CYCLE=6, + ASSIGN=7,ASSIGNPLUS=8,ASSIGNMINUS=9,ASSIGNMULT=10,SEMICOLON=11,WRITE=12, + VAR=13,PLUS=14,MINUS=15,MULT=16,DIV=17,LPAREN=18, + RPAREN=19,COLUMN=20,INUM=21,RNUM=22,ID=23}; public struct ValueType { @@ -37,23 +37,36 @@ public struct ValueType public BlockNode blVal; } // Abstract base class for GPLEX scanners +[GeneratedCodeAttribute( "Gardens Point Parser Generator", "1.5.2")] public abstract class ScanBase : AbstractScanner<ValueType,LexLocation> { private LexLocation __yylloc = new LexLocation(); public override LexLocation yylloc { get { return __yylloc; } set { __yylloc = value; } } protected virtual bool yywrap() { return true; } } +// Utility class for encapsulating token information +[GeneratedCodeAttribute( "Gardens Point Parser Generator", "1.5.2")] +public class ScanObj { + public int token; + public ValueType yylval; + public LexLocation yylloc; + public ScanObj( int t, ValueType val, LexLocation loc ) { + this.token = t; this.yylval = val; this.yylloc = loc; + } +} + +[GeneratedCodeAttribute( "Gardens Point Parser Generator", "1.5.2")] public class Parser: ShiftReduceParser<ValueType, LexLocation> { - // Verbatim content from SimpleYacc.y -// Эти объявления добавляются в класс GPPGParser, представляющий собой парсер, генерируемый системой gppg - public BlockNode root; // Корневой узел синтаксического дерева + // Verbatim content from SimpleYacc.y - 24.09.2018 23:47:03 +// ГќГІГЁ îáúÿâëåГГЁГї äîáà âëÿþòñÿ Гў êëà ññ GPPGParser, ïðåäñòà âëÿþùèé ñîáîé ïà ðñåð, ГЈГҐГåðèðóåìûé ñèñòåìîé gppg + public BlockNode root; // ÊîðГåâîé óçåë Г±ГЁГòà êñè÷åñêîãî äåðåâà public Parser(AbstractScanner<ValueType, LexLocation> scanner) : base(scanner) { } private bool InDefSect = false; - // End verbatim content from SimpleYacc.y + // End verbatim content from SimpleYacc.y - 24.09.2018 23:47:03 #pragma warning disable 649 - private static Dictionary<int, string> aliasses; + private static Dictionary<int, string> aliases; #pragma warning restore 649 private static Rule[] rules = new Rule[30]; private static State[] states = new State[48]; @@ -62,59 +75,61 @@ public class Parser: ShiftReduceParser<ValueType, LexLocation> "write", "empty", "var", "varlist", "stlist", "$accept", "Anon@1", }; static Parser() { - states[0] = new State(new int[]{3,4},new int[]{-1,1,-8,3}); - states[1] = new State(new int[]{2,2}); + states[0] = new State(new int[]{4,4},new int[]{-1,1,-8,3}); + states[1] = new State(new int[]{3,2}); states[2] = new State(-1); states[3] = new State(-2); - states[4] = new State(new int[]{22,18,3,4,5,31,11,35,12,40,4,-11,10,-11},new int[]{-14,5,-6,47,-7,9,-3,10,-8,29,-9,30,-10,34,-12,39,-11,46}); - states[5] = new State(new int[]{4,6,10,7}); + states[4] = new State(new int[]{23,18,4,4,6,31,12,35,13,40,5,-11,11,-11},new int[]{-14,5,-6,47,-7,9,-3,10,-8,29,-9,30,-10,34,-12,39,-11,46}); + states[5] = new State(new int[]{5,6,11,7}); states[6] = new State(-23); - states[7] = new State(new int[]{22,18,3,4,5,31,11,35,12,40,4,-11,10,-11},new int[]{-6,8,-7,9,-3,10,-8,29,-9,30,-10,34,-12,39,-11,46}); + states[7] = new State(new int[]{23,18,4,4,6,31,12,35,13,40,5,-11,11,-11},new int[]{-6,8,-7,9,-3,10,-8,29,-9,30,-10,34,-12,39,-11,46}); states[8] = new State(-4); states[9] = new State(-5); - states[10] = new State(new int[]{6,11}); - states[11] = new State(new int[]{22,18,20,19,17,20},new int[]{-2,12,-4,28,-5,27,-3,17}); - states[12] = new State(new int[]{13,13,14,23,4,-13,10,-13}); - states[13] = new State(new int[]{22,18,20,19,17,20},new int[]{-4,14,-5,27,-3,17}); - states[14] = new State(new int[]{15,15,16,25,13,-14,14,-14,4,-14,10,-14,18,-14,22,-14,3,-14,5,-14,11,-14,12,-14}); - states[15] = new State(new int[]{22,18,20,19,17,20},new int[]{-5,16,-3,17}); + states[10] = new State(new int[]{7,11}); + states[11] = new State(new int[]{23,18,21,19,18,20},new int[]{-2,12,-4,28,-5,27,-3,17}); + states[12] = new State(new int[]{14,13,15,23,5,-13,11,-13}); + states[13] = new State(new int[]{23,18,21,19,18,20},new int[]{-4,14,-5,27,-3,17}); + states[14] = new State(new int[]{16,15,17,25,14,-14,15,-14,5,-14,11,-14,19,-14,23,-14,4,-14,6,-14,12,-14,13,-14}); + states[15] = new State(new int[]{23,18,21,19,18,20},new int[]{-5,16,-3,17}); states[16] = new State(-17); states[17] = new State(-20); states[18] = new State(-12); states[19] = new State(-21); - states[20] = new State(new int[]{22,18,20,19,17,20},new int[]{-2,21,-4,28,-5,27,-3,17}); - states[21] = new State(new int[]{18,22,13,13,14,23}); + states[20] = new State(new int[]{23,18,21,19,18,20},new int[]{-2,21,-4,28,-5,27,-3,17}); + states[21] = new State(new int[]{19,22,14,13,15,23}); states[22] = new State(-22); - states[23] = new State(new int[]{22,18,20,19,17,20},new int[]{-4,24,-5,27,-3,17}); - states[24] = new State(new int[]{15,15,16,25,13,-15,14,-15,4,-15,10,-15,18,-15,22,-15,3,-15,5,-15,11,-15,12,-15}); - states[25] = new State(new int[]{22,18,20,19,17,20},new int[]{-5,26,-3,17}); + states[23] = new State(new int[]{23,18,21,19,18,20},new int[]{-4,24,-5,27,-3,17}); + states[24] = new State(new int[]{16,15,17,25,14,-15,15,-15,5,-15,11,-15,19,-15,23,-15,4,-15,6,-15,12,-15,13,-15}); + states[25] = new State(new int[]{23,18,21,19,18,20},new int[]{-5,26,-3,17}); states[26] = new State(-18); states[27] = new State(-19); - states[28] = new State(new int[]{15,15,16,25,13,-16,14,-16,4,-16,10,-16,18,-16,22,-16,3,-16,5,-16,11,-16,12,-16}); + states[28] = new State(new int[]{16,15,17,25,14,-16,15,-16,5,-16,11,-16,19,-16,23,-16,4,-16,6,-16,12,-16,13,-16}); states[29] = new State(-6); states[30] = new State(-7); - states[31] = new State(new int[]{22,18,20,19,17,20},new int[]{-2,32,-4,28,-5,27,-3,17}); - states[32] = new State(new int[]{13,13,14,23,22,18,3,4,5,31,11,35,12,40,4,-11,10,-11},new int[]{-6,33,-7,9,-3,10,-8,29,-9,30,-10,34,-12,39,-11,46}); + states[31] = new State(new int[]{23,18,21,19,18,20},new int[]{-2,32,-4,28,-5,27,-3,17}); + states[32] = new State(new int[]{14,13,15,23,23,18,4,4,6,31,12,35,13,40,5,-11,11,-11},new int[]{-6,33,-7,9,-3,10,-8,29,-9,30,-10,34,-12,39,-11,46}); states[33] = new State(-24); states[34] = new State(-8); - states[35] = new State(new int[]{17,36}); - states[36] = new State(new int[]{22,18,20,19,17,20},new int[]{-2,37,-4,28,-5,27,-3,17}); - states[37] = new State(new int[]{18,38,13,13,14,23}); + states[35] = new State(new int[]{18,36}); + states[36] = new State(new int[]{23,18,21,19,18,20},new int[]{-2,37,-4,28,-5,27,-3,17}); + states[37] = new State(new int[]{19,38,14,13,15,23}); states[38] = new State(-25); states[39] = new State(-9); states[40] = new State(-26,new int[]{-16,41}); - states[41] = new State(new int[]{22,18},new int[]{-13,42,-3,45}); - states[42] = new State(new int[]{19,43,4,-27,10,-27}); - states[43] = new State(new int[]{22,18},new int[]{-3,44}); + states[41] = new State(new int[]{23,18},new int[]{-13,42,-3,45}); + states[42] = new State(new int[]{20,43,5,-27,11,-27}); + states[43] = new State(new int[]{23,18},new int[]{-3,44}); states[44] = new State(-29); states[45] = new State(-28); states[46] = new State(-10); states[47] = new State(-3); - rules[1] = new Rule(-15, new int[]{-1,2}); + for (int sNo = 0; sNo < states.Length; sNo++) states[sNo].number = sNo; + + rules[1] = new Rule(-15, new int[]{-1,3}); rules[2] = new Rule(-1, new int[]{-8}); rules[3] = new Rule(-14, new int[]{-6}); - rules[4] = new Rule(-14, new int[]{-14,10,-6}); + rules[4] = new Rule(-14, new int[]{-14,11,-6}); rules[5] = new Rule(-6, new int[]{-7}); rules[6] = new Rule(-6, new int[]{-8}); rules[7] = new Rule(-6, new int[]{-9}); @@ -122,24 +137,24 @@ public class Parser: ShiftReduceParser<ValueType, LexLocation> rules[9] = new Rule(-6, new int[]{-12}); rules[10] = new Rule(-6, new int[]{-11}); rules[11] = new Rule(-11, new int[]{}); - rules[12] = new Rule(-3, new int[]{22}); - rules[13] = new Rule(-7, new int[]{-3,6,-2}); - rules[14] = new Rule(-2, new int[]{-2,13,-4}); - rules[15] = new Rule(-2, new int[]{-2,14,-4}); + rules[12] = new Rule(-3, new int[]{23}); + rules[13] = new Rule(-7, new int[]{-3,7,-2}); + rules[14] = new Rule(-2, new int[]{-2,14,-4}); + rules[15] = new Rule(-2, new int[]{-2,15,-4}); rules[16] = new Rule(-2, new int[]{-4}); - rules[17] = new Rule(-4, new int[]{-4,15,-5}); - rules[18] = new Rule(-4, new int[]{-4,16,-5}); + rules[17] = new Rule(-4, new int[]{-4,16,-5}); + rules[18] = new Rule(-4, new int[]{-4,17,-5}); rules[19] = new Rule(-4, new int[]{-5}); rules[20] = new Rule(-5, new int[]{-3}); - rules[21] = new Rule(-5, new int[]{20}); - rules[22] = new Rule(-5, new int[]{17,-2,18}); - rules[23] = new Rule(-8, new int[]{3,-14,4}); - rules[24] = new Rule(-9, new int[]{5,-2,-6}); - rules[25] = new Rule(-10, new int[]{11,17,-2,18}); + rules[21] = new Rule(-5, new int[]{21}); + rules[22] = new Rule(-5, new int[]{18,-2,19}); + rules[23] = new Rule(-8, new int[]{4,-14,5}); + rules[24] = new Rule(-9, new int[]{6,-2,-6}); + rules[25] = new Rule(-10, new int[]{12,18,-2,19}); rules[26] = new Rule(-16, new int[]{}); - rules[27] = new Rule(-12, new int[]{12,-16,-13}); + rules[27] = new Rule(-12, new int[]{13,-16,-13}); rules[28] = new Rule(-13, new int[]{-3}); - rules[29] = new Rule(-13, new int[]{-13,19,-3}); + rules[29] = new Rule(-13, new int[]{-13,20,-3}); } protected override void Initialize() { @@ -151,6 +166,7 @@ public class Parser: ShiftReduceParser<ValueType, LexLocation> protected override void DoAction(int action) { +#pragma warning disable 162, 1522 switch (action) { case 2: // progr -> block @@ -192,7 +208,7 @@ public class Parser: ShiftReduceParser<ValueType, LexLocation> { if (!InDefSect) if (!SymbolTable.vars.ContainsKey(ValueStack[ValueStack.Depth-1].sVal)) - throw new Exception("("+LocationStack[LocationStack.Depth-1].StartLine+","+LocationStack[LocationStack.Depth-1].StartColumn+"): Переменная "+ValueStack[ValueStack.Depth-1].sVal+" не описана"); + throw new Exception("("+LocationStack[LocationStack.Depth-1].StartLine+","+LocationStack[LocationStack.Depth-1].StartColumn+"): ÏåðåìåГГГ Гї "+ValueStack[ValueStack.Depth-1].sVal+" ГГҐ îïèñà ГГ "); CurrentSemanticValue.eVal = new IdNode(ValueStack[ValueStack.Depth-1].sVal); } break; @@ -257,12 +273,13 @@ public class Parser: ShiftReduceParser<ValueType, LexLocation> } break; } +#pragma warning restore 162, 1522 } protected override string TerminalToString(int terminal) { - if (aliasses != null && aliasses.ContainsKey(terminal)) - return aliasses[terminal]; + if (aliases != null && aliases.ContainsKey(terminal)) + return aliases[terminal]; else if (((Tokens)terminal).ToString() != terminal.ToString(CultureInfo.InvariantCulture)) return ((Tokens)terminal).ToString(); else diff --git a/Module7/SimpleLanguage2/SimpleYacc.y b/Module7/SimpleYacc.y similarity index 100% rename from Module7/SimpleLanguage2/SimpleYacc.y rename to Module7/SimpleYacc.y diff --git a/Module7/Visitors/AssignCountVisitor.cs b/Module7/Visitors/AssignCountVisitor.cs new file mode 100644 index 0000000000000000000000000000000000000000..ed269333c7f57d64c47c611f5798d31aac19ddf1 --- /dev/null +++ b/Module7/Visitors/AssignCountVisitor.cs @@ -0,0 +1,14 @@ +п»їusing System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using ProgramTree; + +namespace SimpleLang.Visitors +{ + public class AssignCountVisitor : AutoVisitor + { + public int Count = 0; + + } +} diff --git a/Module8/SimpleLanguage3/Visitors/AutoVisitor.cs b/Module7/Visitors/AutoVisitor.cs similarity index 97% rename from Module8/SimpleLanguage3/Visitors/AutoVisitor.cs rename to Module7/Visitors/AutoVisitor.cs index 7c92bab8a5812171a386e299bcf6fc32c3cde49a..4bc07787c1c5f14bc9f05ff665e570994bd17e63 100644 --- a/Module8/SimpleLanguage3/Visitors/AutoVisitor.cs +++ b/Module7/Visitors/AutoVisitor.cs @@ -9,7 +9,7 @@ namespace SimpleLang.Visitors // базовая логика РѕР±С…РѕРґР° без действий // Если нужны действия или другая логика РѕР±С…РѕРґР°, то соответствующие методы надо переопределять // РџСЂРё переопределении методов для задания действий необходимо РЅРµ забывать обходить подузлы - class AutoVisitor: Visitor + public class AutoVisitor: Visitor { public override void VisitBinOpNode(BinOpNode binop) { diff --git a/Module7/Visitors/ChangeVarIdVisitor.cs b/Module7/Visitors/ChangeVarIdVisitor.cs new file mode 100644 index 0000000000000000000000000000000000000000..7e8942df896a2c4f3d4539a1ae3012d3bb483cc4 --- /dev/null +++ b/Module7/Visitors/ChangeVarIdVisitor.cs @@ -0,0 +1,20 @@ +п»їusing System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using ProgramTree; + +namespace SimpleLang.Visitors +{ + public class ChangeVarIdVisitor : AutoVisitor + { + private string from, to; + + public ChangeVarIdVisitor(string _from, string _to) + { + from = _from; + to = _to; + } + + } +} diff --git a/Module7/Visitors/CommonlyUsedVarVisitor.cs b/Module7/Visitors/CommonlyUsedVarVisitor.cs new file mode 100644 index 0000000000000000000000000000000000000000..a73e3b6cfeb2662db626d3649d7cefc95e687c2d --- /dev/null +++ b/Module7/Visitors/CommonlyUsedVarVisitor.cs @@ -0,0 +1,16 @@ +п»їusing System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using ProgramTree; + +namespace SimpleLang.Visitors +{ + public class CommonlyUsedVarVisitor : AutoVisitor + { + public string mostCommonlyUsedVar() + { + throw new NotImplementedException(); + } + } +} diff --git a/Module7/Visitors/CountCyclesOpVisitor.cs b/Module7/Visitors/CountCyclesOpVisitor.cs new file mode 100644 index 0000000000000000000000000000000000000000..5b5368b01bb2ece936bdcff542998c8c01009aa5 --- /dev/null +++ b/Module7/Visitors/CountCyclesOpVisitor.cs @@ -0,0 +1,16 @@ +п»їusing System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using ProgramTree; + +namespace SimpleLang.Visitors +{ + public class CountCyclesOpVisitor : AutoVisitor + { + public int MidCount() + { + throw new NotImplementedException(); + } + } +} diff --git a/Module7/Visitors/ExprComplexityVisitor.cs b/Module7/Visitors/ExprComplexityVisitor.cs new file mode 100644 index 0000000000000000000000000000000000000000..ef9f0b2a9e8e6d76f0cb90d25515bea16760776e --- /dev/null +++ b/Module7/Visitors/ExprComplexityVisitor.cs @@ -0,0 +1,18 @@ +п»їusing System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using ProgramTree; + +namespace SimpleLang.Visitors +{ + public class ExprComplexityVisitor : AutoVisitor + { + // СЃРїРёСЃРѕРє должен содержать сложность каждого выражения, встреченного РїСЂРё обычном РїРѕСЂСЏРґРєРµ РѕР±С…РѕРґР° AST + public List<int> getComplexityList() + { + throw new NotImplementedException(); + } + + } +} diff --git a/Module7/Visitors/MaxIfCycleNestVisitor.cs b/Module7/Visitors/MaxIfCycleNestVisitor.cs new file mode 100644 index 0000000000000000000000000000000000000000..c399a8ec4e3a067d99e207abc8e8e04f1f0baadb --- /dev/null +++ b/Module7/Visitors/MaxIfCycleNestVisitor.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using ProgramTree; + + +namespace SimpleLang.Visitors +{ + public class MaxIfCycleNestVisitor : AutoVisitor + { + public int MaxNest = 0; + } +} \ No newline at end of file diff --git a/Module7/Visitors/MaxNestCyclesVisitor.cs b/Module7/Visitors/MaxNestCyclesVisitor.cs new file mode 100644 index 0000000000000000000000000000000000000000..e5c768b4052dd05dd6eb58a87db533df5929cf0a --- /dev/null +++ b/Module7/Visitors/MaxNestCyclesVisitor.cs @@ -0,0 +1,13 @@ +п»їusing System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using ProgramTree; + +namespace SimpleLang.Visitors +{ + public class MaxNestCyclesVisitor : AutoVisitor + { + public int MaxNest = 0; + } +} diff --git a/Module7/SimpleLanguage2/Visitors/PrettyPrintVisitor.cs b/Module7/Visitors/PrettyPrintVisitor.cs similarity index 100% rename from Module7/SimpleLanguage2/Visitors/PrettyPrintVisitor.cs rename to Module7/Visitors/PrettyPrintVisitor.cs diff --git a/Module7/SimpleLanguage2/Visitors/Visitor.cs b/Module7/Visitors/Visitor.cs similarity index 100% rename from Module7/SimpleLanguage2/Visitors/Visitor.cs rename to Module7/Visitors/Visitor.cs diff --git a/Module7/SimpleLanguage2/a.txt b/Module7/a.txt similarity index 59% rename from Module7/SimpleLanguage2/a.txt rename to Module7/a.txt index 14eeab4e71d36f47bc653b39285e15b71ec75311..6b4dcc3a46fe12c788850aeee79ba72b9fccc12e 100644 --- a/Module7/SimpleLanguage2/a.txt +++ b/Module7/a.txt @@ -6,6 +6,14 @@ begin cycle 3 begin a := a + 1; + cycle 3 + begin + a := 1 + end; write(a) + end; + cycle 3 + begin + d := 2 end end diff --git a/Module7/SimpleLanguage2/generateParserScanner.bat b/Module7/generateParserScanner.bat similarity index 100% rename from Module7/SimpleLanguage2/generateParserScanner.bat rename to Module7/generateParserScanner.bat diff --git a/Module8/SimpleLanguage3/Main.cs b/Module8/Main.cs similarity index 100% rename from Module8/SimpleLanguage3/Main.cs rename to Module8/Main.cs diff --git a/Module8/SimpleLanguage3/SimpleLang3.csproj b/Module8/ParserGenerator.csproj similarity index 83% rename from Module8/SimpleLanguage3/SimpleLang3.csproj rename to Module8/ParserGenerator.csproj index c1895351e77ec8c19611454dbf7ba10910d985e6..946ec9c81393b23d5997bf6fad324cac0ab69901 100644 --- a/Module8/SimpleLanguage3/SimpleLang3.csproj +++ b/Module8/ParserGenerator.csproj @@ -5,12 +5,12 @@ <Platform Condition=" '$(Platform)' == '' ">x86</Platform> <ProductVersion>8.0.30703</ProductVersion> <SchemaVersion>2.0</SchemaVersion> - <ProjectGuid>{12B9D996-7B4A-4EE4-9AD8-2E24EAF3F574}</ProjectGuid> + <ProjectGuid>{F8734914-3677-4A80-A87D-C274F52576FD}</ProjectGuid> <OutputType>Exe</OutputType> <AppDesignerFolder>Properties</AppDesignerFolder> <RootNamespace>SimpleLang</RootNamespace> <AssemblyName>SimpleLang</AssemblyName> - <TargetFrameworkVersion>v4.0</TargetFrameworkVersion> + <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> <TargetFrameworkProfile>Client</TargetFrameworkProfile> <FileAlignment>512</FileAlignment> </PropertyGroup> @@ -34,11 +34,13 @@ <WarningLevel>4</WarningLevel> </PropertyGroup> <ItemGroup> + <Reference Include="Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> + <HintPath>C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.CSharp.dll</HintPath> + </Reference> <Reference Include="System" /> <Reference Include="System.Core" /> <Reference Include="System.Xml.Linq" /> <Reference Include="System.Data.DataSetExtensions" /> - <Reference Include="Microsoft.CSharp" /> <Reference Include="System.Data" /> <Reference Include="System.Xml" /> </ItemGroup> @@ -57,6 +59,12 @@ <Compile Include="SimpleLex.cs" /> <Compile Include="SimpleYacc.cs" /> </ItemGroup> + <ItemGroup> + <Content Include="a.txt" /> + <Content Include="generateParserScanner.bat" /> + <Content Include="SimpleLex.lex" /> + <Content Include="SimpleYacc.y" /> + </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <!-- To modify your build process, add your task inside one of the targets below and uncomment it. Other similar extension points exist, see Microsoft.Common.targets. diff --git a/Module8/SimpleLanguage3/ParserHelper.cs b/Module8/ParserHelper.cs similarity index 100% rename from Module8/SimpleLanguage3/ParserHelper.cs rename to Module8/ParserHelper.cs diff --git a/Module8/SimpleLanguage3/ProgramTree.cs b/Module8/ProgramTree.cs similarity index 89% rename from Module8/SimpleLanguage3/ProgramTree.cs rename to Module8/ProgramTree.cs index e48dcb56fcb811d4b2e7f37bbf71e935306aedc6..500e94046a9d2b86cf850b1525f5933157af86bc 100644 --- a/Module8/SimpleLanguage3/ProgramTree.cs +++ b/Module8/ProgramTree.cs @@ -142,4 +142,22 @@ namespace ProgramTree v.VisitVarDefNode(this); } } + + public class IfNode : StatementNode + { + public ExprNode expr; + public StatementNode ifTrue, ifFalse; + + public IfNode(ExprNode expr, StatementNode ifTrue, StatementNode ifFalse = null) + { + this.expr = expr; + this.ifTrue = ifTrue; + this.ifFalse = ifFalse; + } + + public override void Visit(Visitor v) + { + v.VisitIfNode(this); + } + } } \ No newline at end of file diff --git a/Module8/SimpleLanguage3/Properties/AssemblyInfo.cs b/Module8/Properties/AssemblyInfo.cs similarity index 100% rename from Module8/SimpleLanguage3/Properties/AssemblyInfo.cs rename to Module8/Properties/AssemblyInfo.cs diff --git a/Module8/SimpleLanguage3/ShiftReduceParserCode.cs b/Module8/ShiftReduceParserCode.cs similarity index 100% rename from Module8/SimpleLanguage3/ShiftReduceParserCode.cs rename to Module8/ShiftReduceParserCode.cs diff --git a/Module8/SimpleLanguage3/SimpleLang3.sln b/Module8/SimpleLanguage3/SimpleLang3.sln deleted file mode 100644 index 091c760b0a517fc75d752363eb5526c0c67086d7..0000000000000000000000000000000000000000 --- a/Module8/SimpleLanguage3/SimpleLang3.sln +++ /dev/null @@ -1,22 +0,0 @@ -п»ї -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2013 -VisualStudioVersion = 12.0.21005.1 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SimpleLang3", "SimpleLang3.csproj", "{12B9D996-7B4A-4EE4-9AD8-2E24EAF3F574}" -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|x86 = Debug|x86 - Release|x86 = Release|x86 - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {12B9D996-7B4A-4EE4-9AD8-2E24EAF3F574}.Debug|x86.ActiveCfg = Debug|x86 - {12B9D996-7B4A-4EE4-9AD8-2E24EAF3F574}.Debug|x86.Build.0 = Debug|x86 - {12B9D996-7B4A-4EE4-9AD8-2E24EAF3F574}.Release|x86.ActiveCfg = Release|x86 - {12B9D996-7B4A-4EE4-9AD8-2E24EAF3F574}.Release|x86.Build.0 = Release|x86 - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection -EndGlobal diff --git a/Module8/SimpleLanguage3/SimpleYacc.lst b/Module8/SimpleLanguage3/SimpleYacc.lst deleted file mode 100644 index 2de6038dc15cafc28cab3190511bf29097e83980..0000000000000000000000000000000000000000 --- a/Module8/SimpleLanguage3/SimpleYacc.lst +++ /dev/null @@ -1,139 +0,0 @@ - -// ========================================================================== -// GPPG error listing for yacc source file <SimpleYacc.y> -// ========================================================================== -// Version: 1.3.6 -// Machine: SSM -// DateTime: 19.08.2014 13:28:35 -// UserName: Станислав -// ========================================================================== - - -%{ -// ГќГІГЁ îáúÿâëåГГЁГї äîáà âëÿþòñÿ Гў êëà ññ GPPGParser, ïðåäñòà âëÿþùèé ñîáîé ïà ðñåð, ГЈГҐГåðèðóåìûé ñèñòåìîé gppg - public BlockNode root; // ÊîðГåâîé óçåë Г±ГЁГòà êñè÷åñêîãî äåðåâà - public Parser(AbstractScanner<ValueType, LexLocation> scanner) : base(scanner) { } - private bool InDefSect = false; -%} - -%output = SimpleYacc.cs - -%union { - public double dVal; - public int iVal; - public string sVal; - public Node nVal; - public ExprNode eVal; - public StatementNode stVal; - public BlockNode blVal; - } - -%using System.IO; -%using ProgramTree; - -%namespace SimpleParser - -%start progr - -%token BEGIN END CYCLE ASSIGN ASSIGNPLUS ASSIGNMINUS ASSIGNMULT SEMICOLON WRITE VAR PLUS MINUS MULT DIV LPAREN RPAREN COLUMN -%token <iVal> INUM -%token <dVal> RNUM -%token <sVal> ID - -%type <nVal> varlist -%type <eVal> expr ident T F -%type <stVal> statement assign block cycle write empty var -%type <blVal> stlist block - -%% -// Error: NonTerminal symbol "st" has no productions -// Warning: Terminating st fixes the following size-2 NonTerminal set - // {cycle, st} -// Error: There are 2 non-terminating NonTerminal Symbols - // {cycle, st} -// ------------------------------------------------------------------ - -progr : block { root = $1; } - ; - -stlist : statement - { - $$ = new BlockNode($1); - } - | stlist SEMICOLON statement - { - $1.Add($3); - $$ = $1; - } - ; - -statement: assign { $$ = $1; } - | block { $$ = $1; } - | cycle { $$ = $1; } - | write { $$ = $1; } - | var { $$ = $1; } - | empty { $$ = $1; } - ; - -empty : { $$ = new EmptyNode(); } - ; - -ident : ID - { - if (!InDefSect) - if (!SymbolTable.vars.ContainsKey($1)) - throw new Exception("("+@1.StartLine+","+@1.StartColumn+"): ÏåðåìåГГГ Гї "+$1+" ГГҐ îïèñà ГГ "); - $$ = new IdNode($1); - } - ; - -assign : ident ASSIGN expr { $$ = new AssignNode($1 as IdNode, $3); } - ; - -expr : expr PLUS T { $$ = new BinOpNode($1,$3,'+'); } - | expr MINUS T { $$ = new BinOpNode($1,$3,'-'); } - | T { $$ = $1; } - ; - -T : T MULT F { $$ = new BinOpNode($1,$3,'*'); } - | T DIV F { $$ = new BinOpNode($1,$3,'/'); } - | F { $$ = $1; } - ; - -F : ident { $$ = $1 as IdNode; } - | INUM { $$ = new IntNumNode($1); } - | LPAREN expr RPAREN { $$ = $2; } - ; - -block : BEGIN stlist END { $$ = $2; } - ; - -cycle : CYCLE expr st { $$ = new CycleNode($2,$3); } - ; - -write : WRITE LPAREN expr RPAREN { $$ = new WriteNode($3); } - ; - -var : VAR { InDefSect = true; } varlist - { - foreach (var v in ($3 as VarDefNode).vars) - SymbolTable.NewVarDef(v.Name, type.tint); - InDefSect = false; - } - ; - -varlist : ident - { - $$ = new VarDefNode($1 as IdNode); - } - | varlist COLUMN ident - { - ($1 as VarDefNode).Add($3 as IdNode); - $$ = $1; - } - ; - -%% - -// ========================================================================== - diff --git a/Module8/SimpleLanguage3/Visitors/AssignCountVisitor.cs b/Module8/SimpleLanguage3/Visitors/AssignCountVisitor.cs deleted file mode 100644 index 43d8554f9a164a5ec9416922969dbb3970d52002..0000000000000000000000000000000000000000 --- a/Module8/SimpleLanguage3/Visitors/AssignCountVisitor.cs +++ /dev/null @@ -1,23 +0,0 @@ -п»їusing System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using ProgramTree; - -namespace SimpleLang.Visitors -{ - class AssignCountVisitor : AutoVisitor - { - public int Count = 0; - public override void VisitAssignNode(AssignNode a) - { - Count += 1; - } - public override void VisitWriteNode(WriteNode w) - { - } - public override void VisitVarDefNode(VarDefNode w) - { - } - } -} diff --git a/Module8/SimpleLanguage3/a.txt b/Module8/SimpleLanguage3/a.txt deleted file mode 100644 index 14eeab4e71d36f47bc653b39285e15b71ec75311..0000000000000000000000000000000000000000 --- a/Module8/SimpleLanguage3/a.txt +++ /dev/null @@ -1,11 +0,0 @@ -begin - var a,b,d; - b := 2; - a := 3; - a := a * 4 + b;;; - cycle 3 - begin - a := a + 1; - write(a) - end -end diff --git a/Module7/SimpleLanguage2/SimpleLex.cs b/Module8/SimpleLex.cs similarity index 93% rename from Module7/SimpleLanguage2/SimpleLex.cs rename to Module8/SimpleLex.cs index 3658b77feb1424c450237fcd33b4482d9a22105a..124489e2d753e8ffee406427501b40410d8779db 100644 --- a/Module7/SimpleLanguage2/SimpleLex.cs +++ b/Module8/SimpleLex.cs @@ -1,10 +1,14 @@ // // This CSharp output file generated by Gardens Point LEX -// Version: 1.1.3.301 -// Machine: SSM -// DateTime: 19.08.2014 13:38:37 -// UserName: ????????? -// GPLEX input file <SimpleLex.lex> +// Gardens Point LEX (GPLEX) is Copyright (c) John Gough, QUT 2006-2014. +// Output produced by GPLEX is the property of the user. +// See accompanying file GPLEXcopyright.rtf. +// +// GPLEX Version: 1.2.2 +// Machine: MAINHOMEPC2 +// DateTime: 30.09.2018 18:23:21 +// UserName: someone +// GPLEX input file <SimpleLex.lex - 30.09.2018 18:22:46> // GPLEX frame file <embedded resource> // // Option settings: unicode, parser, minimize @@ -13,8 +17,8 @@ // // -// Experimental embedded frame -// Version 1.1.3 of 18-April-2010 +// Revised backup code +// Version 1.2.1 of 24-June-2013 // // #define BACKUP @@ -301,8 +305,7 @@ int NextState() { public Scanner(Stream file, string codepage) { SetSource(file, CodePageHandling.GetCodePage(codepage)); - } - + } #endif // !NOFILES public Scanner() { } @@ -330,7 +333,7 @@ int NextState() { if (next < 0xDC00 || next > 0xDFFF) code = ScanBuff.UnicodeReplacementChar; else - code = (0x10000 + (code & 0x3FF << 10) + (next & 0x3FF)); + code = (0x10000 + ((code & 0x3FF) << 10) + (next & 0x3FF)); } #endif cCol++; @@ -383,9 +386,7 @@ int NextState() { GetCode(); } -#if !NOFILES // ================ LineBuffer Initialization =================== - /// <summary> /// Create and initialize a LineBuff buffer object for this scanner /// </summary> @@ -399,6 +400,7 @@ int NextState() { GetCode(); } +#if !NOFILES // =============== StreamBuffer Initialization ================== /// <summary> @@ -500,6 +502,12 @@ int NextState() { } } + /// <summary> + /// Discards all but the first "n" codepoints in the recognized pattern. + /// Resets the buffer position so that only n codepoints have been consumed; + /// yytext is also re-evaluated. + /// </summary> + /// <param name="n">The number of codepoints to consume</param> [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] void yyless(int n) { @@ -519,6 +527,10 @@ int NextState() { // but it does not seem possible to re-establish // the correct column counts except by going forward. // + /// <summary> + /// Removes the last "n" code points from the pattern. + /// </summary> + /// <param name="n">The number to remove</param> [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] void _yytrunc(int n) { yyless(yyleng - n); } @@ -531,18 +543,23 @@ int NextState() { // can't use (tokEPos - tokPos) because of the // possibility of surrogate pairs in the token. // + /// <summary> + /// The length of the pattern in codepoints (not the same as + /// string-length if the pattern contains any surrogate pairs). + /// </summary> [SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] [SuppressMessage("Microsoft.Naming", "CA1709:IdentifiersShouldBeCasedCorrectly", MessageId = "yyleng")] [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "yyleng")] public int yyleng { get { -#if BYTEMODE - return tokEPos - tokPos; -#else if (tokELin == tokLin) return tokECol - tokCol; - else { + else +#if BYTEMODE + return tokEPos - tokPos; +#else + { int ch; int count = 0; int save = buffer.Pos; @@ -551,7 +568,7 @@ int NextState() { ch = buffer.Read(); if (!char.IsHighSurrogate((char)ch)) count++; } while (buffer.Pos < tokEPos && ch != ScanBuff.EndOfFile); - buffer.Pos = save; + buffer.Pos = save; return count; } #endif // BYTEMODE @@ -576,65 +593,56 @@ int NextState() { // ============== The main tokenizer code ================= - int Scan() - { + int Scan() { try { - for (; ; ) - { - int next; // next state to enter -#if BACKUP - Result rslt = Result.noMatch; -#endif // BACKUP + for (; ; ) { + int next; // next state to enter #if LEFTANCHORS - for (;;) - { + for (;;) { // Discard characters that do not start any pattern. // Must check the left anchor condition after *every* GetCode! state = ((cCol == 0) ? anchorState[currentScOrd] : currentStart); - if ((next = NextState()) != goStart) - break; // LOOP EXIT HERE... + if ((next = NextState()) != goStart) break; // LOOP EXIT HERE... GetCode(); } #else // !LEFTANCHORS state = currentStart; - while ((next = NextState()) == goStart) + while ((next = NextState()) == goStart) { // At this point, the current character has no // transition from the current state. We discard // the "no-match" char. In traditional LEX such // characters are echoed to the console. GetCode(); + } #endif // LEFTANCHORS // At last, a valid transition ... MarkToken(); state = next; - GetCode(); - - while ((next = NextState()) > eofNum) // Exit for goStart AND for eofNum + GetCode(); #if BACKUP - if (state <= maxAccept && next > maxAccept) // need to prepare backup data - { - // ctx is an object. The fields may be - // mutated by the call to Recurse2. - // On return the data in ctx is the - // *latest* accept state that was found. - - rslt = Recurse2(ref ctx, next); - if (rslt == Result.noMatch) - RestoreStateAndPos(ref ctx); - break; + bool contextSaved = false; + while ((next = NextState()) > eofNum) { // Exit for goStart AND for eofNum + if (state <= maxAccept && next > maxAccept) { // need to prepare backup data + // Store data for the *latest* accept state that was found. + SaveStateAndPos( ref ctx ); + contextSaved = true; } - else + state = next; + GetCode(); + } + if (state > maxAccept && contextSaved) + RestoreStateAndPos( ref ctx ); +#else // BACKUP + while ((next = NextState()) > eofNum) { // Exit for goStart AND for eofNum + state = next; + GetCode(); + } #endif // BACKUP - { - state = next; - GetCode(); - } - if (state <= maxAccept) - { + if (state <= maxAccept) { MarkEnd(); #region ActionSwitch -#pragma warning disable 162 +#pragma warning disable 162, 1522 switch (state) { case eofNum: @@ -698,7 +706,7 @@ yylval.dVal = double.Parse(yytext); default: break; } -#pragma warning restore 162 +#pragma warning restore 162, 1522 #endregion } } @@ -711,29 +719,7 @@ yylloc = new LexLocation(tokLin, tokCol, tokELin, tokECol); } #if BACKUP - Result Recurse2(ref Context ctx, int next) - { - // Assert: at entry "state" is an accept state AND - // NextState(state, code) != goStart AND - // NextState(state, code) is not an accept state. - // - SaveStateAndPos(ref ctx); - state = next; - GetCode(); - - while ((next = NextState()) > eofNum) - { - if (state <= maxAccept && next > maxAccept) // need to update backup data - SaveStateAndPos(ref ctx); - state = next; - if (state == eofNum) return Result.accept; - GetCode(); - } - return (state <= maxAccept ? Result.accept : Result.noMatch); - } - - void SaveStateAndPos(ref Context ctx) - { + void SaveStateAndPos(ref Context ctx) { ctx.bPos = buffer.Pos; ctx.rPos = readPos; ctx.cCol = cCol; @@ -742,8 +728,7 @@ yylloc = new LexLocation(tokLin, tokCol, tokELin, tokECol); ctx.cChr = code; } - void RestoreStateAndPos(ref Context ctx) - { + void RestoreStateAndPos(ref Context ctx) { buffer.Pos = ctx.bPos; readPos = ctx.rPos; cCol = ctx.cCol; @@ -751,8 +736,7 @@ yylloc = new LexLocation(tokLin, tokCol, tokELin, tokECol); state = ctx.state; code = ctx.cChr; } - -#endif // BACKUP +#endif // BACKUP // ============= End of the tokenizer code ================ @@ -784,16 +768,16 @@ yylloc = new LexLocation(tokLin, tokCol, tokELin, tokECol); #region UserCodeSection -public override void yyerror(string format, params object[] args) // обработка синтаксических ошибок +public override void yyerror(string format, params object[] args) // îáðà áîòêà ñèГГІГ ГЄГ±ГЁГ·ГҐГ±ГЄГЁГµ îøèáîê { var ww = args.Skip(1).Cast<string>().ToArray(); - string errorMsg = string.Format("({0},{1}): Встречено {2}, а ожидалось {3}", yyline, yycol, args[0], string.Join(" или ", ww)); + string errorMsg = string.Format("({0},{1}): Âñòðå÷åГГ® {2}, à îæèäà ëîñü {3}", yyline, yycol, args[0], string.Join(" èëè ", ww)); throw new SyntaxException(errorMsg); } public void LexError() { - string errorMsg = string.Format("({0},{1}): Неизвестный символ {2}", yyline, yycol, yytext); + string errorMsg = string.Format("({0},{1}): ÍåèçâåñòГûé ñèìâîë {2}", yyline, yycol, yytext); throw new LexException(errorMsg); } @@ -869,6 +853,7 @@ class ScannerHelper return new LineBuffer(source); } +#if (!NOFILES) public static ScanBuff GetBuffer(Stream source) { return new BuildBuffer(source); @@ -879,7 +864,8 @@ class ScannerHelper { return new BuildBuffer(source, fallbackCodePage); } -#endif +#endif // !BYTEMODE +#endif // !NOFILES } #region Buffer classes @@ -1002,7 +988,7 @@ class ScannerHelper { ix = lstart = 0; } - for (; ; ) + while (ix < numLines) { int len = line[ix].Length + 1; if (pos < lstart + len) break; @@ -1060,7 +1046,8 @@ class ScannerHelper { cPos = value; findIndex(cPos, out cLine, out curLineStart); - curLine = line[cLine]; + // cLine should be the *next* line after curLine. + curLine = (cLine < numLines ? line[cLine++] : ""); curLineEnd = curLineStart + curLine.Length; } } @@ -1068,7 +1055,7 @@ class ScannerHelper public override string ToString() { return "LineBuffer"; } } - +#if (!NOFILES) // ============================================================== // ===== class BuildBuff : for unicode text files ======== // ============================================================== @@ -1309,12 +1296,13 @@ class ScannerHelper } #endif // !BYTEMODE } +#endif // !NOFILES #endregion Buffer classes // ============================================================== // ============ class CodePageHandling ============= // ============================================================== - +#if (!NOFILES) public static class CodePageHandling { public static int GetCodePage(string option) @@ -1525,6 +1513,7 @@ class ScannerHelper #endif // !BYTEMODE #endregion +#endif // !NOFILES // End of code copied from embedded resource diff --git a/Module8/SimpleLanguage3/SimpleLex.lex b/Module8/SimpleLex.lex similarity index 100% rename from Module8/SimpleLanguage3/SimpleLex.lex rename to Module8/SimpleLex.lex diff --git a/Module8/SimpleLanguage3/SimpleYacc.cs b/Module8/SimpleYacc.cs similarity index 64% rename from Module8/SimpleLanguage3/SimpleYacc.cs rename to Module8/SimpleYacc.cs index fa0d511b9531586610c42c261758510a6d23d3a8..41990d1991abc1e5c84a48ae96c6d9f5381c4264 100644 --- a/Module8/SimpleLanguage3/SimpleYacc.cs +++ b/Module8/SimpleYacc.cs @@ -1,17 +1,18 @@ // This code was generated by the Gardens Point Parser Generator -// Copyright (c) Wayne Kelly, QUT 2005-2010 +// Copyright (c) Wayne Kelly, John Gough, QUT 2005-2014 // (see accompanying GPPGcopyright.rtf) -// GPPG version 1.3.6 -// Machine: HUB -// DateTime: 21.09.2017 21:02:18 +// GPPG version 1.5.2 +// Machine: MAINHOMEPC2 +// DateTime: 30.09.2018 18:23:23 // UserName: someone -// Input file <SimpleYacc.y> +// Input file <SimpleYacc.y - 30.09.2018 18:22:38> // options: no-lines gplex using System; using System.Collections.Generic; +using System.CodeDom.Compiler; using System.Globalization; using System.Text; using QUT.Gppg; @@ -20,11 +21,10 @@ using ProgramTree; namespace SimpleParser { -public enum Tokens { - error=1,EOF=2,BEGIN=3,END=4,CYCLE=5,ASSIGN=6, - ASSIGNPLUS=7,ASSIGNMINUS=8,ASSIGNMULT=9,SEMICOLON=10,WRITE=11,VAR=12, - PLUS=13,MINUS=14,MULT=15,DIV=16,LPAREN=17,RPAREN=18, - COLUMN=19,INUM=20,RNUM=21,ID=22}; +public enum Tokens {error=2,EOF=3,BEGIN=4,END=5,CYCLE=6, + ASSIGN=7,ASSIGNPLUS=8,ASSIGNMINUS=9,ASSIGNMULT=10,SEMICOLON=11,WRITE=12, + VAR=13,PLUS=14,MINUS=15,MULT=16,DIV=17,LPAREN=18, + RPAREN=19,COLUMN=20,INUM=21,RNUM=22,ID=23}; public struct ValueType { @@ -37,23 +37,36 @@ public struct ValueType public BlockNode blVal; } // Abstract base class for GPLEX scanners +[GeneratedCodeAttribute( "Gardens Point Parser Generator", "1.5.2")] public abstract class ScanBase : AbstractScanner<ValueType,LexLocation> { private LexLocation __yylloc = new LexLocation(); public override LexLocation yylloc { get { return __yylloc; } set { __yylloc = value; } } protected virtual bool yywrap() { return true; } } +// Utility class for encapsulating token information +[GeneratedCodeAttribute( "Gardens Point Parser Generator", "1.5.2")] +public class ScanObj { + public int token; + public ValueType yylval; + public LexLocation yylloc; + public ScanObj( int t, ValueType val, LexLocation loc ) { + this.token = t; this.yylval = val; this.yylloc = loc; + } +} + +[GeneratedCodeAttribute( "Gardens Point Parser Generator", "1.5.2")] public class Parser: ShiftReduceParser<ValueType, LexLocation> { - // Verbatim content from SimpleYacc.y -// Эти объявления добавляются в класс GPPGParser, представляющий собой парсер, генерируемый системой gppg - public BlockNode root; // Корневой узел синтаксического дерева + // Verbatim content from SimpleYacc.y - 30.09.2018 18:22:38 +// ГќГІГЁ îáúÿâëåГГЁГї äîáà âëÿþòñÿ Гў êëà ññ GPPGParser, ïðåäñòà âëÿþùèé ñîáîé ïà ðñåð, ГЈГҐГåðèðóåìûé ñèñòåìîé gppg + public BlockNode root; // ÊîðГåâîé óçåë Г±ГЁГòà êñè÷åñêîãî äåðåâà public Parser(AbstractScanner<ValueType, LexLocation> scanner) : base(scanner) { } private bool InDefSect = false; - // End verbatim content from SimpleYacc.y + // End verbatim content from SimpleYacc.y - 30.09.2018 18:22:38 #pragma warning disable 649 - private static Dictionary<int, string> aliasses; + private static Dictionary<int, string> aliases; #pragma warning restore 649 private static Rule[] rules = new Rule[30]; private static State[] states = new State[48]; @@ -62,59 +75,61 @@ public class Parser: ShiftReduceParser<ValueType, LexLocation> "write", "empty", "var", "varlist", "stlist", "$accept", "Anon@1", }; static Parser() { - states[0] = new State(new int[]{3,4},new int[]{-1,1,-8,3}); - states[1] = new State(new int[]{2,2}); + states[0] = new State(new int[]{4,4},new int[]{-1,1,-8,3}); + states[1] = new State(new int[]{3,2}); states[2] = new State(-1); states[3] = new State(-2); - states[4] = new State(new int[]{22,18,3,4,5,31,11,35,12,40,4,-11,10,-11},new int[]{-14,5,-6,47,-7,9,-3,10,-8,29,-9,30,-10,34,-12,39,-11,46}); - states[5] = new State(new int[]{4,6,10,7}); + states[4] = new State(new int[]{23,18,4,4,6,31,12,35,13,40,5,-11,11,-11},new int[]{-14,5,-6,47,-7,9,-3,10,-8,29,-9,30,-10,34,-12,39,-11,46}); + states[5] = new State(new int[]{5,6,11,7}); states[6] = new State(-23); - states[7] = new State(new int[]{22,18,3,4,5,31,11,35,12,40,4,-11,10,-11},new int[]{-6,8,-7,9,-3,10,-8,29,-9,30,-10,34,-12,39,-11,46}); + states[7] = new State(new int[]{23,18,4,4,6,31,12,35,13,40,5,-11,11,-11},new int[]{-6,8,-7,9,-3,10,-8,29,-9,30,-10,34,-12,39,-11,46}); states[8] = new State(-4); states[9] = new State(-5); - states[10] = new State(new int[]{6,11}); - states[11] = new State(new int[]{22,18,20,19,17,20},new int[]{-2,12,-4,28,-5,27,-3,17}); - states[12] = new State(new int[]{13,13,14,23,4,-13,10,-13}); - states[13] = new State(new int[]{22,18,20,19,17,20},new int[]{-4,14,-5,27,-3,17}); - states[14] = new State(new int[]{15,15,16,25,13,-14,14,-14,4,-14,10,-14,18,-14,22,-14,3,-14,5,-14,11,-14,12,-14}); - states[15] = new State(new int[]{22,18,20,19,17,20},new int[]{-5,16,-3,17}); + states[10] = new State(new int[]{7,11}); + states[11] = new State(new int[]{23,18,21,19,18,20},new int[]{-2,12,-4,28,-5,27,-3,17}); + states[12] = new State(new int[]{14,13,15,23,5,-13,11,-13}); + states[13] = new State(new int[]{23,18,21,19,18,20},new int[]{-4,14,-5,27,-3,17}); + states[14] = new State(new int[]{16,15,17,25,14,-14,15,-14,5,-14,11,-14,19,-14,23,-14,4,-14,6,-14,12,-14,13,-14}); + states[15] = new State(new int[]{23,18,21,19,18,20},new int[]{-5,16,-3,17}); states[16] = new State(-17); states[17] = new State(-20); states[18] = new State(-12); states[19] = new State(-21); - states[20] = new State(new int[]{22,18,20,19,17,20},new int[]{-2,21,-4,28,-5,27,-3,17}); - states[21] = new State(new int[]{18,22,13,13,14,23}); + states[20] = new State(new int[]{23,18,21,19,18,20},new int[]{-2,21,-4,28,-5,27,-3,17}); + states[21] = new State(new int[]{19,22,14,13,15,23}); states[22] = new State(-22); - states[23] = new State(new int[]{22,18,20,19,17,20},new int[]{-4,24,-5,27,-3,17}); - states[24] = new State(new int[]{15,15,16,25,13,-15,14,-15,4,-15,10,-15,18,-15,22,-15,3,-15,5,-15,11,-15,12,-15}); - states[25] = new State(new int[]{22,18,20,19,17,20},new int[]{-5,26,-3,17}); + states[23] = new State(new int[]{23,18,21,19,18,20},new int[]{-4,24,-5,27,-3,17}); + states[24] = new State(new int[]{16,15,17,25,14,-15,15,-15,5,-15,11,-15,19,-15,23,-15,4,-15,6,-15,12,-15,13,-15}); + states[25] = new State(new int[]{23,18,21,19,18,20},new int[]{-5,26,-3,17}); states[26] = new State(-18); states[27] = new State(-19); - states[28] = new State(new int[]{15,15,16,25,13,-16,14,-16,4,-16,10,-16,18,-16,22,-16,3,-16,5,-16,11,-16,12,-16}); + states[28] = new State(new int[]{16,15,17,25,14,-16,15,-16,5,-16,11,-16,19,-16,23,-16,4,-16,6,-16,12,-16,13,-16}); states[29] = new State(-6); states[30] = new State(-7); - states[31] = new State(new int[]{22,18,20,19,17,20},new int[]{-2,32,-4,28,-5,27,-3,17}); - states[32] = new State(new int[]{13,13,14,23,22,18,3,4,5,31,11,35,12,40,4,-11,10,-11},new int[]{-6,33,-7,9,-3,10,-8,29,-9,30,-10,34,-12,39,-11,46}); + states[31] = new State(new int[]{23,18,21,19,18,20},new int[]{-2,32,-4,28,-5,27,-3,17}); + states[32] = new State(new int[]{14,13,15,23,23,18,4,4,6,31,12,35,13,40,5,-11,11,-11},new int[]{-6,33,-7,9,-3,10,-8,29,-9,30,-10,34,-12,39,-11,46}); states[33] = new State(-24); states[34] = new State(-8); - states[35] = new State(new int[]{17,36}); - states[36] = new State(new int[]{22,18,20,19,17,20},new int[]{-2,37,-4,28,-5,27,-3,17}); - states[37] = new State(new int[]{18,38,13,13,14,23}); + states[35] = new State(new int[]{18,36}); + states[36] = new State(new int[]{23,18,21,19,18,20},new int[]{-2,37,-4,28,-5,27,-3,17}); + states[37] = new State(new int[]{19,38,14,13,15,23}); states[38] = new State(-25); states[39] = new State(-9); states[40] = new State(-26,new int[]{-16,41}); - states[41] = new State(new int[]{22,18},new int[]{-13,42,-3,45}); - states[42] = new State(new int[]{19,43,4,-27,10,-27}); - states[43] = new State(new int[]{22,18},new int[]{-3,44}); + states[41] = new State(new int[]{23,18},new int[]{-13,42,-3,45}); + states[42] = new State(new int[]{20,43,5,-27,11,-27}); + states[43] = new State(new int[]{23,18},new int[]{-3,44}); states[44] = new State(-29); states[45] = new State(-28); states[46] = new State(-10); states[47] = new State(-3); - rules[1] = new Rule(-15, new int[]{-1,2}); + for (int sNo = 0; sNo < states.Length; sNo++) states[sNo].number = sNo; + + rules[1] = new Rule(-15, new int[]{-1,3}); rules[2] = new Rule(-1, new int[]{-8}); rules[3] = new Rule(-14, new int[]{-6}); - rules[4] = new Rule(-14, new int[]{-14,10,-6}); + rules[4] = new Rule(-14, new int[]{-14,11,-6}); rules[5] = new Rule(-6, new int[]{-7}); rules[6] = new Rule(-6, new int[]{-8}); rules[7] = new Rule(-6, new int[]{-9}); @@ -122,24 +137,24 @@ public class Parser: ShiftReduceParser<ValueType, LexLocation> rules[9] = new Rule(-6, new int[]{-12}); rules[10] = new Rule(-6, new int[]{-11}); rules[11] = new Rule(-11, new int[]{}); - rules[12] = new Rule(-3, new int[]{22}); - rules[13] = new Rule(-7, new int[]{-3,6,-2}); - rules[14] = new Rule(-2, new int[]{-2,13,-4}); - rules[15] = new Rule(-2, new int[]{-2,14,-4}); + rules[12] = new Rule(-3, new int[]{23}); + rules[13] = new Rule(-7, new int[]{-3,7,-2}); + rules[14] = new Rule(-2, new int[]{-2,14,-4}); + rules[15] = new Rule(-2, new int[]{-2,15,-4}); rules[16] = new Rule(-2, new int[]{-4}); - rules[17] = new Rule(-4, new int[]{-4,15,-5}); - rules[18] = new Rule(-4, new int[]{-4,16,-5}); + rules[17] = new Rule(-4, new int[]{-4,16,-5}); + rules[18] = new Rule(-4, new int[]{-4,17,-5}); rules[19] = new Rule(-4, new int[]{-5}); rules[20] = new Rule(-5, new int[]{-3}); - rules[21] = new Rule(-5, new int[]{20}); - rules[22] = new Rule(-5, new int[]{17,-2,18}); - rules[23] = new Rule(-8, new int[]{3,-14,4}); - rules[24] = new Rule(-9, new int[]{5,-2,-6}); - rules[25] = new Rule(-10, new int[]{11,17,-2,18}); + rules[21] = new Rule(-5, new int[]{21}); + rules[22] = new Rule(-5, new int[]{18,-2,19}); + rules[23] = new Rule(-8, new int[]{4,-14,5}); + rules[24] = new Rule(-9, new int[]{6,-2,-6}); + rules[25] = new Rule(-10, new int[]{12,18,-2,19}); rules[26] = new Rule(-16, new int[]{}); - rules[27] = new Rule(-12, new int[]{12,-16,-13}); + rules[27] = new Rule(-12, new int[]{13,-16,-13}); rules[28] = new Rule(-13, new int[]{-3}); - rules[29] = new Rule(-13, new int[]{-13,19,-3}); + rules[29] = new Rule(-13, new int[]{-13,20,-3}); } protected override void Initialize() { @@ -151,6 +166,7 @@ public class Parser: ShiftReduceParser<ValueType, LexLocation> protected override void DoAction(int action) { +#pragma warning disable 162, 1522 switch (action) { case 2: // progr -> block @@ -192,7 +208,7 @@ public class Parser: ShiftReduceParser<ValueType, LexLocation> { if (!InDefSect) if (!SymbolTable.vars.ContainsKey(ValueStack[ValueStack.Depth-1].sVal)) - throw new Exception("("+LocationStack[LocationStack.Depth-1].StartLine+","+LocationStack[LocationStack.Depth-1].StartColumn+"): Переменная "+ValueStack[ValueStack.Depth-1].sVal+" не описана"); + throw new Exception("("+LocationStack[LocationStack.Depth-1].StartLine+","+LocationStack[LocationStack.Depth-1].StartColumn+"): ÏåðåìåГГГ Гї "+ValueStack[ValueStack.Depth-1].sVal+" ГГҐ îïèñà ГГ "); CurrentSemanticValue.eVal = new IdNode(ValueStack[ValueStack.Depth-1].sVal); } break; @@ -257,18 +273,18 @@ public class Parser: ShiftReduceParser<ValueType, LexLocation> } break; } +#pragma warning restore 162, 1522 } protected override string TerminalToString(int terminal) { - if (aliasses != null && aliasses.ContainsKey(terminal)) - return aliasses[terminal]; + if (aliases != null && aliases.ContainsKey(terminal)) + return aliases[terminal]; else if (((Tokens)terminal).ToString() != terminal.ToString(CultureInfo.InvariantCulture)) return ((Tokens)terminal).ToString(); else return CharToString((char)terminal); } - } } diff --git a/Module7/SimpleLanguage2/SimpleYacc.lst b/Module8/SimpleYacc.lst similarity index 100% rename from Module7/SimpleLanguage2/SimpleYacc.lst rename to Module8/SimpleYacc.lst diff --git a/Module8/SimpleLanguage3/SimpleYacc.y b/Module8/SimpleYacc.y similarity index 100% rename from Module8/SimpleLanguage3/SimpleYacc.y rename to Module8/SimpleYacc.y diff --git a/Module7/SimpleLanguage2/Visitors/AssignCountVisitor.cs b/Module8/Visitors/AssignCountVisitor.cs similarity index 100% rename from Module7/SimpleLanguage2/Visitors/AssignCountVisitor.cs rename to Module8/Visitors/AssignCountVisitor.cs diff --git a/Module7/SimpleLanguage2/Visitors/AutoVisitor.cs b/Module8/Visitors/AutoVisitor.cs similarity index 86% rename from Module7/SimpleLanguage2/Visitors/AutoVisitor.cs rename to Module8/Visitors/AutoVisitor.cs index 7c92bab8a5812171a386e299bcf6fc32c3cde49a..5d508770fc5449f20e1d15859b78725d4e64af48 100644 --- a/Module7/SimpleLanguage2/Visitors/AutoVisitor.cs +++ b/Module8/Visitors/AutoVisitor.cs @@ -41,5 +41,14 @@ namespace SimpleLang.Visitors foreach (var v in w.vars) v.Visit(this); } + public override void VisitIfNode(IfNode cond) + { + cond.expr.Visit(this); + cond.ifTrue.Visit(this); + if (cond.ifFalse != null) + { + cond.ifFalse.Visit(this); + } + } } } diff --git a/Module8/SimpleLanguage3/Visitors/GenCodeVisitors/GenCodeCreator.cs b/Module8/Visitors/GenCodeVisitors/GenCodeCreator.cs similarity index 100% rename from Module8/SimpleLanguage3/Visitors/GenCodeVisitors/GenCodeCreator.cs rename to Module8/Visitors/GenCodeVisitors/GenCodeCreator.cs diff --git a/Module8/SimpleLanguage3/Visitors/GenCodeVisitors/GenCodeVisitor.cs b/Module8/Visitors/GenCodeVisitors/GenCodeVisitor.cs similarity index 98% rename from Module8/SimpleLanguage3/Visitors/GenCodeVisitors/GenCodeVisitor.cs rename to Module8/Visitors/GenCodeVisitors/GenCodeVisitor.cs index 58959faa0645a1d1d3bebc94d00795a3f0c19eef..88891ad31e66280ab20cdf988e42cb6e1390af22 100644 --- a/Module8/SimpleLanguage3/Visitors/GenCodeVisitors/GenCodeVisitor.cs +++ b/Module8/Visitors/GenCodeVisitors/GenCodeVisitor.cs @@ -7,7 +7,7 @@ using System.Reflection.Emit; namespace SimpleLang.Visitors { - class GenCodeVisitor: Visitor + public class GenCodeVisitor: Visitor { private Dictionary<string, LocalBuilder> vars = new Dictionary<string, LocalBuilder>(); private GenCodeCreator genc; diff --git a/Module8/SimpleLanguage3/Visitors/PrettyPrintVisitor.cs b/Module8/Visitors/PrettyPrintVisitor.cs similarity index 79% rename from Module8/SimpleLanguage3/Visitors/PrettyPrintVisitor.cs rename to Module8/Visitors/PrettyPrintVisitor.cs index 87892b1214d751d13640e7adf61bd276ef870904..3d15de39959c545a460a796bde688a34245d45b0 100644 --- a/Module8/SimpleLanguage3/Visitors/PrettyPrintVisitor.cs +++ b/Module8/Visitors/PrettyPrintVisitor.cs @@ -84,5 +84,24 @@ namespace SimpleLang.Visitors for (int i = 1; i < w.vars.Count; i++) Text += ',' + w.vars[i].Name; } + public override void VisitIfNode(IfNode cond) + { + Text += IndentStr() + "if "; + cond.expr.Visit(this); + Text += " then "; + Text += Environment.NewLine; + IndentPlus(); + cond.ifTrue.Visit(this); + IndentMinus(); + if (null != cond.ifFalse) + { + Text += Environment.NewLine; + Text += IndentStr() + "else "; + Text += Environment.NewLine; + IndentPlus(); + cond.ifFalse.Visit(this); + IndentMinus(); + } + } } } diff --git a/Module8/SimpleLanguage3/Visitors/Visitor.cs b/Module8/Visitors/Visitor.cs similarity index 92% rename from Module8/SimpleLanguage3/Visitors/Visitor.cs rename to Module8/Visitors/Visitor.cs index b9dcae028c1c74893e7bf551cd0feccf5bcbf0c9..5fc65fcae1f33553c60fe2590eba8e3f086a85ca 100644 --- a/Module8/SimpleLanguage3/Visitors/Visitor.cs +++ b/Module8/Visitors/Visitor.cs @@ -17,5 +17,6 @@ namespace SimpleLang.Visitors public virtual void VisitWriteNode(WriteNode w) { } public virtual void VisitVarDefNode(VarDefNode w) { } public virtual void VisitEmptyNode(EmptyNode w) { } + public virtual void VisitIfNode(IfNode cond) { } } } diff --git a/Module8/a.txt b/Module8/a.txt new file mode 100644 index 0000000000000000000000000000000000000000..94c60d8155c0fa67c790ae4970973d7e10cb0c3f --- /dev/null +++ b/Module8/a.txt @@ -0,0 +1,21 @@ +begin + var a,b,c,d; + b := 14 / 5; write(b); + c := 14 % 5; write(c); + a := 3; + a := a * 4 + b; + cycle 3 + begin + a := a + 1; + write(a) + end; + b := 0; + a := 1; + if a then b := 1; + if a then c := 1 else c := 0; + a := 0; + if a then d := 0 else d := 1; + write(b); + write(c); + write(d); +end diff --git a/Module8/SimpleLanguage3/generateParserScanner.bat b/Module8/generateParserScanner.bat similarity index 100% rename from Module8/SimpleLanguage3/generateParserScanner.bat rename to Module8/generateParserScanner.bat diff --git a/README.md b/README.md index 1a6caae8b8ef3db0cfef99d6ea2d663a9d9e853f..a624bdfbcdd6a819f13a6ae6b997f73bbb1edb70 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,11 @@ +### Как использовать этот репозиторий + +git clone --recurse-submodules <репозиторий> + +### Как запустить тесты РёР· репозитория + +[Рнструкция](https://github.com/czen/MMCS_CS311/wiki/Как-запустить-тесты-РёР·-репозитория) + ### Учебная карта дисциплины [РЎРј. страничку РІ Moodle](http://edu.mmcs.sfedu.ru/course/view.php?id=194) diff --git a/TestASTParser/Properties/AssemblyInfo.cs b/TestASTParser/Properties/AssemblyInfo.cs new file mode 100644 index 0000000000000000000000000000000000000000..1cbec02c9a268de880c2c3533e3a924df18a3b76 --- /dev/null +++ b/TestASTParser/Properties/AssemblyInfo.cs @@ -0,0 +1,35 @@ +п»їusing System.Reflection; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("TestASTParser")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("TestASTParser")] +[assembly: AssemblyCopyright("Copyright В© 2018")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("71D0BFF1-8F45-450E-A78F-153391124637")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] \ No newline at end of file diff --git a/TestASTParser/TestASTParser.csproj b/TestASTParser/TestASTParser.csproj new file mode 100644 index 0000000000000000000000000000000000000000..bf58456ffe78c32f6cec83b0c0d62ee64cb372cd --- /dev/null +++ b/TestASTParser/TestASTParser.csproj @@ -0,0 +1,76 @@ +п»ї<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <Import Project="..\packages\NUnit.3.10.1\build\NUnit.props" Condition="Exists('..\packages\NUnit.3.10.1\build\NUnit.props')" /> + <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <ProjectGuid>{71D0BFF1-8F45-450E-A78F-153391124637}</ProjectGuid> + <ProjectTypeGuids>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> + <OutputType>Library</OutputType> + <AppDesignerFolder>Properties</AppDesignerFolder> + <RootNamespace>TestASTParser</RootNamespace> + <AssemblyName>TestASTParser</AssemblyName> + <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> + <FileAlignment>512</FileAlignment> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> + <PlatformTarget>AnyCPU</PlatformTarget> + <DebugSymbols>true</DebugSymbols> + <DebugType>full</DebugType> + <Optimize>false</Optimize> + <OutputPath>bin\Debug\</OutputPath> + <DefineConstants>DEBUG;TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> + <PlatformTarget>AnyCPU</PlatformTarget> + <DebugType>pdbonly</DebugType> + <Optimize>true</Optimize> + <OutputPath>bin\Release\</OutputPath> + <DefineConstants>TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <ItemGroup> + <Reference Include="Newtonsoft.Json, Version=11.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed"> + <HintPath>..\packages\Newtonsoft.Json.11.0.2\lib\net35\Newtonsoft.Json.dll</HintPath> + </Reference> + <Reference Include="nunit.framework, Version=3.10.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb"> + <HintPath>..\packages\NUnit.3.10.1\lib\net35\nunit.framework.dll</HintPath> + <Private>True</Private> + </Reference> + <Reference Include="System" /> + <Reference Include="System.Core" /> + <Reference Include="System.Data" /> + <Reference Include="System.Xml" /> + </ItemGroup> + <ItemGroup> + <Compile Include="Tests.cs" /> + <Compile Include="Properties\AssemblyInfo.cs" /> + </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\Module6\ParserAST.csproj"> + <Project>{9815f576-cab5-4d52-9963-f9b8277e340c}</Project> + <Name>ParserAST</Name> + </ProjectReference> + </ItemGroup> + <ItemGroup> + <None Include="packages.config" /> + </ItemGroup> + <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> + <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild"> + <PropertyGroup> + <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}.</ErrorText> + </PropertyGroup> + <Error Condition="!Exists('..\packages\NUnit.3.10.1\build\NUnit.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\NUnit.3.10.1\build\NUnit.props'))" /> + </Target> + <!-- To modify your build process, add your task inside one of the targets below and uncomment it. + Other similar extension points exist, see Microsoft.Common.targets. + <Target Name="BeforeBuild"> + </Target> + <Target Name="AfterBuild"> + </Target> + --> +</Project> \ No newline at end of file diff --git a/TestASTParser/Tests.cs b/TestASTParser/Tests.cs new file mode 100644 index 0000000000000000000000000000000000000000..aca03bea319b9367dc8c214908cd6e284e2266f5 --- /dev/null +++ b/TestASTParser/Tests.cs @@ -0,0 +1,90 @@ +using System; +using NUnit.Framework; +using SimpleScanner; +using SimpleParser; +using System.Text; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; + +namespace TestASTParser +{ + public class ASTParserTests + { + public static JObject Parse(string text) + { + Scanner scanner = new Scanner(); + scanner.SetSource(text, 0); + + Parser parser = new Parser(scanner); + + var b = parser.Parse(); + if (!b) + Assert.Fail("программа РЅРµ распознана"); + else + { + JsonSerializerSettings jsonSettings = new JsonSerializerSettings(); + jsonSettings.Formatting = Newtonsoft.Json.Formatting.Indented; + jsonSettings.TypeNameHandling = TypeNameHandling.All; + string output = JsonConvert.SerializeObject(parser.root, jsonSettings); + return JObject.Parse(output); + } + + return null; + + } + } + + [TestFixture] + public class WhileTests + { + + [Test] + public void TestWhile() + { + var tree = ASTParserTests.Parse("begin while 2 do a:=2 end"); + Assert.AreEqual("ProgramTree.WhileNode, SimpleLang", (string)tree["StList"]["$values"][0]["$type"]); + Assert.AreEqual("ProgramTree.IntNumNode, SimpleLang", (string)tree["StList"]["$values"][0]["Expr"]["$type"]); + Assert.AreEqual("2", ((string)tree["StList"]["$values"][0]["Expr"]["Num"]).Trim()); + Assert.AreEqual("ProgramTree.AssignNode, SimpleLang", (string)tree["StList"]["$values"][0]["Stat"]["$type"]); + } + } + + [TestFixture] + public class RepeatTests + { + + [Test] + public void TestRepeat() + { + var tree = ASTParserTests.Parse("begin repeat a:=2 until 2 end"); + Assert.AreEqual("ProgramTree.RepeatNode, SimpleLang", (string)tree["StList"]["$values"][0]["$type"]); + // TODO: проверить узлы содержимого repeat + } + } + + [TestFixture] + public class ForTests + { + + [Test] + public void TestFor() + { + var tree = ASTParserTests.Parse("begin for i:=2 to 10 do a:=2 end"); + Assert.AreEqual("ProgramTree.ForNode, SimpleLang", (string)tree["StList"]["$values"][0]["$type"]); + // TODO: проверить узлы содержимого for + } + } + + [TestFixture] + public class WriteTests + { + + [Test] + public void TestWrite() + { + var tree = ASTParserTests.Parse("begin write(2) end"); + Assert.AreEqual("ProgramTree.WriteNode, SimpleLang", (string)tree["StList"]["$values"][0]["$type"]); + // TODO: проверить содержимое write + } + } +} \ No newline at end of file diff --git a/TestASTParser/packages.config b/TestASTParser/packages.config new file mode 100644 index 0000000000000000000000000000000000000000..6386acbb2c0793a01af2210c3c040b325a0ba6bd --- /dev/null +++ b/TestASTParser/packages.config @@ -0,0 +1,4 @@ +п»ї<?xml version="1.0" encoding="utf-8"?> +<packages> + <package id="NUnit" version="3.10.1" targetFramework="net35" /> +</packages> \ No newline at end of file diff --git a/TestCodeGenerator/Properties/AssemblyInfo.cs b/TestCodeGenerator/Properties/AssemblyInfo.cs new file mode 100644 index 0000000000000000000000000000000000000000..367efae98356a5d5c61c84b10a27c9faae370353 --- /dev/null +++ b/TestCodeGenerator/Properties/AssemblyInfo.cs @@ -0,0 +1,35 @@ +п»їusing System.Reflection; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("TestCodeGenerator")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("TestCodeGenerator")] +[assembly: AssemblyCopyright("Copyright В© 2018")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("186EE1C7-7F34-4428-8A82-F63C8D4368E8")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] \ No newline at end of file diff --git a/TestCodeGenerator/TestCodeGenerator.csproj b/TestCodeGenerator/TestCodeGenerator.csproj new file mode 100644 index 0000000000000000000000000000000000000000..c2110b61d07ef287676eccbbf31d987f2c200177 --- /dev/null +++ b/TestCodeGenerator/TestCodeGenerator.csproj @@ -0,0 +1,73 @@ +п»ї<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <Import Project="..\packages\NUnit.3.10.1\build\NUnit.props" Condition="Exists('..\packages\NUnit.3.10.1\build\NUnit.props')" /> + <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <ProjectGuid>{186EE1C7-7F34-4428-8A82-F63C8D4368E8}</ProjectGuid> + <ProjectTypeGuids>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> + <OutputType>Library</OutputType> + <AppDesignerFolder>Properties</AppDesignerFolder> + <RootNamespace>TestCodeGenerator</RootNamespace> + <AssemblyName>TestCodeGenerator</AssemblyName> + <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> + <FileAlignment>512</FileAlignment> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> + <PlatformTarget>AnyCPU</PlatformTarget> + <DebugSymbols>true</DebugSymbols> + <DebugType>full</DebugType> + <Optimize>false</Optimize> + <OutputPath>bin\Debug\</OutputPath> + <DefineConstants>DEBUG;TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> + <PlatformTarget>AnyCPU</PlatformTarget> + <DebugType>pdbonly</DebugType> + <Optimize>true</Optimize> + <OutputPath>bin\Release\</OutputPath> + <DefineConstants>TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <ItemGroup> + <Reference Include="nunit.framework, Version=3.10.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb"> + <HintPath>..\packages\NUnit.3.10.1\lib\net35\nunit.framework.dll</HintPath> + <Private>True</Private> + </Reference> + <Reference Include="System" /> + <Reference Include="System.Core" /> + <Reference Include="System.Data" /> + <Reference Include="System.Xml" /> + </ItemGroup> + <ItemGroup> + <Compile Include="Tests.cs" /> + <Compile Include="Properties\AssemblyInfo.cs" /> + </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\Module8\ParserGenerator.csproj"> + <Project>{f8734914-3677-4a80-a87d-c274f52576fd}</Project> + <Name>ParserGenerator</Name> + </ProjectReference> + </ItemGroup> + <ItemGroup> + <None Include="packages.config" /> + </ItemGroup> + <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> + <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild"> + <PropertyGroup> + <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}.</ErrorText> + </PropertyGroup> + <Error Condition="!Exists('..\packages\NUnit.3.10.1\build\NUnit.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\NUnit.3.10.1\build\NUnit.props'))" /> + </Target> + <!-- To modify your build process, add your task inside one of the targets below and uncomment it. + Other similar extension points exist, see Microsoft.Common.targets. + <Target Name="BeforeBuild"> + </Target> + <Target Name="AfterBuild"> + </Target> + --> +</Project> \ No newline at end of file diff --git a/TestCodeGenerator/Tests.cs b/TestCodeGenerator/Tests.cs new file mode 100644 index 0000000000000000000000000000000000000000..bdd7f41048afe6e25c8c21c808f04fc62f789349 --- /dev/null +++ b/TestCodeGenerator/Tests.cs @@ -0,0 +1,98 @@ +using System; +using System.IO; +using System.Net; +using NUnit.Framework; +using SimpleScanner; +using SimpleParser; +using SimpleLang.Visitors; + +namespace TestCodeGenerator +{ + public class TestHelper + { + public static Parser Parse(string text) + { + Scanner scanner = new Scanner(); + scanner.SetSource(text, 0); + Parser parser = new Parser(scanner); + Assert.IsTrue(parser.Parse()); + return parser; + } + + public static string GenerateNRun(string text) + { + var parser = TestHelper.Parse(text); + var code = new GenCodeVisitor(); + parser.root.Visit(code); + code.EndProgram(); + //code.PrintCommands(); + string output = ""; + using(MemoryStream ms = new MemoryStream()) + { + var sw = new StreamWriter(ms); + try + { + Console.SetOut(sw); + code.RunProgram(); + sw.Flush(); + + ms.Seek(0, SeekOrigin.Begin); + var sr = new StreamReader(ms); + output = sr.ReadToEnd().Trim(); + } + finally + { + sw.Dispose(); + } + } + return output; + } + } + + [TestFixture] + public class TestCodeGenerator + { + [Test] + public void SmokeTest() + { + var parser = TestHelper.Parse(@"begin end"); + var code = new GenCodeVisitor(); + parser.root.Visit(code); + code.EndProgram(); + //code.PrintCommands(); + code.RunProgram(); + } + + [Test] + public void TestOutput() + { + Assert.AreEqual("2", TestHelper.GenerateNRun(@"begin write(2) end")); + } + + [Test] + public void TestIntDivMod() + { + Assert.AreEqual("48", TestHelper.GenerateNRun(@"begin var a; a := (232 / 5) + (232 % 5); write(a) end")); + } + + [Test] + public void TestIf() + { + Assert.AreEqual("3", TestHelper.GenerateNRun(@"begin var a; a := 0; if a then write(2) else write(3) end")); + + Assert.AreEqual("3", TestHelper.GenerateNRun(@"begin var x,y; x := 1; y := x-1; if x then if y then write(2) else write(3) end")); + } + + [Test] + public void TestWhile() + { + Assert.AreEqual("1024", TestHelper.GenerateNRun(@"begin var a,b; b:=1; a:=10; while a do begin a:=a-1; b:=b*2; end; write(b) end")); + } + + [Test] + public void TestUntil() + { + Assert.AreEqual("1024", TestHelper.GenerateNRun(@"begin var a,b; b:=1; a:=10; repeat a:=a-1; b:=b*2 until a; write(b) end")); + } + } +} \ No newline at end of file diff --git a/TestCodeGenerator/packages.config b/TestCodeGenerator/packages.config new file mode 100644 index 0000000000000000000000000000000000000000..6386acbb2c0793a01af2210c3c040b325a0ba6bd --- /dev/null +++ b/TestCodeGenerator/packages.config @@ -0,0 +1,4 @@ +п»ї<?xml version="1.0" encoding="utf-8"?> +<packages> + <package id="NUnit" version="3.10.1" targetFramework="net35" /> +</packages> \ No newline at end of file diff --git a/TestDescentParser/Properties/AssemblyInfo.cs b/TestDescentParser/Properties/AssemblyInfo.cs new file mode 100644 index 0000000000000000000000000000000000000000..5eef0067b88011d3984d8d68998e8b34f8310e6e --- /dev/null +++ b/TestDescentParser/Properties/AssemblyInfo.cs @@ -0,0 +1,35 @@ +п»їusing System.Reflection; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("TestDescentParser")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("TestDescentParser")] +[assembly: AssemblyCopyright("Copyright В© 2018")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("F6EABC41-B147-44EB-B6EF-13C3183C9961")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] \ No newline at end of file diff --git a/TestDescentParser/TestDescentParser.csproj b/TestDescentParser/TestDescentParser.csproj new file mode 100644 index 0000000000000000000000000000000000000000..d5a7ba658fcfec362ae8a708b8baa8d2c7a12d95 --- /dev/null +++ b/TestDescentParser/TestDescentParser.csproj @@ -0,0 +1,77 @@ +п»ї<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <Import Project="..\packages\NUnit.3.10.1\build\NUnit.props" Condition="Exists('..\packages\NUnit.3.10.1\build\NUnit.props')" /> + <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <ProjectGuid>{F6EABC41-B147-44EB-B6EF-13C3183C9961}</ProjectGuid> + <ProjectTypeGuids>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> + <OutputType>Library</OutputType> + <AppDesignerFolder>Properties</AppDesignerFolder> + <RootNamespace>TestDescentParser</RootNamespace> + <AssemblyName>TestDescentParser</AssemblyName> + <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> + <FileAlignment>512</FileAlignment> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> + <PlatformTarget>AnyCPU</PlatformTarget> + <DebugSymbols>true</DebugSymbols> + <DebugType>full</DebugType> + <Optimize>false</Optimize> + <OutputPath>bin\Debug\</OutputPath> + <DefineConstants>DEBUG;TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> + <PlatformTarget>AnyCPU</PlatformTarget> + <DebugType>pdbonly</DebugType> + <Optimize>true</Optimize> + <OutputPath>bin\Release\</OutputPath> + <DefineConstants>TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <ItemGroup> + <Reference Include="nunit.framework, Version=3.10.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb"> + <HintPath>..\packages\NUnit.3.10.1\lib\net35\nunit.framework.dll</HintPath> + <Private>True</Private> + </Reference> + <Reference Include="System" /> + <Reference Include="System.Core" /> + <Reference Include="System.Data" /> + <Reference Include="System.Xml" /> + </ItemGroup> + <ItemGroup> + <Compile Include="Tests.cs" /> + <Compile Include="Properties\AssemblyInfo.cs" /> + </ItemGroup> + <ItemGroup> + <None Include="packages.config" /> + </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\Module2\SimpleLangLexer\SimpleLexer.csproj"> + <Project>{28c0284b-2f43-45d6-a77f-ab08f919717d}</Project> + <Name>SimpleLexer</Name> + </ProjectReference> + <ProjectReference Include="..\Module4\SimpleLangParser\RecursiveDescentParser.csproj"> + <Project>{837bc9fe-daa9-416e-bcce-0235e5c38d4b}</Project> + <Name>RecursiveDescentParser</Name> + </ProjectReference> + </ItemGroup> + <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> + <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild"> + <PropertyGroup> + <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}.</ErrorText> + </PropertyGroup> + <Error Condition="!Exists('..\packages\NUnit.3.10.1\build\NUnit.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\NUnit.3.10.1\build\NUnit.props'))" /> + </Target> + <!-- To modify your build process, add your task inside one of the targets below and uncomment it. + Other similar extension points exist, see Microsoft.Common.targets. + <Target Name="BeforeBuild"> + </Target> + <Target Name="AfterBuild"> + </Target> + --> +</Project> \ No newline at end of file diff --git a/TestDescentParser/Tests.cs b/TestDescentParser/Tests.cs new file mode 100644 index 0000000000000000000000000000000000000000..4f93546459d52544341a74816857ea9bf298ca9c --- /dev/null +++ b/TestDescentParser/Tests.cs @@ -0,0 +1,127 @@ +using System; +using NUnit.Framework; +using SimpleLexer; +using SimpleLangParser; +using System.IO; + +namespace TestDescentParser +{ + [TestFixture] + public class DescentParserTests + { + private bool Parse(string text) + { + TextReader inputReader = new StringReader(text); + Lexer l = new Lexer(inputReader); + Parser p = new Parser(l); + p.Progr(); + if (l.LexKind == Tok.EOF) + { + return true; + } + else + { + return false; + } + } + + [Test] + public void TestWhile() + { + Assert.IsTrue(Parse(@"begin while 5 do a:=2 end")); + + Assert.IsTrue(Parse(@"begin + while 5 do + begin + a:=2 + end + end")); + + Assert.IsTrue(Parse(@"begin + while 5 do + begin + while 6 do + a:=2; + while 7 do + begin + a:=3; + c:=4 + end + end + end")); + + } + + [Test] + public void TestFor() + { + Assert.IsTrue(Parse(@"begin + for a:=1 to 5 do + begin + b:=b+1 + end + end")); + + Assert.IsTrue(Parse(@"begin + for a:=1 to 5 do + begin + for i:=1 to 6 do + c:=c-1; + b:=b+1 + end + end")); + + } + + [Test] + public void TestIf() + { + Assert.IsTrue(Parse(@"begin + if 2 then + a:=2 + else + b:=2; + + if 3 then + if c then + c:=4 + else + m:=1 + else + v:=8; + + if 4 then + if 4 then + if 6 then + m:=0 + end")); + + } + + [Test] + public void TestExpr() + { + Assert.IsTrue(Parse(@"begin + if 2+2*(c-d/3) then + begin + a:=2; + while 2-3+f do c:=c*2 + end + else + b:=2-3*(c-d/f*3); + + for i:=2-3*(s-d) to (c-3) do + a:=(a-(3-3)); + + if 3 then + if (c-3) then + c:=4+2 + else + m:=1 + else + v:=(8+2) + end")); + + } + } +} \ No newline at end of file diff --git a/TestDescentParser/packages.config b/TestDescentParser/packages.config new file mode 100644 index 0000000000000000000000000000000000000000..6386acbb2c0793a01af2210c3c040b325a0ba6bd --- /dev/null +++ b/TestDescentParser/packages.config @@ -0,0 +1,4 @@ +п»ї<?xml version="1.0" encoding="utf-8"?> +<packages> + <package id="NUnit" version="3.10.1" targetFramework="net35" /> +</packages> \ No newline at end of file diff --git a/TestGeneratedLexer/Properties/AssemblyInfo.cs b/TestGeneratedLexer/Properties/AssemblyInfo.cs new file mode 100644 index 0000000000000000000000000000000000000000..a808b53d3a5ef7bedb637dc39408342c8c4779ab --- /dev/null +++ b/TestGeneratedLexer/Properties/AssemblyInfo.cs @@ -0,0 +1,35 @@ +п»їusing System.Reflection; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("TestGeneratedLexer")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("TestGeneratedLexer")] +[assembly: AssemblyCopyright("Copyright В© 2018")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("24413F87-3568-4BDF-ADC8-760086CEE37A")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] \ No newline at end of file diff --git a/TestGeneratedLexer/TestGeneratedLexer.csproj b/TestGeneratedLexer/TestGeneratedLexer.csproj new file mode 100644 index 0000000000000000000000000000000000000000..5321e949efbc3ff44a43da03b198d00246ff822d --- /dev/null +++ b/TestGeneratedLexer/TestGeneratedLexer.csproj @@ -0,0 +1,74 @@ +п»ї<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <Import Project="..\packages\NUnit.3.10.1\build\NUnit.props" Condition="Exists('..\packages\NUnit.3.10.1\build\NUnit.props')" /> + <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <ProjectGuid>{24413F87-3568-4BDF-ADC8-760086CEE37A}</ProjectGuid> + <ProjectTypeGuids>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> + <OutputType>Library</OutputType> + <AppDesignerFolder>Properties</AppDesignerFolder> + <RootNamespace>TestGeneratedLexer</RootNamespace> + <AssemblyName>TestGeneratedLexer</AssemblyName> + <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> + <FileAlignment>512</FileAlignment> + <LangVersion>6</LangVersion> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> + <PlatformTarget>AnyCPU</PlatformTarget> + <DebugSymbols>true</DebugSymbols> + <DebugType>full</DebugType> + <Optimize>false</Optimize> + <OutputPath>bin\Debug\</OutputPath> + <DefineConstants>DEBUG;TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> + <PlatformTarget>AnyCPU</PlatformTarget> + <DebugType>pdbonly</DebugType> + <Optimize>true</Optimize> + <OutputPath>bin\Release\</OutputPath> + <DefineConstants>TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <ItemGroup> + <Reference Include="nunit.framework, Version=3.10.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb"> + <HintPath>..\packages\NUnit.3.10.1\lib\net35\nunit.framework.dll</HintPath> + <Private>True</Private> + </Reference> + <Reference Include="System" /> + <Reference Include="System.Core" /> + <Reference Include="System.Data" /> + <Reference Include="System.Xml" /> + </ItemGroup> + <ItemGroup> + <Compile Include="Tests.cs" /> + <Compile Include="Properties\AssemblyInfo.cs" /> + </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\Module3\GeneratedLexer.csproj"> + <Project>{4dd13462-dcb4-4c3e-b777-7fea90cea9d1}</Project> + <Name>GeneratedLexer</Name> + </ProjectReference> + </ItemGroup> + <ItemGroup> + <None Include="packages.config" /> + </ItemGroup> + <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> + <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild"> + <PropertyGroup> + <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}.</ErrorText> + </PropertyGroup> + <Error Condition="!Exists('..\packages\NUnit.3.10.1\build\NUnit.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\NUnit.3.10.1\build\NUnit.props'))" /> + </Target> + <!-- To modify your build process, add your task inside one of the targets below and uncomment it. + Other similar extension points exist, see Microsoft.Common.targets. + <Target Name="BeforeBuild"> + </Target> + <Target Name="AfterBuild"> + </Target> + --> +</Project> \ No newline at end of file diff --git a/TestGeneratedLexer/Tests.cs b/TestGeneratedLexer/Tests.cs new file mode 100644 index 0000000000000000000000000000000000000000..88eda14b29dc1918ee9e2efe593d87a500a0eb76 --- /dev/null +++ b/TestGeneratedLexer/Tests.cs @@ -0,0 +1,84 @@ +п»їusing System; +using NUnit.Framework; +using GeneratedLexer; + +namespace TestGeneratedLexer +{ + [TestFixture] + public class GeneratedLexerTests + { + [Test] + public void TestIdCount() + { + LexerAddon lexer = new LexerAddon(@"id1 id2 id3 + id4 id5 "); + lexer.Lex(); + Assert.AreEqual(5, lexer.idCount); + } + + [Test] + public void TestIdInfo() + { + LexerAddon lexer = new LexerAddon(@"i22d1 i id3 + Md4 inNd5 "); + lexer.Lex(); + Assert.AreEqual(5, lexer.idCount); + Assert.AreEqual(5, lexer.maxIdLength); + Assert.AreEqual(1, lexer.minIdLength); + Assert.AreEqual(3.4, lexer.avgIdLength, 0.001); + } + + [Test] + public void TestNumbers() + { + LexerAddon lexer = new LexerAddon(@"i22d1 5.6 i 32 id3 + Md4 8.9 inNd5 1 42 "); + lexer.Lex(); + + Assert.AreEqual(75, lexer.sumInt); + Assert.AreEqual(14.5, lexer.sumDouble, 0.001); + } + + [Test] + public void TestSingleLineCmt() + { + LexerAddon lexer = new LexerAddon(@"i22d1 5.6 // i 32 id3 + Md4 8.9 inNd5 1 42 "); + lexer.Lex(); + + Assert.AreEqual(3, lexer.idCount); + Assert.AreEqual(43, lexer.sumInt); + Assert.AreEqual(14.5, lexer.sumDouble, 0.001); + } + + [Test] + public void TestMultiLineCmt() + { + LexerAddon lexer = new LexerAddon(@"i22d1 5.6 { i 32 id3 + Md4 2.3 2 33} 8.9 inNd5 1 42 "); + lexer.Lex(); + + Assert.AreEqual(2, lexer.idCount); + Assert.AreEqual(43, lexer.sumInt); + Assert.AreEqual(14.5, lexer.sumDouble, 0.001); + } + + [Test] + public void TestMultiLineCmtIds() + { + LexerAddon lexer = new LexerAddon(@"i22d1 5.6 { i 32 id3 + Md4 tgg begin ide2 + end ids 2.3 2 33} 8.9 inNd5 1 42 "); + lexer.Lex(); + + Assert.AreEqual(6, lexer.idsInComment.Count); + Assert.Contains("i", lexer.idsInComment); + Assert.Contains("id3", lexer.idsInComment); + Assert.Contains("Md4", lexer.idsInComment); + Assert.Contains("tgg", lexer.idsInComment); + Assert.Contains("ide2", lexer.idsInComment); + Assert.Contains("ids", lexer.idsInComment); + + } + } +} \ No newline at end of file diff --git a/TestGeneratedLexer/packages.config b/TestGeneratedLexer/packages.config new file mode 100644 index 0000000000000000000000000000000000000000..6386acbb2c0793a01af2210c3c040b325a0ba6bd --- /dev/null +++ b/TestGeneratedLexer/packages.config @@ -0,0 +1,4 @@ +п»ї<?xml version="1.0" encoding="utf-8"?> +<packages> + <package id="NUnit" version="3.10.1" targetFramework="net35" /> +</packages> \ No newline at end of file diff --git a/TestGeneratedParser/Properties/AssemblyInfo.cs b/TestGeneratedParser/Properties/AssemblyInfo.cs new file mode 100644 index 0000000000000000000000000000000000000000..c258e60cee654a3b612124ba7b87b4591abc566c --- /dev/null +++ b/TestGeneratedParser/Properties/AssemblyInfo.cs @@ -0,0 +1,35 @@ +п»їusing System.Reflection; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("TestGeneratedParser")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("TestGeneratedParser")] +[assembly: AssemblyCopyright("Copyright В© 2018")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("76382857-9D1F-4098-8376-48EF4A702445")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] \ No newline at end of file diff --git a/TestGeneratedParser/TestGeneratedParser.csproj b/TestGeneratedParser/TestGeneratedParser.csproj new file mode 100644 index 0000000000000000000000000000000000000000..56b0492a0afca6d5b9aa01efc4efb95b22f5b355 --- /dev/null +++ b/TestGeneratedParser/TestGeneratedParser.csproj @@ -0,0 +1,73 @@ +п»ї<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <Import Project="..\packages\NUnit.3.10.1\build\NUnit.props" Condition="Exists('..\packages\NUnit.3.10.1\build\NUnit.props')" /> + <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <ProjectGuid>{76382857-9D1F-4098-8376-48EF4A702445}</ProjectGuid> + <ProjectTypeGuids>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> + <OutputType>Library</OutputType> + <AppDesignerFolder>Properties</AppDesignerFolder> + <RootNamespace>TestGeneratedParser</RootNamespace> + <AssemblyName>TestGeneratedParser</AssemblyName> + <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> + <FileAlignment>512</FileAlignment> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> + <PlatformTarget>AnyCPU</PlatformTarget> + <DebugSymbols>true</DebugSymbols> + <DebugType>full</DebugType> + <Optimize>false</Optimize> + <OutputPath>bin\Debug\</OutputPath> + <DefineConstants>DEBUG;TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> + <PlatformTarget>AnyCPU</PlatformTarget> + <DebugType>pdbonly</DebugType> + <Optimize>true</Optimize> + <OutputPath>bin\Release\</OutputPath> + <DefineConstants>TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <ItemGroup> + <Reference Include="nunit.framework, Version=3.10.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb"> + <HintPath>..\packages\NUnit.3.10.1\lib\net35\nunit.framework.dll</HintPath> + <Private>True</Private> + </Reference> + <Reference Include="System" /> + <Reference Include="System.Core" /> + <Reference Include="System.Data" /> + <Reference Include="System.Xml" /> + </ItemGroup> + <ItemGroup> + <Compile Include="Tests.cs" /> + <Compile Include="Properties\AssemblyInfo.cs" /> + </ItemGroup> + <ItemGroup> + <None Include="packages.config" /> + </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\Module5\GeneratedParser.csproj"> + <Project>{0ed50d29-f6c3-44f3-bf13-bc1a433f6119}</Project> + <Name>GeneratedParser</Name> + </ProjectReference> + </ItemGroup> + <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> + <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild"> + <PropertyGroup> + <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}.</ErrorText> + </PropertyGroup> + <Error Condition="!Exists('..\packages\NUnit.3.10.1\build\NUnit.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\NUnit.3.10.1\build\NUnit.props'))" /> + </Target> + <!-- To modify your build process, add your task inside one of the targets below and uncomment it. + Other similar extension points exist, see Microsoft.Common.targets. + <Target Name="BeforeBuild"> + </Target> + <Target Name="AfterBuild"> + </Target> + --> +</Project> \ No newline at end of file diff --git a/TestGeneratedParser/Tests.cs b/TestGeneratedParser/Tests.cs new file mode 100644 index 0000000000000000000000000000000000000000..9994bb73e93a0e5c8b934b3bf5917f592bf5d665 --- /dev/null +++ b/TestGeneratedParser/Tests.cs @@ -0,0 +1,74 @@ +п»їusing System; +using System.IO; +using NUnit.Framework; +using SimpleScanner; +using SimpleParser; + +namespace TestGeneratedParser +{ + [TestFixture] + public class GeneratedParserTests + { + private bool Parse(string text) + { + Scanner scanner = new Scanner(); + scanner.SetSource(text, 0); + + Parser parser = new Parser(scanner); + + return parser.Parse(); + } + + [Test] + public void TestWhile() + { + Assert.True(Parse(@"begin while 2 do a:=2 end")); + } + + [Test] + public void TestRepeat() + { + Assert.True(Parse(@"begin repeat a:=2; b:=3 until 3 end")); + } + + [Test] + public void TestFor() + { + Assert.True(Parse(@"begin for i:= 1 to 5 do a:=2 end")); + + Assert.True(Parse(@"begin for i:= 1 to 5 do if 3 then a:=2 else a:=5 end")); + } + + [Test] + public void TestWrite() + { + Assert.True(Parse(@"begin for i:= 1 to 5 do begin a:=2; write(a) end end")); + + Assert.True(Parse(@"begin if 2 then write(3) else write(4) end")); + + Assert.True(Parse(@"begin repeat write(5) until 2 end")); + } + + [Test] + public void TestIf() + { + Assert.True(Parse(@"begin if 2 then a:=3 else c:=8; if 3 then if 5 then z:=0 else n:=12 else g:=7 end")); + + Assert.True(Parse(@"begin while 1 do begin if 2 then a:=1 else b:=2 end end")); + } + + [Test] + public void TestVar() + { + Assert.True(Parse(@"begin var a,b,d end")); + } + + [Test] + public void TestExr() + { + Assert.True(Parse(@"begin a:=x-z*3/(c+3-(ddz)+2) end")); + + Assert.True(Parse(@"begin for i:=2+2*(c-3) to 5+6*2 do begin a:=x-z*3/(c+3-(ddz)+2); if (2-2) then c:=a-2 else write(2+2) end end")); + } + } +} \ No newline at end of file diff --git a/TestGeneratedParser/packages.config b/TestGeneratedParser/packages.config new file mode 100644 index 0000000000000000000000000000000000000000..6386acbb2c0793a01af2210c3c040b325a0ba6bd --- /dev/null +++ b/TestGeneratedParser/packages.config @@ -0,0 +1,4 @@ +п»ї<?xml version="1.0" encoding="utf-8"?> +<packages> + <package id="NUnit" version="3.10.1" targetFramework="net35" /> +</packages> \ No newline at end of file diff --git a/TestLexer/Properties/AssemblyInfo.cs b/TestLexer/Properties/AssemblyInfo.cs new file mode 100644 index 0000000000000000000000000000000000000000..46410a84ad0c08cd0499034ced37eb098e80bf69 --- /dev/null +++ b/TestLexer/Properties/AssemblyInfo.cs @@ -0,0 +1,35 @@ +п»їusing System.Reflection; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("TestLexer")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("TestLexer")] +[assembly: AssemblyCopyright("Copyright В© 2018")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("144BB4B2-AC18-4A38-9BEC-98BB22EB3382")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] \ No newline at end of file diff --git a/TestLexer/TestLexer.csproj b/TestLexer/TestLexer.csproj new file mode 100644 index 0000000000000000000000000000000000000000..9bfdc4954a1574049f8fa1a1ca5770c3b5590523 --- /dev/null +++ b/TestLexer/TestLexer.csproj @@ -0,0 +1,76 @@ +п»ї<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <Import Project="..\packages\NUnit.3.10.1\build\NUnit.props" Condition="Exists('..\packages\NUnit.3.10.1\build\NUnit.props')" /> + <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <ProjectGuid>{144BB4B2-AC18-4A38-9BEC-98BB22EB3382}</ProjectGuid> + <ProjectTypeGuids>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> + <OutputType>Library</OutputType> + <AppDesignerFolder>Properties</AppDesignerFolder> + <RootNamespace>TestLexer</RootNamespace> + <AssemblyName>TestLexer</AssemblyName> + <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> + <FileAlignment>512</FileAlignment> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> + <PlatformTarget>AnyCPU</PlatformTarget> + <DebugSymbols>true</DebugSymbols> + <DebugType>full</DebugType> + <Optimize>false</Optimize> + <OutputPath>bin\Debug\</OutputPath> + <DefineConstants>DEBUG;TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> + <PlatformTarget>AnyCPU</PlatformTarget> + <DebugType>pdbonly</DebugType> + <Optimize>true</Optimize> + <OutputPath>bin\Release\</OutputPath> + <DefineConstants>TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <ItemGroup> + <Reference Include="Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> + <HintPath>C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.CSharp.dll</HintPath> + </Reference> + <Reference Include="nunit.framework, Version=3.10.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb"> + <HintPath>..\packages\NUnit.3.10.1\lib\net35\nunit.framework.dll</HintPath> + <Private>True</Private> + </Reference> + <Reference Include="System" /> + <Reference Include="System.Core" /> + <Reference Include="System.Data" /> + <Reference Include="System.Xml" /> + </ItemGroup> + <ItemGroup> + <Compile Include="Tests.cs" /> + <Compile Include="Properties\AssemblyInfo.cs" /> + </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\Module1\Lexer.csproj"> + <Project>{9715d879-f06e-4a65-ada0-a4df3619119d}</Project> + <Name>Lexer</Name> + </ProjectReference> + </ItemGroup> + <ItemGroup> + <None Include="packages.config" /> + </ItemGroup> + <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> + <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild"> + <PropertyGroup> + <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}.</ErrorText> + </PropertyGroup> + <Error Condition="!Exists('..\packages\NUnit.3.10.1\build\NUnit.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\NUnit.3.10.1\build\NUnit.props'))" /> + </Target> + <!-- To modify your build process, add your task inside one of the targets below and uncomment it. + Other similar extension points exist, see Microsoft.Common.targets. + <Target Name="BeforeBuild"> + </Target> + <Target Name="AfterBuild"> + </Target> + --> +</Project> \ No newline at end of file diff --git a/TestLexer/Tests.cs b/TestLexer/Tests.cs new file mode 100644 index 0000000000000000000000000000000000000000..cf890315f47c52caf3fc3aa467dfc49fe688555a --- /dev/null +++ b/TestLexer/Tests.cs @@ -0,0 +1,530 @@ +п»їusing System; +using NUnit.Framework; +using Lexer; + +namespace TestLexer +{ + [TestFixture] + public class TestIntLexer + { + [Test] + public void TestIntParse() + { + Lexer.Lexer l = new IntLexer("154216"); + Assert.IsTrue(l.Parse(), "РќРµ разбирает 154216"); + + l = new IntLexer("1"); + Assert.IsTrue(l.Parse(), "РќРµ разбирает 1"); + + l = new IntLexer("0"); + Assert.IsTrue(l.Parse(), "РќРµ разбирает 0"); + + l = new IntLexer("-0"); + Assert.IsTrue(l.Parse(), "РќРµ разбирает -0"); + + l = new IntLexer("+0"); + Assert.IsTrue(l.Parse(), "РќРµ разбирает +0"); + + l = new IntLexer("+11"); + Assert.IsTrue(l.Parse(), "РќРµ разбирает +11"); + + l = new IntLexer("-12"); + Assert.IsTrue(l.Parse(), "РќРµ разбирает -12"); + } + + [Test] + public void TestIntFailDot() + { + Lexer.Lexer l = new IntLexer("1.54216"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает 1.54216"); + } + + [Test] + public void TestIntFailSymbol() + { + Lexer.Lexer l = new IntLexer("Р°1"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает a1"); + l = new IntLexer("1a"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает 1Р°"); + } + + [Test] + public void TestIntFailEpty() + { + Lexer.Lexer l = new IntLexer(""); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает пустую строку"); + } + + [Test] + public void TestIntCollectNumber() + { + IntLexer lInt = new IntLexer("12"); + Assert.IsTrue(lInt.Parse()); + Assert.AreEqual(lInt.parseResult, 12, "РќРµ собирает число 12"); + + lInt = new IntLexer("-12"); + Assert.IsTrue(lInt.Parse()); + Assert.AreEqual(lInt.parseResult, -12, "РќРµ собирает число -12"); + + lInt = new IntLexer("+12"); + Assert.IsTrue(lInt.Parse()); + Assert.AreEqual(lInt.parseResult, 12, "РќРµ собирает число +12"); + + lInt = new IntLexer("+0"); + Assert.IsTrue(lInt.Parse()); + Assert.AreEqual(lInt.parseResult, 0, "РќРµ собирает число +0"); + } + } + + [TestFixture] + public class TestIdLexer + { + [Test] + public void TestIdParse() + { + IdentLexer l = new IdentLexer("abc22"); + Assert.IsTrue(l.Parse(), "РќРµ разбирает Р°bc22"); + + l = new IdentLexer("a"); + Assert.IsTrue(l.Parse(), "РќРµ разбирает Р°"); + } + + [Test] + public void TestIdEmpty() + { + IdentLexer l = new IdentLexer(""); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает пустую строку"); + } + + [Test] + public void TestIdCaps() + { + IdentLexer l = new IdentLexer("AAAAA"); + Assert.IsTrue(l.Parse(), "РќРµ разбирает заглавные Р±СѓРєРІС‹"); + } + + [Test] + public void TestIdNumbers() + { + IdentLexer l = new IdentLexer("a12345"); + Assert.IsTrue(l.Parse(), "РќРµ разбирает цифры РІ конце"); + } + + [Test] + public void TestIdUnderscore() + { + IdentLexer l = new IdentLexer("a12_5"); + Assert.IsTrue(l.Parse(), "РќРµ разбирает _"); + } + + [Test] + public void TestIdDot() + { + IdentLexer l = new IdentLexer("f67.3"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает точку"); + } + + [Test] + public void TestIdDollar() + { + IdentLexer l = new IdentLexer("f67$3"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает $"); + } + } + + [TestFixture] + public class TestIntNotZeroLexer + { + [Test] + public void TestIntNotZeroParse() + { + IntNoZeroLexer l = new IntNoZeroLexer("1234"); + Assert.IsTrue(l.Parse(), "РќРµ разбирает 1234"); + } + + [Test] + public void TestIntNotZeroFail() + { + IntNoZeroLexer l = new IntNoZeroLexer("01234"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает 0"); + + l = new IntNoZeroLexer("+01234"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает +0"); + + l = new IntNoZeroLexer("-01234"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает -0"); + + l = new IntNoZeroLexer("0"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает 0"); + } + + [Test] + public void TestIntNotZeroPass() + { + IntNoZeroLexer l = new IntNoZeroLexer("12034"); + Assert.IsTrue(l.Parse(), "РќРµ разбирает 12034"); + + l = new IntNoZeroLexer("+12034"); + Assert.IsTrue(l.Parse(), "РќРµ разбирает +12034"); + + l = new IntNoZeroLexer("12034"); + Assert.IsTrue(l.Parse(), "РќРµ разбирает 12034"); + + l = new IntNoZeroLexer("-12034"); + Assert.IsTrue(l.Parse(), "РќРµ разбирает -12034"); + } + + } + + [TestFixture] + public class TestLetterDigitLexer + { + + [Test] + public void TestLetterDigitParse() + { + LetterDigitLexer l = new LetterDigitLexer("a2f5j3"); + Assert.IsTrue(l.Parse(), "РќРµ разбирает a2f5j3"); + + l = new LetterDigitLexer("a"); + Assert.IsTrue(l.Parse(), "РќРµ разбирает a"); + + l = new LetterDigitLexer("a2"); + Assert.IsTrue(l.Parse(), "РќРµ разбирает a2"); + + l = new LetterDigitLexer("a2B"); + Assert.IsTrue(l.Parse(), "РќРµ разбирает a2B"); + + } + + [Test] + public void TestLetterDigitFail() + { + LetterDigitLexer l = new LetterDigitLexer("a25j3"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает a25j3"); + + l = new LetterDigitLexer("a2jb3"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает a2jb3"); + + l = new LetterDigitLexer("2a3b"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает 2a3b"); + + l = new LetterDigitLexer("a2f 3b"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает a2f 3b"); + } + } + + + [TestFixture] + public class TestLetterListLexer + { + [Test] + public void TestLetterListParse() + { + LetterListLexer l = new LetterListLexer("a,b,c"); + Assert.IsTrue(l.Parse(), "РќРµ пропускает a,b,c"); + Assert.AreEqual(l.ParseResult.Count, 3, "неправильно собран СЃРїРёСЃРѕРє-результат"); + Assert.Contains('a', l.ParseResult); + Assert.Contains('b', l.ParseResult); + Assert.Contains('c', l.ParseResult); + + l = new LetterListLexer("a"); + Assert.IsTrue(l.Parse(), "РќРµ пропускает a"); + Assert.AreEqual(l.ParseResult.Count, 1, "неправильно собран СЃРїРёСЃРѕРє-результат"); + Assert.Contains('a', l.ParseResult); + + l = new LetterListLexer("a,B"); + Assert.IsTrue(l.Parse(), "РќРµ пропускает a,B"); + Assert.AreEqual(l.ParseResult.Count, 2, "неправильно собран СЃРїРёСЃРѕРє-результат"); + Assert.Contains('a', l.ParseResult); + Assert.Contains('B', l.ParseResult); + + l = new LetterListLexer("a;B"); + Assert.IsTrue(l.Parse(), "РќРµ пропускает a;B"); + + l = new LetterListLexer("a,B;c"); + Assert.IsTrue(l.Parse(), "РќРµ пропускает a,B;c"); + + l = new LetterListLexer("a;b,c;d"); + Assert.IsTrue(l.Parse(), "РќРµ пропускает a;b,c;d"); + + } + + [Test] + public void TestLetterListFail() + { + LetterListLexer l = new LetterListLexer("a,b,"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает a,b,"); + + l = new LetterListLexer(",b"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает ,b"); + + l = new LetterListLexer(";b"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает ;b"); + + l = new LetterListLexer("B;"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает B;"); + + l = new LetterListLexer("a,,b"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает a,,b"); + + l = new LetterListLexer("a;;b"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает a;;b"); + + l = new LetterListLexer("a;1"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает цифры"); + } + + } + + [TestFixture] + public class TestDigitListLexer + { + [Test] + public void TestDigitListParse() + { + DigitListLexer l = new DigitListLexer("1 2"); + Assert.IsTrue(l.Parse(), "РќРµ пропускает 1 2"); + Assert.AreEqual(l.ParseResult.Count, 2, "неправильно собран СЃРїРёСЃРѕРє-результат"); + Assert.Contains(1, l.ParseResult); + Assert.Contains(2, l.ParseResult); + + l = new DigitListLexer("1"); + Assert.IsTrue(l.Parse(), "РќРµ пропускает 1"); + Assert.AreEqual(l.ParseResult.Count, 1, "неправильно собран СЃРїРёСЃРѕРє-результат"); + Assert.Contains(1, l.ParseResult); + + l = new DigitListLexer("5 6"); + Assert.IsTrue(l.Parse(), "РќРµ пропускает 5 6"); + Assert.AreEqual(l.ParseResult.Count, 2, "неправильно собран СЃРїРёСЃРѕРє-результат"); + Assert.Contains(5, l.ParseResult); + Assert.Contains(6, l.ParseResult); + } + + [Test] + public void TestDigitListFail() + { + DigitListLexer l = new DigitListLexer("12"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает 12"); + + l = new DigitListLexer(" 1"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает пробелы РІ начале"); + + l = new DigitListLexer("1 "); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает пробелы РІ конце"); + + l = new DigitListLexer("1, 2"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает ,"); + + l = new DigitListLexer("1;2"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает ;"); + + l = new DigitListLexer("1a2"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает 1a2"); + } + + } + + [TestFixture] + public class TestLetterDigitGroupLexer + { + [Test] + public void TestLetterDigitGroupParse() + { + LetterDigitGroupLexer l = new LetterDigitGroupLexer("aa12ab23"); + Assert.IsTrue(l.Parse(), "РќРµ пропускает aa12ab23"); + Assert.AreEqual(l.ParseResult, "aa12ab23", "неправильно собран aa12ab23"); + + l = new LetterDigitGroupLexer("a1b2"); + Assert.IsTrue(l.Parse(), "РќРµ пропускает a1b2"); + Assert.AreEqual(l.ParseResult, "a1b2", "неправильно собран a1b2"); + + l = new LetterDigitGroupLexer("aa"); + Assert.IsTrue(l.Parse(), "РќРµ пропускает aa"); + Assert.AreEqual(l.ParseResult, "aa", "неправильно собран aa"); + + l = new LetterDigitGroupLexer("A11"); + Assert.IsTrue(l.Parse(), "РќРµ пропускает A11"); + Assert.AreEqual(l.ParseResult, "A11", "неправильно собран A11"); + + l = new LetterDigitGroupLexer("ab33cd58e3n5ss32"); + Assert.IsTrue(l.Parse(), "РќРµ пропускает ab33cd58e3n5ss32"); + Assert.AreEqual(l.ParseResult, "ab33cd58e3n5ss32", "неправильно собран ab33cd58e3n5ss32"); + } + + [Test] + public void TestLetterDigitGroupFail() + { + LetterDigitGroupLexer l = new LetterDigitGroupLexer("1a"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает 1a"); + + l = new LetterDigitGroupLexer("a111b"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает a111b"); + + l = new LetterDigitGroupLexer("A_34b"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает A_34b"); + + l = new LetterDigitGroupLexer(""); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает пустую строку"); + + l = new LetterDigitGroupLexer("11"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает 11"); + + l = new LetterDigitGroupLexer("_A34"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает _A34"); + } + } + + [TestFixture] + public class TestDoubleLexer + { + [Test] + public void TestDoubleParse() + { + + DoubleLexer l = new DoubleLexer("123.4"); + Assert.IsTrue(l.Parse(), "РќРµ понимает 123.4"); + Assert.AreEqual(l.ParseResult, 123.4, "Неправильно прочитал 123.4"); + + l = new DoubleLexer("123"); + Assert.IsTrue(l.Parse(), "РќРµ понимает 123"); + Assert.AreEqual(l.ParseResult, 123, "Неправильно прочитал 123"); + + l = new DoubleLexer("0.4"); + Assert.IsTrue(l.Parse(), "РќРµ понимает 0.4"); + Assert.AreEqual(l.ParseResult, 0.4, "Неправильно прочитал 0.4"); + + l = new DoubleLexer("0.4"); + Assert.IsTrue(l.Parse(), "РќРµ понимает 0.4"); + Assert.AreEqual(l.ParseResult, 0.4, "Неправильно прочитал 0.4"); + + } + + [Test] + public void TestDoubleFail() + { + DoubleLexer l = new DoubleLexer(".4"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает .4"); + + l = new DoubleLexer("4."); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает 4."); + + l = new DoubleLexer("."); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает ."); + + l = new DoubleLexer(""); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает пустую строку"); + } + + } + + [TestFixture] + public class TestQuotedStringLexer + { + + [Test] + public void TestQuotedStringParse() + { + StringLexer l = new StringLexer("''"); + Assert.IsTrue(l.Parse(), "РќРµ пропускает ''"); + + l = new StringLexer("'aa#2N3@_3-x//45'"); + Assert.IsTrue(l.Parse(), "РќРµ пропускает 'aa#2N3@_3-x//45'"); + + l = new StringLexer("'23 3 a'"); + Assert.IsTrue(l.Parse(), "РќРµ пропускает '23 3 a'"); + } + + [Test] + public void TestQuotedStringFail() + { + StringLexer l = new StringLexer("'"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает '"); + + l = new StringLexer("aa'"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает aa'"); + + l = new StringLexer("b 'add'"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает b 'add'"); + + l = new StringLexer("'add' d"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает 'add' d"); + } + + } + + [TestFixture] + public class TestCommentLexer + { + + [Test] + public void TestCommentParse() + { + CommentLexer l = new CommentLexer("/**/"); + Assert.IsTrue(l.Parse(), "РќРµ пропускает /**/"); + + l = new CommentLexer("/*as3 @4&*_ -dd %~f*/"); + Assert.IsTrue(l.Parse(), "РќРµ пропускает /*as3 @4&*_ -dd %~f*/"); + + l = new CommentLexer("/*asda \n */"); + Assert.IsTrue(l.Parse(), "РќРµ пропускает перенос строки"); + } + + [Test] + public void TestCommentFail() + { + CommentLexer l = new CommentLexer("*/"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает */'"); + + l = new CommentLexer("/*"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает /*'"); + + l = new CommentLexer("/* \n"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает */ \n'"); + + l = new CommentLexer("/**/*/"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает /**/*/'"); + } + + } + + [TestFixture] + public class TestIdChainLexer + { + [Test] + public void TestIdChainParse() + { + IdentChainLexer l = new IdentChainLexer("Id1"); + Assert.IsTrue(l.Parse(), "РќРµ пропускает Id1"); + + l = new IdentChainLexer("Id1.Id2"); + Assert.IsTrue(l.Parse(), "РќРµ пропускает Id1.Id2"); + + l = new IdentChainLexer("uUd.k_22.sa3"); + Assert.IsTrue(l.Parse(), "РќРµ пропускает uUd.k_22.sa3"); + } + + [Test] + public void TestIdChainFail() + { + IdentChainLexer l = new IdentChainLexer("3Id1"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает 3Id1"); + + l = new IdentChainLexer(".Id2"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает .Id2"); + + l = new IdentChainLexer("uUd."); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает uUd."); + + l = new IdentChainLexer("uUd.3sa"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает uUd.3sa"); + + l = new IdentChainLexer("uUd. _sa"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает uUd. _sa"); + + l = new IdentChainLexer("uUd,sa"); + Assert.Throws<LexerException>(() => { l.Parse(); }, "Пропускает uUd,sa"); + + } + } +} \ No newline at end of file diff --git a/TestLexer/packages.config b/TestLexer/packages.config new file mode 100644 index 0000000000000000000000000000000000000000..6386acbb2c0793a01af2210c3c040b325a0ba6bd --- /dev/null +++ b/TestLexer/packages.config @@ -0,0 +1,4 @@ +п»ї<?xml version="1.0" encoding="utf-8"?> +<packages> + <package id="NUnit" version="3.10.1" targetFramework="net35" /> +</packages> \ No newline at end of file diff --git a/TestSimpleLexer/Properties/AssemblyInfo.cs b/TestSimpleLexer/Properties/AssemblyInfo.cs new file mode 100644 index 0000000000000000000000000000000000000000..01ff6a4fb5c1e34e5c1d2e76a3935e1e893eda8e --- /dev/null +++ b/TestSimpleLexer/Properties/AssemblyInfo.cs @@ -0,0 +1,35 @@ +п»їusing System.Reflection; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("TestSimpleLexer")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("TestSimpleLexer")] +[assembly: AssemblyCopyright("Copyright В© 2018")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("53767749-BCA6-4A3A-A4E6-D23519008305")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] \ No newline at end of file diff --git a/TestSimpleLexer/TestSimpleLexer.csproj b/TestSimpleLexer/TestSimpleLexer.csproj new file mode 100644 index 0000000000000000000000000000000000000000..c85ced49493f510654d8fc2e1df38a77b7430823 --- /dev/null +++ b/TestSimpleLexer/TestSimpleLexer.csproj @@ -0,0 +1,76 @@ +п»ї<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <Import Project="..\packages\NUnit.3.10.1\build\NUnit.props" Condition="Exists('..\packages\NUnit.3.10.1\build\NUnit.props')" /> + <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <ProjectGuid>{53767749-BCA6-4A3A-A4E6-D23519008305}</ProjectGuid> + <ProjectTypeGuids>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> + <OutputType>Library</OutputType> + <AppDesignerFolder>Properties</AppDesignerFolder> + <RootNamespace>TestSimpleLexer</RootNamespace> + <AssemblyName>TestSimpleLexer</AssemblyName> + <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> + <FileAlignment>512</FileAlignment> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> + <PlatformTarget>AnyCPU</PlatformTarget> + <DebugSymbols>true</DebugSymbols> + <DebugType>full</DebugType> + <Optimize>false</Optimize> + <OutputPath>bin\Debug\</OutputPath> + <DefineConstants>DEBUG;TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> + <PlatformTarget>AnyCPU</PlatformTarget> + <DebugType>pdbonly</DebugType> + <Optimize>true</Optimize> + <OutputPath>bin\Release\</OutputPath> + <DefineConstants>TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <ItemGroup> + <Reference Include="Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"> + <HintPath>C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.CSharp.dll</HintPath> + </Reference> + <Reference Include="nunit.framework, Version=3.10.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb"> + <HintPath>..\packages\NUnit.3.10.1\lib\net35\nunit.framework.dll</HintPath> + <Private>True</Private> + </Reference> + <Reference Include="System" /> + <Reference Include="System.Core" /> + <Reference Include="System.Data" /> + <Reference Include="System.Xml" /> + </ItemGroup> + <ItemGroup> + <Compile Include="Tests.cs" /> + <Compile Include="Properties\AssemblyInfo.cs" /> + </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\Module2\SimpleLangLexer\SimpleLexer.csproj"> + <Project>{28c0284b-2f43-45d6-a77f-ab08f919717d}</Project> + <Name>SimpleLexer</Name> + </ProjectReference> + </ItemGroup> + <ItemGroup> + <None Include="packages.config" /> + </ItemGroup> + <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> + <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild"> + <PropertyGroup> + <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}.</ErrorText> + </PropertyGroup> + <Error Condition="!Exists('..\packages\NUnit.3.10.1\build\NUnit.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\NUnit.3.10.1\build\NUnit.props'))" /> + </Target> + <!-- To modify your build process, add your task inside one of the targets below and uncomment it. + Other similar extension points exist, see Microsoft.Common.targets. + <Target Name="BeforeBuild"> + </Target> + <Target Name="AfterBuild"> + </Target> + --> +</Project> \ No newline at end of file diff --git a/TestSimpleLexer/Tests.cs b/TestSimpleLexer/Tests.cs new file mode 100644 index 0000000000000000000000000000000000000000..f88d7fd1b3d77ffb0ff0e159e0e03e2f5f1e0ef4 --- /dev/null +++ b/TestSimpleLexer/Tests.cs @@ -0,0 +1,356 @@ +п»їusing System; +using NUnit.Framework; +using System.IO; +using SimpleLexer; +using System.Collections.Generic; +using System.Linq; + + +namespace TestSimpleLexer +{ + + public class LexemList: List<KeyValuePair<Tok, string>> + { + public void Add(Tok key, string value) + { + var element = new KeyValuePair<Tok, string>(key, value); + this.Add(element); + } + } + + [TestFixture] + public class TestSimpleLexer + { + public static List< KeyValuePair<Tok, string> > getLexerOutput(Lexer l) + { + List< KeyValuePair<Tok, string> > result = new List< KeyValuePair<Tok, string> >(); + do + { + result.Add(new KeyValuePair<Tok, string>(l.LexKind, l.LexText)); + l.NextLexem(); + } while (l.LexKind != Tok.EOF); + + return result; + } + + [Test] + public void TestId() + { + string text = @"id123"; + TextReader inputReader = new StringReader(text); + Lexer l = new Lexer(inputReader); + try + { + var lexems = getLexerOutput(l); + Assert.IsTrue(lexems.Count == 1); + Assert.IsTrue(lexems.First().Key == Tok.ID); + Assert.IsTrue(lexems.First().Value == "id123"); + } + catch (LexerException e) + { + Assert.Fail(); + } + } + + } + + [TestFixture] + public class TestSimpleLexerOps + { + [Test] + public void TestOps() + { + string text = @",:+-* / -+ /**/"; + TextReader inputReader = new StringReader(text); + Lexer l = new Lexer(inputReader); + + var lexems = TestSimpleLexer.getLexerOutput(l); + Assert.IsTrue(lexems.Count == 12); + CollectionAssert.AreEqual(new LexemList() + { + {Tok.COMMA, ","}, + {Tok.COLON, ":"}, + {Tok.PLUS, "+"}, + {Tok.MINUS, "-"}, + {Tok.MULT, "*"}, + {Tok.DIVISION, "/"}, + {Tok.MINUS, "-"}, + {Tok.PLUS, "+"}, + {Tok.DIVISION, "/"}, + {Tok.MULT, "*"}, + {Tok.MULT, "*"}, + {Tok.DIVISION, "/"}, + + }.ToList(), lexems); + } + + [Test] + public void TestKeywords() + { + string text = @"div mod - and or not notmod anddiv modor *"; + TextReader inputReader = new StringReader(text); + Lexer l = new Lexer(inputReader); + + var lexems = TestSimpleLexer.getLexerOutput(l); + Assert.IsTrue(lexems.Count == 10); + CollectionAssert.AreEqual(new LexemList() + { + {Tok.DIV, "div"}, + {Tok.MOD, "mod"}, + {Tok.MINUS, "-"}, + {Tok.AND, "and"}, + {Tok.OR, "or"}, + {Tok.NOT, "not"}, + {Tok.ID, "notmod"}, + {Tok.ID, "anddiv"}, + {Tok.ID, "modor"}, + {Tok.MULT, "*"} + + }.ToList(), lexems); + } + + [Test] + public void TestOpsFail() + { + string text = @"-`-$ # @"; + TextReader inputReader = new StringReader(text); + Lexer l = new Lexer(inputReader); + Assert.Throws<LexerException>(() => + { + var lexems = TestSimpleLexer.getLexerOutput(l); + }); + } + + } + + [TestFixture] + public class TestSimpleLexerAssigns + { + [Test] + public void TestAssigns() + { + string text = @"+ = -= *= /= +==:=6-=-*+/"; + TextReader inputReader = new StringReader(text); + Lexer l = new Lexer(inputReader); + + var lexems = TestSimpleLexer.getLexerOutput(l); + Assert.IsTrue(lexems.Count == 14); + CollectionAssert.AreEqual(new LexemList() + { + {Tok.PLUS, "+"}, + {Tok.EQ, "="}, + {Tok.MINUSASSIGN, "-="}, + {Tok.MULTASSIGN, "*="}, + {Tok.DIVASSIGN, "/="}, + {Tok.PLUSASSIGN, "+="}, + {Tok.EQ, "="}, + {Tok.ASSIGN, ":="}, + {Tok.INUM, "6"}, + {Tok.MINUSASSIGN, "-="}, + {Tok.MINUS, "-"}, + {Tok.MULT, "*"}, + {Tok.PLUS, "+"}, + {Tok.DIVISION, "/"} + + }.ToList(), lexems); + } + + } + + [TestFixture] + public class TestSimpleLexerComparisons + { + [Test] + public void TestComparisons() + { + string text = @"><>>=<=+<> > <="; + TextReader inputReader = new StringReader(text); + Lexer l = new Lexer(inputReader); + + var lexems = TestSimpleLexer.getLexerOutput(l); + Assert.IsTrue(lexems.Count == 8); + CollectionAssert.AreEqual(new LexemList() + { + {Tok.GT, ">"}, + {Tok.NEQ, "<>"}, + {Tok.GEQ, ">="}, + {Tok.LEQ, "<="}, + {Tok.PLUS, "+"}, + {Tok.NEQ, "<>"}, + {Tok.GT, ">"}, + {Tok.LEQ, "<="} + + }.ToList(), lexems); + } + + [Test] + public void TestComparisonsAndOps() + { + string text = @">+6<>>=<= +<> >+=<= <=="; + TextReader inputReader = new StringReader(text); + Lexer l = new Lexer(inputReader); + + var lexems = TestSimpleLexer.getLexerOutput(l); + Assert.IsTrue(lexems.Count == 13); + CollectionAssert.AreEqual(new LexemList() + { + {Tok.GT, ">"}, + {Tok.PLUS, "+"}, + {Tok.INUM, "6"}, + {Tok.NEQ, "<>"}, + {Tok.GEQ, ">="}, + {Tok.LEQ, "<="}, + {Tok.PLUS, "+"}, + {Tok.NEQ, "<>"}, + {Tok.GT, ">"}, + {Tok.PLUSASSIGN, "+="}, + {Tok.LEQ, "<="}, + {Tok.LEQ, "<="}, + {Tok.EQ, "="} + + }.ToList(), lexems); + } + + } + + [TestFixture] + public class TestSimpleLexerLineCmt + { + [Test] + public void TestComment() + { + string text = @" ts:=623 // 23 sa 3"; + TextReader inputReader = new StringReader(text); + Lexer l = new Lexer(inputReader); + + var lexems = TestSimpleLexer.getLexerOutput(l); + Assert.IsTrue(lexems.Count == 3); + CollectionAssert.AreEqual(new LexemList() + { + {Tok.ID, "ts"}, + {Tok.ASSIGN, ":="}, + {Tok.INUM, "623"}, + + }.ToList(), lexems); + } + + [Test] public void TestCommentFileEnd() + { + string text = @" ts:=623 //"; + TextReader inputReader = new StringReader(text); + Lexer l = new Lexer(inputReader); + + var lexems = TestSimpleLexer.getLexerOutput(l); + Assert.IsTrue(lexems.Count == 3); + CollectionAssert.AreEqual(new LexemList() + { + {Tok.ID, "ts"}, + {Tok.ASSIGN, ":="}, + {Tok.INUM, "623"}, + + }.ToList(), lexems); + } + + [Test] public void TestCommentNextLine() + { + string text = @" ts:=623 // cmt + id := 22"; + TextReader inputReader = new StringReader(text); + Lexer l = new Lexer(inputReader); + + var lexems = TestSimpleLexer.getLexerOutput(l); + Assert.AreEqual(6, lexems.Count); + CollectionAssert.AreEqual(new LexemList() + { + {Tok.ID, "ts"}, + {Tok.ASSIGN, ":="}, + {Tok.INUM, "623"}, + {Tok.ID, "id"}, + {Tok.ASSIGN, ":="}, + {Tok.INUM, "22"}, + + }.ToList(), lexems); + } + + } + + [TestFixture] + public class TestSimpleLexerMultLineCmt + { + [Test] + public void TestMultLineComment() + { + string text = @" ts:=623 { 23 sa 3 }"; + TextReader inputReader = new StringReader(text); + Lexer l = new Lexer(inputReader); + + var lexems = TestSimpleLexer.getLexerOutput(l); + Assert.IsTrue(lexems.Count == 3); + CollectionAssert.AreEqual(new LexemList() + { + {Tok.ID, "ts"}, + {Tok.ASSIGN, ":="}, + {Tok.INUM, "623"}, + + }.ToList(), lexems); + } + + [Test] + public void TestCommentFileEnd() + { + string text = @" ts:=623 { }"; + TextReader inputReader = new StringReader(text); + Lexer l = new Lexer(inputReader); + + var lexems = TestSimpleLexer.getLexerOutput(l); + Assert.IsTrue(lexems.Count == 3); + CollectionAssert.AreEqual(new LexemList() + { + {Tok.ID, "ts"}, + {Tok.ASSIGN, ":="}, + {Tok.INUM, "623"}, + + }.ToList(), lexems); + } + + [Test] + public void TestCommentNextLine() + { + string text = @" ts:=623 { cmt + cmt } id := 22"; + TextReader inputReader = new StringReader(text); + Lexer l = new Lexer(inputReader); + + var lexems = TestSimpleLexer.getLexerOutput(l); + Assert.AreEqual(6, lexems.Count); + CollectionAssert.AreEqual(new LexemList() + { + {Tok.ID, "ts"}, + {Tok.ASSIGN, ":="}, + {Tok.INUM, "623"}, + {Tok.ID, "id"}, + {Tok.ASSIGN, ":="}, + {Tok.INUM, "22"}, + + }.ToList(), lexems); + } + + [Test] + public void TestCommentNotClosed() + { + string text = @" ts:=623 { cmt + cmt id := 22"; + TextReader inputReader = new StringReader(text); + Lexer l = new Lexer(inputReader); + + Assert.Throws<LexerException>(() => + { + var lexems = TestSimpleLexer.getLexerOutput(l); + }); + + + } + + } + +} \ No newline at end of file diff --git a/TestSimpleLexer/packages.config b/TestSimpleLexer/packages.config new file mode 100644 index 0000000000000000000000000000000000000000..6386acbb2c0793a01af2210c3c040b325a0ba6bd --- /dev/null +++ b/TestSimpleLexer/packages.config @@ -0,0 +1,4 @@ +п»ї<?xml version="1.0" encoding="utf-8"?> +<packages> + <package id="NUnit" version="3.10.1" targetFramework="net35" /> +</packages> \ No newline at end of file diff --git a/TestVisitors/Properties/AssemblyInfo.cs b/TestVisitors/Properties/AssemblyInfo.cs new file mode 100644 index 0000000000000000000000000000000000000000..eb605513713b05e1e3352ea865f7a396552367a2 --- /dev/null +++ b/TestVisitors/Properties/AssemblyInfo.cs @@ -0,0 +1,35 @@ +п»їusing System.Reflection; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("TestVisitors")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("TestVisitors")] +[assembly: AssemblyCopyright("Copyright В© 2018")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("365C36ED-B8E4-428A-9AF4-A3142E06FE1E")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] \ No newline at end of file diff --git a/TestVisitors/TestVisitors.csproj b/TestVisitors/TestVisitors.csproj new file mode 100644 index 0000000000000000000000000000000000000000..9513e9de2b4c48a690888e32741115a06b6651e9 --- /dev/null +++ b/TestVisitors/TestVisitors.csproj @@ -0,0 +1,73 @@ +п»ї<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <Import Project="..\packages\NUnit.3.10.1\build\NUnit.props" Condition="Exists('..\packages\NUnit.3.10.1\build\NUnit.props')" /> + <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> + <PropertyGroup> + <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> + <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> + <ProjectGuid>{365C36ED-B8E4-428A-9AF4-A3142E06FE1E}</ProjectGuid> + <ProjectTypeGuids>{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids> + <OutputType>Library</OutputType> + <AppDesignerFolder>Properties</AppDesignerFolder> + <RootNamespace>TestVisitors</RootNamespace> + <AssemblyName>TestVisitors</AssemblyName> + <TargetFrameworkVersion>v3.5</TargetFrameworkVersion> + <FileAlignment>512</FileAlignment> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> + <PlatformTarget>AnyCPU</PlatformTarget> + <DebugSymbols>true</DebugSymbols> + <DebugType>full</DebugType> + <Optimize>false</Optimize> + <OutputPath>bin\Debug\</OutputPath> + <DefineConstants>DEBUG;TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> + <PlatformTarget>AnyCPU</PlatformTarget> + <DebugType>pdbonly</DebugType> + <Optimize>true</Optimize> + <OutputPath>bin\Release\</OutputPath> + <DefineConstants>TRACE</DefineConstants> + <ErrorReport>prompt</ErrorReport> + <WarningLevel>4</WarningLevel> + </PropertyGroup> + <ItemGroup> + <Reference Include="nunit.framework, Version=3.10.1.0, Culture=neutral, PublicKeyToken=2638cd05610744eb"> + <HintPath>..\packages\NUnit.3.10.1\lib\net35\nunit.framework.dll</HintPath> + <Private>True</Private> + </Reference> + <Reference Include="System" /> + <Reference Include="System.Core" /> + <Reference Include="System.Data" /> + <Reference Include="System.Xml" /> + </ItemGroup> + <ItemGroup> + <Compile Include="Tests.cs" /> + <Compile Include="Properties\AssemblyInfo.cs" /> + </ItemGroup> + <ItemGroup> + <None Include="packages.config" /> + </ItemGroup> + <ItemGroup> + <ProjectReference Include="..\Module7\ParserVistors.csproj"> + <Project>{5f9f534d-dde0-4b4a-9bc2-2af8c9ed83fa}</Project> + <Name>ParserVistors</Name> + </ProjectReference> + </ItemGroup> + <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> + <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild"> + <PropertyGroup> + <ErrorText>This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105.The missing file is {0}.</ErrorText> + </PropertyGroup> + <Error Condition="!Exists('..\packages\NUnit.3.10.1\build\NUnit.props')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\NUnit.3.10.1\build\NUnit.props'))" /> + </Target> + <!-- To modify your build process, add your task inside one of the targets below and uncomment it. + Other similar extension points exist, see Microsoft.Common.targets. + <Target Name="BeforeBuild"> + </Target> + <Target Name="AfterBuild"> + </Target> + --> +</Project> \ No newline at end of file diff --git a/TestVisitors/Tests.cs b/TestVisitors/Tests.cs new file mode 100644 index 0000000000000000000000000000000000000000..c73110f0634db59582d7ddee1fa6cf21f7bb4d43 --- /dev/null +++ b/TestVisitors/Tests.cs @@ -0,0 +1,210 @@ +using System; +using NUnit.Framework; +using SimpleScanner; +using SimpleParser; +using SimpleLang.Visitors; + +namespace TestVisitors +{ + public class TestHelpers + { + public static Parser Parse(string text) + { + Scanner scanner = new Scanner(); + scanner.SetSource(text, 0); + + Parser parser = new Parser(scanner); + return parser; + } + } + + [TestFixture] + public class TestAvgOpCount + { + [Test] + public void NoLoopTest() + { + Parser p = TestHelpers.Parse(@"begin end "); + Assert.IsTrue(p.Parse()); + var avgCounter = new CountCyclesOpVisitor(); + p.root.Visit(avgCounter); + Assert.AreEqual(0, avgCounter.MidCount()); + } + + [Test] + public void ThreeLoopsTest() + { + Parser p = TestHelpers.Parse(@"begin + var a,b,d; + b := 2; + a := 3; + a := a * 4 + b;;; + cycle 3 + begin + a := a + 1; + cycle 3 + begin + a := 1; + a := 1; + a := 1; + a := 1; + a := 1; + a := 1; + a := 1; + a := 1; + a := 1; + a := 1; + end; + write(a) + end; + cycle 3 + begin + d := 2 + end + end"); + Assert.IsTrue(p.Parse()); + var avgCounter = new CountCyclesOpVisitor(); + p.root.Visit(avgCounter); + Assert.AreEqual(4, avgCounter.MidCount()); + } + } + + [TestFixture] + public class TestCommonVariable + { + [Test] + public void OneVarTest() + { + Parser p = TestHelpers.Parse(@"begin var a; a:=2; a:=a+2*a-3; a:=3; end "); + Assert.IsTrue(p.Parse()); + var varCounter = new CommonlyUsedVarVisitor(); + p.root.Visit(varCounter); + Assert.AreEqual("a", varCounter.mostCommonlyUsedVar()); + } + + [Test] + public void ManyVarTest() + { + Parser p = TestHelpers.Parse(@"begin var a,b,c; a:=2+c-b; b:=a+2*a-3-b+b-b+b+b; b:=c-3+b-3; end "); + Assert.IsTrue(p.Parse()); + var varCounter = new CommonlyUsedVarVisitor(); + p.root.Visit(varCounter); + Assert.AreEqual("b", varCounter.mostCommonlyUsedVar()); + } + } + + [TestFixture] + public class TestExprComplexity + { + [Test] + public void AssignTest() + { + Parser p = TestHelpers.Parse(@"begin var a; a:=2+2; a:=a+2*a-3; a:=3; end "); + Assert.IsTrue(p.Parse()); + var exprMeter = new ExprComplexityVisitor(); + p.root.Visit(exprMeter); + var resultList = exprMeter.getComplexityList(); + CollectionAssert.AreEqual(new int[] {1, 5, 0}, resultList); + } + + [Test] + public void CycleTest() + { + Parser p = TestHelpers.Parse(@"begin var a; cycle 2+2/3 a:=2-2 end "); + Assert.IsTrue(p.Parse()); + var exprMeter = new ExprComplexityVisitor(); + p.root.Visit(exprMeter); + var resultList = exprMeter.getComplexityList(); + CollectionAssert.AreEqual(new int[] {4, 1}, resultList); + } + + [Test] + public void WriteTest() + { + Parser p = TestHelpers.Parse(@"begin write(2+2-3) end "); + Assert.IsTrue(p.Parse()); + var exprMeter = new ExprComplexityVisitor(); + p.root.Visit(exprMeter); + var resultList = exprMeter.getComplexityList(); + CollectionAssert.AreEqual(new int[] {2}, resultList); + } + + [TestFixture] + public class TestLoopNestVisitor + { + [Test] + public void OneLoopTest() + { + Parser p = TestHelpers.Parse(@"begin cycle 2 write(2) end"); + Assert.IsTrue(p.Parse()); + var loopCounter = new MaxNestCyclesVisitor(); + p.root.Visit(loopCounter); + Assert.AreEqual(1, loopCounter.MaxNest); + } + + [Test] + public void ThreeLoopsTest() + { + Parser p = TestHelpers.Parse(@"begin cycle 2 cycle 3 cycle 4 write(5) end"); + Assert.IsTrue(p.Parse()); + var loopCounter = new MaxNestCyclesVisitor(); + p.root.Visit(loopCounter); + Assert.AreEqual(3, loopCounter.MaxNest); + } + + [Test] + public void LoopTreeTest() + { + Parser p = TestHelpers.Parse(@"begin var a; + cycle 2 + cycle 1 + a:=2; + cycle 3 + cycle 5 + cycle 6 + a:=5; + cycle 4 + write(5) + end"); + Assert.IsTrue(p.Parse()); + var loopCounter = new MaxNestCyclesVisitor(); + p.root.Visit(loopCounter); + Assert.AreEqual(4, loopCounter.MaxNest); + } + + } + + } + + [TestFixture] + public class TestVariableRenamer + { + [Test] + public void SimpleTest() + { + Parser p = TestHelpers.Parse(@"begin var a; a:=2; a:=a+2*a-3; a:=3; end "); + Assert.IsTrue(p.Parse()); + var varRenamer = new ChangeVarIdVisitor("a", "z"); + p.root.Visit(varRenamer); + + var varCounter = new CommonlyUsedVarVisitor(); + p.root.Visit(varCounter); + Assert.AreEqual("z", varCounter.mostCommonlyUsedVar()); + } + } + + [TestFixture] + public class TestIfCycleNest + { + [Test] + public void FirstTest() + { + Parser p = TestHelpers.Parse(@"begin var a; cycle 3 if 1 then cycle 4 a := 1 end "); + Assert.IsTrue(p.Parse()); + var nestWalker = new MaxIfCycleNestVisitor(); + p.root.Visit(nestWalker); + Assert.AreEqual(3, nestWalker.MaxNest); + } + } + +} \ No newline at end of file diff --git a/TestVisitors/packages.config b/TestVisitors/packages.config new file mode 100644 index 0000000000000000000000000000000000000000..6386acbb2c0793a01af2210c3c040b325a0ba6bd --- /dev/null +++ b/TestVisitors/packages.config @@ -0,0 +1,4 @@ +п»ї<?xml version="1.0" encoding="utf-8"?> +<packages> + <package id="NUnit" version="3.10.1" targetFramework="net35" /> +</packages> \ No newline at end of file diff --git a/build.sh b/build.sh new file mode 100755 index 0000000000000000000000000000000000000000..1c2c77b58dced806736a1e93f1c0733700324eed --- /dev/null +++ b/build.sh @@ -0,0 +1,2 @@ +#!/bin/bash +xbuild /p:TargetFrameworkVersion="v4.0" \ No newline at end of file diff --git a/run_tests.sh b/run_tests.sh new file mode 100755 index 0000000000000000000000000000000000000000..5f65f9859c0de073ce5542119b4d46f50e9b2526 --- /dev/null +++ b/run_tests.sh @@ -0,0 +1,9 @@ +#!/bin/bash +mono NUnit.3.10.1/NUnit.ConsoleRunner.3.9.0/tools/nunit3-console.exe --labels=All TestLexer/bin/Debug/TestLexer.dll +mono NUnit.3.10.1/NUnit.ConsoleRunner.3.9.0/tools/nunit3-console.exe --labels=All TestSimpleLexer/bin/Debug/TestSimpleLexer.dll +mono NUnit.3.10.1/NUnit.ConsoleRunner.3.9.0/tools/nunit3-console.exe --labels=All TestGeneratedLexer/bin/Debug/TestGeneratedLexer.dll +mono NUnit.3.10.1/NUnit.ConsoleRunner.3.9.0/tools/nunit3-console.exe --labels=All TestDescentParser/bin/Debug/TestDescentParser.dll +mono NUnit.3.10.1/NUnit.ConsoleRunner.3.9.0/tools/nunit3-console.exe --labels=All TestGeneratedParser/bin/Debug/TestGeneratedParser.dll +mono NUnit.3.10.1/NUnit.ConsoleRunner.3.9.0/tools/nunit3-console.exe --labels=All TestASTParser/bin/Debug/TestASTParser.dll +mono NUnit.3.10.1/NUnit.ConsoleRunner.3.9.0/tools/nunit3-console.exe --labels=All TestVisitors/bin/Debug/TestVisitors.dll +mono NUnit.3.10.1/NUnit.ConsoleRunner.3.9.0/tools/nunit3-console.exe --labels=All TestCodeGenerator/bin/Debug/TestCodeGenerator.dll