diff --git a/Module1/packages.config b/Module1/packages.config new file mode 100644 index 0000000000000000000000000000000000000000..14b53d1ecb2e718f6d9c15e6fcb20c081a72bdd1 --- /dev/null +++ b/Module1/packages.config @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="utf-8"?> +<packages> + <package id="NUnit" version="3.13.3" targetFramework="net40" /> + <package id="NUnit.ConsoleRunner" version="3.15.2" targetFramework="net40" /> + <package id="NUnit3TestAdapter" version="4.2.1" targetFramework="net40" /> +</packages> \ No newline at end of file diff --git a/Module2/SimpleLangLexer/packages.config b/Module2/SimpleLangLexer/packages.config new file mode 100644 index 0000000000000000000000000000000000000000..14b53d1ecb2e718f6d9c15e6fcb20c081a72bdd1 --- /dev/null +++ b/Module2/SimpleLangLexer/packages.config @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="utf-8"?> +<packages> + <package id="NUnit" version="3.13.3" targetFramework="net40" /> + <package id="NUnit.ConsoleRunner" version="3.15.2" targetFramework="net40" /> + <package id="NUnit3TestAdapter" version="4.2.1" targetFramework="net40" /> +</packages> \ No newline at end of file diff --git a/Module2/SimpleLexerDemo/packages.config b/Module2/SimpleLexerDemo/packages.config new file mode 100644 index 0000000000000000000000000000000000000000..14b53d1ecb2e718f6d9c15e6fcb20c081a72bdd1 --- /dev/null +++ b/Module2/SimpleLexerDemo/packages.config @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="utf-8"?> +<packages> + <package id="NUnit" version="3.13.3" targetFramework="net40" /> + <package id="NUnit.ConsoleRunner" version="3.15.2" targetFramework="net40" /> + <package id="NUnit3TestAdapter" version="4.2.1" targetFramework="net40" /> +</packages> \ No newline at end of file diff --git a/Module4/SimpleLangParser/SimpleLangParser.cs b/Module4/SimpleLangParser/SimpleLangParser.cs index 9cb9109f725b79b7a98073cd699adbfccb2e6e06..743f1546760751db68b54ff468ab6a543035cec5 100644 --- a/Module4/SimpleLangParser/SimpleLangParser.cs +++ b/Module4/SimpleLangParser/SimpleLangParser.cs @@ -27,17 +27,67 @@ namespace SimpleLangParser Block(); } - public void Expr() + /*public void Expr() { + if (l.LexKind == Tok.ID || l.LexKind == Tok.INUM || l.LexKind == Tok.PLUS || l.LexKind == Tok.MINUS || l.LexKind == Tok.MULT || l.LexKind == Tok.DIVISION || l.LexKind == Tok.RIGHT_BRACKET || l.LexKind == Tok.LEFT_BRACKET) while (l.LexKind == Tok.ID || l.LexKind == Tok.INUM || l.LexKind == Tok.PLUS || l.LexKind == Tok.MINUS || l.LexKind == Tok.MULT || l.LexKind == Tok.DIVISION || l.LexKind == Tok.RIGHT_BRACKET || l.LexKind == Tok.LEFT_BRACKET) l.NextLexem(); //if (l.LexKind != Tok.ID && l.LexKind != Tok.INUM && l.LexKind != Tok.PLUS && l.LexKind != Tok.MINUS && l.LexKind != Tok.MULT && l.LexKind != Tok.DIVISION && l.LexKind != Tok.RIGHT_BRACKET && l.LexKind != Tok.LEFT_BRACKET) else SyntaxError("expression expected"); - } - - public void Assign() + }*/ + + public void Expr() + { + T(); + A(); + } + public void T() + { + M(); + B(); + } + public void A() + { + if (l.LexKind == Tok.MINUS || l.LexKind == Tok.PLUS) + { + l.NextLexem(); + T(); + A(); + } + } + public void M() + { + if (l.LexKind == Tok.ID) + { + l.NextLexem(); + } + if (l.LexKind == Tok.INUM) + { + l.NextLexem(); + } + if (l.LexKind == Tok.LEFT_BRACKET) + { + l.NextLexem(); + Expr(); + if (l.LexKind == Tok.RIGHT_BRACKET) + l.NextLexem(); + else + SyntaxError(") "); + } + } + public void B() + { + if (l.LexKind == Tok.DIVISION || l.LexKind == Tok.MULT) + { + l.NextLexem(); + M(); + B(); + } + } + + public void Assign() { l.NextLexem(); // пропуск id if (l.LexKind == Tok.ASSIGN) diff --git a/Module4/SimpleLangParser/packages.config b/Module4/SimpleLangParser/packages.config new file mode 100644 index 0000000000000000000000000000000000000000..14b53d1ecb2e718f6d9c15e6fcb20c081a72bdd1 --- /dev/null +++ b/Module4/SimpleLangParser/packages.config @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="utf-8"?> +<packages> + <package id="NUnit" version="3.13.3" targetFramework="net40" /> + <package id="NUnit.ConsoleRunner" version="3.15.2" targetFramework="net40" /> + <package id="NUnit3TestAdapter" version="4.2.1" targetFramework="net40" /> +</packages> \ No newline at end of file diff --git a/Module4/SimpleLangParserTest/packages.config b/Module4/SimpleLangParserTest/packages.config new file mode 100644 index 0000000000000000000000000000000000000000..14b53d1ecb2e718f6d9c15e6fcb20c081a72bdd1 --- /dev/null +++ b/Module4/SimpleLangParserTest/packages.config @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="utf-8"?> +<packages> + <package id="NUnit" version="3.13.3" targetFramework="net40" /> + <package id="NUnit.ConsoleRunner" version="3.15.2" targetFramework="net40" /> + <package id="NUnit3TestAdapter" version="4.2.1" targetFramework="net40" /> +</packages> \ No newline at end of file diff --git a/Module5/SimpleLex.cs b/Module5/SimpleLex.cs index 868c03ed8aac52c487673aa76226398614f3039f..934e216800105026c3de2aeeee2ca4410e1a0bf9 100644 --- a/Module5/SimpleLex.cs +++ b/Module5/SimpleLex.cs @@ -1,9 +1,9 @@ // // This CSharp output file generated by Gardens Point LEX // Version: 1.1.3.301 -// Machine: LAPTOP-8RKO19AH -// DateTime: 18.10.2022 0:13:27 -// UserName: luspa +// Machine: MIL8A-202A-05 +// DateTime: 24.10.2022 12:52:53 +// UserName: user // GPLEX input file <SimpleLex.lex> // GPLEX frame file <embedded resource> // @@ -123,8 +123,8 @@ namespace SimpleScanner enum Result {accept, noMatch, contextFound}; - const int maxAccept = 7; - const int initial = 8; + const int maxAccept = 14; + const int initial = 15; const int eofNum = 0; const int goStart = -1; const int INITIAL = 0; @@ -161,24 +161,24 @@ namespace SimpleScanner } }; - static int[] startState = new int[] {8, 0}; + static int[] startState = new int[] {15, 0}; #region CompressedCharacterMap // - // There are 8 equivalence classes + // There are 15 equivalence classes // There are 2 character sequence regions // There are 1 tables, 123 entries // There are 1 runs, 0 singletons // Decision tree depth is 1 // static sbyte[] mapC0 = new sbyte[123] { -/* '\0' */ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0, 7, 7, 0, 7, 7, -/* '\x10' */ 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, -/* '\x20' */ 0, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 2, 7, -/* '0' */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 6, 7, 5, 7, 7, -/* '@' */ 7, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, -/* 'P' */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 7, 7, 7, 7, 3, -/* '`' */ 7, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, +/* '\0' */ 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 0, 14, 14, 0, 14, 14, +/* '\x10' */ 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, +/* '\x20' */ 0, 14, 14, 14, 14, 14, 14, 14, 7, 8, 12, 10, 9, 11, 2, 13, +/* '0' */ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, 6, 14, 5, 14, 14, +/* '@' */ 14, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, +/* 'P' */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 14, 14, 14, 14, 3, +/* '`' */ 14, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 'p' */ 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3 }; static sbyte MapC(int code) @@ -186,22 +186,29 @@ namespace SimpleScanner if (code < 123) // '\0' <= code <= 'z' return mapC0[code - 0]; else // '{' <= code <= '\U0010FFFF' - return (sbyte)7; + return (sbyte)14; } #endregion - static Table[] NxS = new Table[10] { + static Table[] NxS = new Table[17] { /* NxS[ 0] */ new Table(0, 0, 0, null), -/* NxS[ 1] */ new Table(1, 2, -1, new sbyte[] {1, 9}), +/* NxS[ 1] */ new Table(1, 2, -1, new sbyte[] {1, 16}), /* NxS[ 2] */ new Table(0, 0, -1, null), /* NxS[ 3] */ new Table(1, 3, -1, new sbyte[] {3, -1, 3}), -/* NxS[ 4] */ new Table(5, 1, -1, new sbyte[] {6}), +/* NxS[ 4] */ new Table(5, 1, -1, new sbyte[] {13}), /* NxS[ 5] */ new Table(0, 0, -1, null), /* NxS[ 6] */ new Table(0, 0, -1, null), -/* NxS[ 7] */ new Table(1, 1, -1, new sbyte[] {7}), -/* NxS[ 8] */ new Table(1, 7, -1, new sbyte[] {1, 2, 3, 4, 2, 5, - 2}), -/* NxS[ 9] */ new Table(1, 1, -1, new sbyte[] {7}), +/* NxS[ 7] */ new Table(0, 0, -1, null), +/* NxS[ 8] */ new Table(0, 0, -1, null), +/* NxS[ 9] */ new Table(0, 0, -1, null), +/* NxS[ 10] */ new Table(0, 0, -1, null), +/* NxS[ 11] */ new Table(0, 0, -1, null), +/* NxS[ 12] */ new Table(0, 0, -1, null), +/* NxS[ 13] */ new Table(0, 0, -1, null), +/* NxS[ 14] */ new Table(1, 1, -1, new sbyte[] {14}), +/* NxS[ 15] */ new Table(1, 14, -1, new sbyte[] {1, 2, 3, 4, 2, 5, + 6, 7, 8, 9, 10, 11, 12, 2}), +/* NxS[ 16] */ new Table(1, 1, -1, new sbyte[] {14}), }; int NextState() { @@ -211,7 +218,7 @@ int NextState() { unchecked { int rslt; int idx = MapC(code) - NxS[state].min; - if (idx < 0) idx += 8; + if (idx < 0) idx += 15; if ((uint)idx >= (uint)NxS[state].rng) rslt = NxS[state].dflt; else rslt = NxS[state].nxt[idx]; return rslt; @@ -647,9 +654,30 @@ int res = ScannerHelper.GetIDToken(yytext); return (int)Tokens.SEMICOLON; break; case 6: -return (int)Tokens.ASSIGN; +return (int)Tokens.LEFT_PR; break; case 7: +return (int)Tokens.RIGHT_PR; + break; + case 8: +return (int)Tokens.COMMA; + break; + case 9: +return (int)Tokens.PLUS; + break; + case 10: +return (int)Tokens.MINUS; + break; + case 11: +return (int)Tokens.MULT; + break; + case 12: +return (int)Tokens.DIVISION; + break; + case 13: +return (int)Tokens.ASSIGN; + break; + case 14: return (int)Tokens.RNUM; break; default: @@ -764,6 +792,17 @@ class ScannerHelper keywords.Add("begin",(int)Tokens.BEGIN); keywords.Add("end",(int)Tokens.END); keywords.Add("cycle",(int)Tokens.CYCLE); + keywords.Add("while",(int)Tokens.WHILE); + keywords.Add("do",(int)Tokens.DO); + keywords.Add("repeat",(int)Tokens.REPEAT); + keywords.Add("until",(int)Tokens.UNTIL); + keywords.Add("for",(int)Tokens.FOR); + keywords.Add("to",(int)Tokens.TO); + keywords.Add("write",(int)Tokens.WRITE); + keywords.Add("if",(int)Tokens.IF); + keywords.Add("then",(int)Tokens.THEN); + keywords.Add("else",(int)Tokens.ELSE); + keywords.Add("var",(int)Tokens.VAR); } public static int GetIDToken(string s) { diff --git a/Module5/SimpleLex.lex b/Module5/SimpleLex.lex index 10547e9b9695423ba9e54065df5945d19fd597a9..5c023f1ad6dfe6446e3a8d259c069f17f8f2ce70 100644 --- a/Module5/SimpleLex.lex +++ b/Module5/SimpleLex.lex @@ -28,6 +28,13 @@ ID {Alpha}{AlphaDigit}* ":=" { return (int)Tokens.ASSIGN; } ";" { return (int)Tokens.SEMICOLON; } +"(" { return (int)Tokens.LEFT_PR; } +")" { return (int)Tokens.RIGHT_PR; } +"," { return (int)Tokens.COMMA; } +"+" { return (int)Tokens.PLUS; } +"-" { return (int)Tokens.MINUS; } +"*" { return (int)Tokens.MULT; } +"/" { return (int)Tokens.DIVISION; } [^ \r\n] { LexError(); @@ -63,6 +70,17 @@ class ScannerHelper keywords.Add("begin",(int)Tokens.BEGIN); keywords.Add("end",(int)Tokens.END); keywords.Add("cycle",(int)Tokens.CYCLE); + keywords.Add("while",(int)Tokens.WHILE); + keywords.Add("do",(int)Tokens.DO); + keywords.Add("repeat",(int)Tokens.REPEAT); + keywords.Add("until",(int)Tokens.UNTIL); + keywords.Add("for",(int)Tokens.FOR); + keywords.Add("to",(int)Tokens.TO); + keywords.Add("write",(int)Tokens.WRITE); + keywords.Add("if",(int)Tokens.IF); + keywords.Add("then",(int)Tokens.THEN); + keywords.Add("else",(int)Tokens.ELSE); + keywords.Add("var",(int)Tokens.VAR); } public static int GetIDToken(string s) { diff --git a/Module5/SimpleYacc.cs b/Module5/SimpleYacc.cs index a09c90871b0162a6aaa47aff63db4c35faf8fc65..532ce07dfcc03ae132e38b801fdf30d946137874 100644 --- a/Module5/SimpleYacc.cs +++ b/Module5/SimpleYacc.cs @@ -3,9 +3,9 @@ // (see accompanying GPPGcopyright.rtf) // GPPG version 1.3.6 -// Machine: LAPTOP-8RKO19AH -// DateTime: 18.10.2022 0:13:27 -// UserName: luspa +// Machine: MIL8A-202A-05 +// DateTime: 24.10.2022 12:52:53 +// UserName: user // Input file <SimpleYacc.y> // options: no-lines gplex @@ -20,7 +20,10 @@ 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}; + RNUM=7,ID=8,ASSIGN=9,SEMICOLON=10,WHILE=11,DO=12, + REPEAT=13,UNTIL=14,FOR=15,TO=16,WRITE=17,LEFT_PR=18, + RIGHT_PR=19,IF=20,ELSE=21,THEN=22,VAR=23,COMMA=24, + PLUS=25,MINUS=26,MULT=27,DIVISION=28,IFX=29}; // Abstract base class for GPLEX scanners public abstract class ScanBase : AbstractScanner<int,LexLocation> { @@ -39,35 +42,84 @@ public class Parser: ShiftReduceParser<int, LexLocation> #pragma warning disable 649 private static Dictionary<int, string> aliasses; #pragma warning restore 649 - private static Rule[] rules = new Rule[14]; - private static State[] states = new State[22]; + private static Rule[] rules = new Rule[36]; + private static State[] states = new State[70]; private static string[] nonTerms = new string[] { "progr", "$accept", "block", "stlist", "statement", "assign", "cycle", - "ident", "expr", }; + "while", "repeat", "for", "write", "if", "var", "ident", "expr", "idlist", + "T", "F", }; 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[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[4] = new State(new int[]{8,18,3,4,5,31,11,35,13,40,15,46,17,53,20,58,23,65},new int[]{-4,5,-5,44,-6,9,-14,10,-3,29,-7,30,-8,34,-9,39,-10,45,-11,52,-12,57,-13,64}); states[5] = new State(new int[]{4,6,10,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[6] = new State(-16); + states[7] = new State(new int[]{8,18,3,4,5,31,11,35,13,40,15,46,17,53,20,58,23,65},new int[]{-5,8,-6,9,-14,10,-3,29,-7,30,-8,34,-9,39,-10,45,-11,52,-12,57,-13,64}); 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[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[20] = new State(-13); - states[21] = new State(-3); + states[11] = new State(new int[]{8,18,6,19,18,20},new int[]{-15,12,-17,28,-18,27,-14,17}); + states[12] = new State(new int[]{25,13,26,23,4,-15,10,-15,14,-15,21,-15,16,-15}); + states[13] = new State(new int[]{8,18,6,19,18,20},new int[]{-17,14,-18,27,-14,17}); + states[14] = new State(new int[]{27,15,28,25,25,-28,26,-28,4,-28,10,-28,14,-28,21,-28,16,-28,19,-28,8,-28,3,-28,5,-28,11,-28,13,-28,15,-28,17,-28,20,-28,23,-28,12,-28,22,-28}); + states[15] = new State(new int[]{8,18,6,19,18,20},new int[]{-18,16,-14,17}); + states[16] = new State(-31); + states[17] = new State(-33); + states[18] = new State(-14); + states[19] = new State(-34); + states[20] = new State(new int[]{8,18,6,19,18,20},new int[]{-15,21,-17,28,-18,27,-14,17}); + states[21] = new State(new int[]{19,22,25,13,26,23}); + states[22] = new State(-35); + states[23] = new State(new int[]{8,18,6,19,18,20},new int[]{-17,24,-18,27,-14,17}); + states[24] = new State(new int[]{27,15,28,25,25,-29,26,-29,4,-29,10,-29,14,-29,21,-29,16,-29,19,-29,8,-29,3,-29,5,-29,11,-29,13,-29,15,-29,17,-29,20,-29,23,-29,12,-29,22,-29}); + states[25] = new State(new int[]{8,18,6,19,18,20},new int[]{-18,26,-14,17}); + states[26] = new State(-32); + states[27] = new State(-30); + states[28] = new State(new int[]{27,15,28,25,25,-27,26,-27,4,-27,10,-27,14,-27,21,-27,16,-27,19,-27,8,-27,3,-27,5,-27,11,-27,13,-27,15,-27,17,-27,20,-27,23,-27,12,-27,22,-27}); + states[29] = new State(-6); + states[30] = new State(-7); + states[31] = new State(new int[]{8,18,6,19,18,20},new int[]{-15,32,-17,28,-18,27,-14,17}); + states[32] = new State(new int[]{25,13,26,23,8,18,3,4,5,31,11,35,13,40,15,46,17,53,20,58,23,65},new int[]{-5,33,-6,9,-14,10,-3,29,-7,30,-8,34,-9,39,-10,45,-11,52,-12,57,-13,64}); + states[33] = new State(-17); + states[34] = new State(-8); + states[35] = new State(new int[]{8,18,6,19,18,20},new int[]{-15,36,-17,28,-18,27,-14,17}); + states[36] = new State(new int[]{12,37,25,13,26,23}); + states[37] = new State(new int[]{8,18,3,4,5,31,11,35,13,40,15,46,17,53,20,58,23,65},new int[]{-5,38,-6,9,-14,10,-3,29,-7,30,-8,34,-9,39,-10,45,-11,52,-12,57,-13,64}); + states[38] = new State(-18); + states[39] = new State(-9); + states[40] = new State(new int[]{8,18,3,4,5,31,11,35,13,40,15,46,17,53,20,58,23,65},new int[]{-4,41,-5,44,-6,9,-14,10,-3,29,-7,30,-8,34,-9,39,-10,45,-11,52,-12,57,-13,64}); + states[41] = new State(new int[]{14,42,10,7}); + states[42] = new State(new int[]{8,18,6,19,18,20},new int[]{-15,43,-17,28,-18,27,-14,17}); + states[43] = new State(new int[]{25,13,26,23,4,-19,10,-19,14,-19,21,-19}); + states[44] = new State(-3); + states[45] = new State(-10); + states[46] = new State(new int[]{8,18},new int[]{-6,47,-14,10}); + states[47] = new State(new int[]{16,48}); + states[48] = new State(new int[]{8,18,6,19,18,20},new int[]{-15,49,-17,28,-18,27,-14,17}); + states[49] = new State(new int[]{12,50,25,13,26,23}); + states[50] = new State(new int[]{8,18,3,4,5,31,11,35,13,40,15,46,17,53,20,58,23,65},new int[]{-5,51,-6,9,-14,10,-3,29,-7,30,-8,34,-9,39,-10,45,-11,52,-12,57,-13,64}); + states[51] = new State(-20); + states[52] = new State(-11); + states[53] = new State(new int[]{18,54}); + states[54] = new State(new int[]{8,18,6,19,18,20},new int[]{-15,55,-17,28,-18,27,-14,17}); + states[55] = new State(new int[]{19,56,25,13,26,23}); + states[56] = new State(-21); + states[57] = new State(-12); + states[58] = new State(new int[]{8,18,6,19,18,20},new int[]{-15,59,-17,28,-18,27,-14,17}); + states[59] = new State(new int[]{22,60,25,13,26,23}); + states[60] = new State(new int[]{8,18,3,4,5,31,11,35,13,40,15,46,17,53,20,58,23,65},new int[]{-5,61,-6,9,-14,10,-3,29,-7,30,-8,34,-9,39,-10,45,-11,52,-12,57,-13,64}); + states[61] = new State(new int[]{21,62,4,-22,10,-22,14,-22}); + states[62] = new State(new int[]{8,18,3,4,5,31,11,35,13,40,15,46,17,53,20,58,23,65},new int[]{-5,63,-6,9,-14,10,-3,29,-7,30,-8,34,-9,39,-10,45,-11,52,-12,57,-13,64}); + states[63] = new State(-23); + states[64] = new State(-13); + states[65] = new State(new int[]{8,69},new int[]{-16,66}); + states[66] = new State(new int[]{24,67,4,-26,10,-26,14,-26,21,-26}); + states[67] = new State(new int[]{8,68}); + states[68] = new State(-25); + states[69] = new State(-24); rules[1] = new Rule(-2, new int[]{-1,2}); rules[2] = new Rule(-1, new int[]{-3}); @@ -76,12 +128,34 @@ public class Parser: ShiftReduceParser<int, LexLocation> 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[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[8] = new Rule(-5, new int[]{-8}); + rules[9] = new Rule(-5, new int[]{-9}); + rules[10] = new Rule(-5, new int[]{-10}); + rules[11] = new Rule(-5, new int[]{-11}); + rules[12] = new Rule(-5, new int[]{-12}); + rules[13] = new Rule(-5, new int[]{-13}); + rules[14] = new Rule(-14, new int[]{8}); + rules[15] = new Rule(-6, new int[]{-14,9,-15}); + rules[16] = new Rule(-3, new int[]{3,-4,4}); + rules[17] = new Rule(-7, new int[]{5,-15,-5}); + rules[18] = new Rule(-8, new int[]{11,-15,12,-5}); + rules[19] = new Rule(-9, new int[]{13,-4,14,-15}); + rules[20] = new Rule(-10, new int[]{15,-6,16,-15,12,-5}); + rules[21] = new Rule(-11, new int[]{17,18,-15,19}); + rules[22] = new Rule(-12, new int[]{20,-15,22,-5}); + rules[23] = new Rule(-12, new int[]{20,-15,22,-5,21,-5}); + rules[24] = new Rule(-16, new int[]{8}); + rules[25] = new Rule(-16, new int[]{-16,24,8}); + rules[26] = new Rule(-13, new int[]{23,-16}); + rules[27] = new Rule(-15, new int[]{-17}); + rules[28] = new Rule(-15, new int[]{-15,25,-17}); + rules[29] = new Rule(-15, new int[]{-15,26,-17}); + rules[30] = new Rule(-17, new int[]{-18}); + rules[31] = new Rule(-17, new int[]{-17,27,-18}); + rules[32] = new Rule(-17, new int[]{-17,28,-18}); + rules[33] = new Rule(-18, new int[]{-14}); + rules[34] = new Rule(-18, new int[]{6}); + rules[35] = new Rule(-18, new int[]{18,-15,19}); } protected override void Initialize() { diff --git a/Module5/SimpleYacc.lst b/Module5/SimpleYacc.lst index 6a40f2d376fe60dec695a3f3c5f547d9b46f6bf6..ab0ac4b8cdd9884d53b9ad31dfcd72630b0c17fc 100644 --- a/Module5/SimpleYacc.lst +++ b/Module5/SimpleYacc.lst @@ -3,9 +3,9 @@ // GPPG error listing for yacc source file <SimpleYacc.y> // ========================================================================== // Version: 1.3.6 -// Machine: SSM -// DateTime: 17.08.2014 10:25:15 -// UserName: Станислав +// Machine: MIL8A-202A-05 +// DateTime: 24.10.2022 12:52:13 +// UserName: user // ========================================================================== @@ -16,19 +16,20 @@ %output = SimpleYacc.cs -%using System.IO; - %namespace SimpleParser -%token BEGIN END CYCLE INUM RNUM ID ASSIGN SEMICOLON +%token BEGIN END CYCLE INUM RNUM ID ASSIGN SEMICOLON WHILE DO REPEAT UNTIL FOR TO WRITE LEFT_PR RIGHT_PR IF ELSE THEN VAR COMMA INUM PLUS MINUS MULT DIVISION LEFT_PR RIGHT_PR %% -// Error: NonTerminal symbol "st" has no productions -// Warning: Terminating st fixes the following size-2 NonTerminal set - // {cycle, st} +// Error: NonTerminal symbol "smt" has no productions +// Error: NonTerminal symbol "th" has no productions +// Warning: Terminating th fixes the following size-1 NonTerminal set + // {th} // Error: There are 2 non-terminating NonTerminal Symbols - // {cycle, st} -// ------------------------------------------------------------------ + // {smt, th} +// Warning: Terminating smt fixes the following size-1 NonTerminal set + // {smt} +// ------------------------------------------------------------------- progr : block ; @@ -40,6 +41,12 @@ stlist : statement statement: assign | block | cycle + | while + | repeat + | for + | write + | if + | var ; ident : ID @@ -48,17 +55,50 @@ ident : ID assign : ident ASSIGN expr ; -expr : ident - | INUM +block : BEGIN stlist END ; -block : BEGIN stlist END +cycle : CYCLE expr statement + ; + +while : WHILE expr DO statement + ; + +repeat : REPEAT stlist UNTIL expr ; -cycle : CYCLE expr st +for : FOR assign TO expr DO statement + ; + +write : WRITE LEFT_PR expr RIGHT_PR + ; + +if : IF expr THEN statement %prec IFX + | IF expr THEN statement ELSE statement + ; + +idlist : ID + | idlist COMMA ID + ; + +var : VAR idlist + ; + +expr : T + | expr PLUS T + | expr MINUS T + ; + +T : F + | smt MULT th + | smt DIVISION th + ; + +F : ident + | INUM + | LEFT_PR expr RIGHT_PR ; %% - // ========================================================================== diff --git a/Module5/SimpleYacc.y b/Module5/SimpleYacc.y index 95947328e21b95b7a66c1d8f5fbd6990cff9fae3..399bccf53801fb83344c3418682d4a3cfffee8dd 100644 --- a/Module5/SimpleYacc.y +++ b/Module5/SimpleYacc.y @@ -7,7 +7,7 @@ %namespace SimpleParser -%token BEGIN END CYCLE INUM RNUM ID ASSIGN SEMICOLON +%token BEGIN END CYCLE INUM RNUM ID ASSIGN SEMICOLON WHILE DO REPEAT UNTIL FOR TO WRITE LEFT_PR RIGHT_PR IF ELSE THEN VAR COMMA INUM PLUS MINUS MULT DIVISION LEFT_PR RIGHT_PR %% @@ -21,6 +21,12 @@ stlist : statement statement: assign | block | cycle + | while + | repeat + | for + | write + | if + | var ; ident : ID @@ -29,14 +35,48 @@ ident : ID assign : ident ASSIGN expr ; -expr : ident - | INUM - ; - block : BEGIN stlist END ; cycle : CYCLE expr statement ; + +while : WHILE expr DO statement + ; + +repeat : REPEAT stlist UNTIL expr + ; + +for : FOR assign TO expr DO statement + ; + +write : WRITE LEFT_PR expr RIGHT_PR + ; + +if : IF expr THEN statement %prec IFX + | IF expr THEN statement ELSE statement + ; + +idlist : ID + | idlist COMMA ID + ; + +var : VAR idlist + ; + +expr : T + | expr PLUS T + | expr MINUS T + ; + +T : F + | T MULT F + | T DIVISION F + ; + +F : ident + | INUM + | LEFT_PR expr RIGHT_PR + ; %% diff --git a/Module5/packages.config b/Module5/packages.config new file mode 100644 index 0000000000000000000000000000000000000000..14b53d1ecb2e718f6d9c15e6fcb20c081a72bdd1 --- /dev/null +++ b/Module5/packages.config @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="utf-8"?> +<packages> + <package id="NUnit" version="3.13.3" targetFramework="net40" /> + <package id="NUnit.ConsoleRunner" version="3.15.2" targetFramework="net40" /> + <package id="NUnit3TestAdapter" version="4.2.1" targetFramework="net40" /> +</packages> \ No newline at end of file diff --git a/Module7/packages.config b/Module7/packages.config new file mode 100644 index 0000000000000000000000000000000000000000..14b53d1ecb2e718f6d9c15e6fcb20c081a72bdd1 --- /dev/null +++ b/Module7/packages.config @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="utf-8"?> +<packages> + <package id="NUnit" version="3.13.3" targetFramework="net40" /> + <package id="NUnit.ConsoleRunner" version="3.15.2" targetFramework="net40" /> + <package id="NUnit3TestAdapter" version="4.2.1" targetFramework="net40" /> +</packages> \ No newline at end of file diff --git a/Module8/packages.config b/Module8/packages.config new file mode 100644 index 0000000000000000000000000000000000000000..14b53d1ecb2e718f6d9c15e6fcb20c081a72bdd1 --- /dev/null +++ b/Module8/packages.config @@ -0,0 +1,6 @@ +<?xml version="1.0" encoding="utf-8"?> +<packages> + <package id="NUnit" version="3.13.3" targetFramework="net40" /> + <package id="NUnit.ConsoleRunner" version="3.15.2" targetFramework="net40" /> + <package id="NUnit3TestAdapter" version="4.2.1" targetFramework="net40" /> +</packages> \ No newline at end of file diff --git a/TestDescentParser/Tests.cs b/TestDescentParser/Tests.cs index e523ebdcfd052b9918adee764cc2c2a39fc22587..d57f353e17a7506276b87e6d47836cc49c141875 100644 --- a/TestDescentParser/Tests.cs +++ b/TestDescentParser/Tests.cs @@ -124,7 +124,27 @@ namespace TestDescentParser else v:=(8+2) end")); - - } + Assert.Throws<ParserException>(() => 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