diff --git a/Module5/Gplex.exe b/Module5/Gplex.exe new file mode 100644 index 0000000000000000000000000000000000000000..51a522e221f5ca52cc934933e08745f1653a98b5 Binary files /dev/null and b/Module5/Gplex.exe differ diff --git a/Module5/Gppg.exe b/Module5/Gppg.exe new file mode 100644 index 0000000000000000000000000000000000000000..edca401986741541f14b7d4bd89adc57802327a2 Binary files /dev/null and b/Module5/Gppg.exe differ diff --git a/Module5/SimpleLex.cs b/Module5/SimpleLex.cs index af473da5e86ca6d0a581b5a1a65f18662ef3ba88..1c6e98eff9653a06aa7dd7a5b54eede837c26071 100644 --- a/Module5/SimpleLex.cs +++ b/Module5/SimpleLex.cs @@ -1,14 +1,10 @@ // // This CSharp output file generated by Gardens Point 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:11:00 -// UserName: someone -// GPLEX input file <SimpleLex.lex - 24.09.2018 23:47:02> +// Version: 1.1.3.301 +// Machine: DESKTOP-H0VRSS3 +// DateTime: 16.10.2022 1:09:37 +// UserName: nikit +// GPLEX input file <SimpleLex.lex> // GPLEX frame file <embedded resource> // // Option settings: unicode, parser, minimize @@ -17,8 +13,8 @@ // // -// Revised backup code -// Version 1.2.1 of 24-June-2013 +// Experimental embedded frame +// Version 1.1.3 of 18-April-2010 // // #define BACKUP @@ -127,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; @@ -165,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) @@ -190,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() { @@ -215,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; @@ -295,7 +298,8 @@ int NextState() { public Scanner(Stream file, string codepage) { SetSource(file, CodePageHandling.GetCodePage(codepage)); - } + } + #endif // !NOFILES public Scanner() { } @@ -323,7 +327,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++; @@ -376,7 +380,9 @@ int NextState() { GetCode(); } +#if !NOFILES // ================ LineBuffer Initialization =================== + /// <summary> /// Create and initialize a LineBuff buffer object for this scanner /// </summary> @@ -390,7 +396,6 @@ int NextState() { GetCode(); } -#if !NOFILES // =============== StreamBuffer Initialization ================== /// <summary> @@ -492,12 +497,6 @@ 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) { @@ -517,10 +516,6 @@ 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); } @@ -533,23 +528,18 @@ 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 -#if BYTEMODE - return tokEPos - tokPos; -#else - { + else { int ch; int count = 0; int save = buffer.Pos; @@ -558,7 +548,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 @@ -583,56 +573,65 @@ int NextState() { // ============== The main tokenizer code ================= - int Scan() { + int Scan() + { try { - for (; ; ) { - int next; // next state to enter + for (; ; ) + { + int next; // next state to enter +#if BACKUP + Result rslt = Result.noMatch; +#endif // BACKUP #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(); + GetCode(); + + while ((next = NextState()) > eofNum) // Exit for goStart AND for eofNum #if BACKUP - 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; + 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; } - 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(); - } + else #endif // BACKUP - if (state <= maxAccept) { + { + state = next; + GetCode(); + } + if (state <= maxAccept) + { MarkEnd(); #region ActionSwitch -#pragma warning disable 162, 1522 +#pragma warning disable 162 switch (state) { case eofNum: @@ -655,15 +654,36 @@ int res = ScannerHelper.GetIDToken(yytext); return (int)Tokens.SEMICOLON; break; case 6: -return (int)Tokens.ASSIGN; +return (int)Tokens.LEFT_BRACKET; break; case 7: +return (int)Tokens.RIGHT_BRACKET; + 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.MULTIPLY; + break; + case 12: +return (int)Tokens.DIVIDE; + break; + case 13: +return (int)Tokens.ASSIGN; + break; + case 14: return (int)Tokens.RNUM; break; default: break; } -#pragma warning restore 162, 1522 +#pragma warning restore 162 #endregion } } @@ -676,7 +696,29 @@ yylloc = new LexLocation(tokLin, tokCol, tokELin, tokECol); } #if BACKUP - void SaveStateAndPos(ref Context ctx) { + 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) + { ctx.bPos = buffer.Pos; ctx.rPos = readPos; ctx.cCol = cCol; @@ -685,7 +727,8 @@ 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; @@ -693,7 +736,8 @@ yylloc = new LexLocation(tokLin, tokCol, tokELin, tokECol); state = ctx.state; code = ctx.cChr; } -#endif // BACKUP + +#endif // BACKUP // ============= End of the tokenizer code ================ @@ -725,16 +769,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); } @@ -748,10 +792,21 @@ 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) { - if (keywords.ContainsKey(s.ToLower())) // ÿçûê Гå÷óâñòâèòåëåà ê ðåãèñòðó + if (keywords.ContainsKey(s.ToLower())) // язык нечувствителен к регистру return keywords[s]; else return (int)Tokens.ID; @@ -807,7 +862,6 @@ class ScannerHelper return new LineBuffer(source); } -#if (!NOFILES) public static ScanBuff GetBuffer(Stream source) { return new BuildBuffer(source); @@ -818,8 +872,7 @@ class ScannerHelper { return new BuildBuffer(source, fallbackCodePage); } -#endif // !BYTEMODE -#endif // !NOFILES +#endif } #region Buffer classes @@ -942,7 +995,7 @@ class ScannerHelper { ix = lstart = 0; } - while (ix < numLines) + for (; ; ) { int len = line[ix].Length + 1; if (pos < lstart + len) break; @@ -1000,8 +1053,7 @@ class ScannerHelper { cPos = value; findIndex(cPos, out cLine, out curLineStart); - // cLine should be the *next* line after curLine. - curLine = (cLine < numLines ? line[cLine++] : ""); + curLine = line[cLine]; curLineEnd = curLineStart + curLine.Length; } } @@ -1009,7 +1061,7 @@ class ScannerHelper public override string ToString() { return "LineBuffer"; } } -#if (!NOFILES) + // ============================================================== // ===== class BuildBuff : for unicode text files ======== // ============================================================== @@ -1250,13 +1302,12 @@ class ScannerHelper } #endif // !BYTEMODE } -#endif // !NOFILES #endregion Buffer classes // ============================================================== // ============ class CodePageHandling ============= // ============================================================== -#if (!NOFILES) + public static class CodePageHandling { public static int GetCodePage(string option) @@ -1467,7 +1518,6 @@ class ScannerHelper #endif // !BYTEMODE #endregion -#endif // !NOFILES // End of code copied from embedded resource diff --git a/Module5/SimpleLex.lex b/Module5/SimpleLex.lex index 10547e9b9695423ba9e54065df5945d19fd597a9..6e8d0924dd9bc338026b64d56058808749c14a13 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_BRACKET; } +")" { return (int)Tokens.RIGHT_BRACKET; } +"," { return (int)Tokens.COMMA; } +"+" { return (int)Tokens.PLUS; } +"-" { return (int)Tokens.MINUS; } +"*" { return (int)Tokens.MULTIPLY; } +"/" { return (int)Tokens.DIVIDE; } [^ \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 0be791437a683b1c6147f0ca5e9e36fc68941431..1cabddd8e6899d5cb0384978451309e2241021b6 100644 --- a/Module5/SimpleYacc.cs +++ b/Module5/SimpleYacc.cs @@ -1,102 +1,165 @@ // This code was generated by the Gardens Point Parser Generator -// Copyright (c) Wayne Kelly, John Gough, QUT 2005-2014 +// Copyright (c) Wayne Kelly, QUT 2005-2010 // (see accompanying GPPGcopyright.rtf) -// GPPG version 1.5.2 -// Machine: MAINHOMEPC2 -// DateTime: 30.09.2018 18:11:19 -// UserName: someone -// Input file <SimpleYacc.y - 24.09.2018 23:47:02> +// GPPG version 1.3.6 +// Machine: DESKTOP-H0VRSS3 +// DateTime: 16.10.2022 1:09:37 +// UserName: nikit +// Input file <SimpleYacc.y> // 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=2,EOF=3,BEGIN=4,END=5,CYCLE=6, - INUM=7,RNUM=8,ID=9,ASSIGN=10,SEMICOLON=11}; +public enum Tokens { + error=1,EOF=2,BEGIN=3,END=4,CYCLE=5,WHILE=6, + DO=7,REPEAT=8,UNTIL=9,FOR=10,TO=11,WRITE=12, + IF=13,THEN=14,ELSE=15,VAR=16,INUM=17,RNUM=18, + ID=19,ASSIGN=20,PLUS=21,MINUS=22,MULTIPLY=23,DIVIDE=24, + SEMICOLON=25,LEFT_BRACKET=26,RIGHT_BRACKET=27,COMMA=28}; // 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 - 24.09.2018 23:47:02 -// ГќГІГЁ îáúÿâëåГГЁГї äîáà âëÿþòñÿ Гў êëà ññ GPPGParser, ïðåäñòà âëÿþùèé ñîáîé ïà ðñåð, ГЈГҐГåðèðóåìûé ñèñòåìîé gppg + // Verbatim content from SimpleYacc.y +// Эти объявления добавляются в класс GPPGParser, представляющий собой парсер, генерируемый системой gppg public Parser(AbstractScanner<int, LexLocation> scanner) : base(scanner) { } - // End verbatim content from SimpleYacc.y - 24.09.2018 23:47:02 + // End verbatim content from SimpleYacc.y #pragma warning disable 649 - private static Dictionary<int, string> aliases; + 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[38]; + private static State[] states = new State[72]; 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[]{4,4},new int[]{-1,1,-3,3}); - states[1] = new State(new int[]{3,2}); + 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[]{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[]{9,14,4,4,6,18},new int[]{-5,8,-6,9,-8,10,-3,16,-7,17}); + states[4] = new State(new int[]{19,18,3,4,5,33,6,37,8,42,10,48,12,55,13,60,16,67},new int[]{-4,5,-5,46,-6,9,-14,10,-3,31,-7,32,-8,36,-9,41,-10,47,-11,54,-12,59,-13,66}); + states[5] = new State(new int[]{4,6,25,7}); + states[6] = new State(-18); + states[7] = new State(new int[]{19,18,3,4,5,33,6,37,8,42,10,48,12,55,13,60,16,67},new int[]{-5,8,-6,9,-14,10,-3,31,-7,32,-8,36,-9,41,-10,47,-11,54,-12,59,-13,66}); states[8] = new State(-4); states[9] = new State(-5); - 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[]{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); - - for (int sNo = 0; sNo < states.Length; sNo++) states[sNo].number = sNo; + states[10] = new State(new int[]{20,11}); + states[11] = new State(new int[]{19,18,17,29,26,20},new int[]{-15,12,-14,28,-17,30,-18,27}); + states[12] = new State(new int[]{21,13,22,23,4,-15,25,-15,9,-15,15,-15,11,-15}); + states[13] = new State(new int[]{19,18,17,19,26,20},new int[]{-17,14,-18,27,-14,17}); + states[14] = new State(new int[]{23,15,24,25,21,-30,22,-30,4,-30,25,-30,9,-30,15,-30,11,-30,27,-30,19,-30,3,-30,5,-30,6,-30,8,-30,10,-30,12,-30,13,-30,16,-30,7,-30,14,-30}); + states[15] = new State(new int[]{19,18,17,19,26,20},new int[]{-18,16,-14,17}); + states[16] = new State(-33); + states[17] = new State(-35); + states[18] = new State(-14); + states[19] = new State(-36); + states[20] = new State(new int[]{19,18,17,29,26,20},new int[]{-15,21,-14,28,-17,30,-18,27}); + states[21] = new State(new int[]{27,22,21,13,22,23}); + states[22] = new State(-37); + states[23] = new State(new int[]{19,18,17,19,26,20},new int[]{-17,24,-18,27,-14,17}); + states[24] = new State(new int[]{23,15,24,25,21,-31,22,-31,4,-31,25,-31,9,-31,15,-31,11,-31,27,-31,19,-31,3,-31,5,-31,6,-31,8,-31,10,-31,12,-31,13,-31,16,-31,7,-31,14,-31}); + states[25] = new State(new int[]{19,18,17,19,26,20},new int[]{-18,26,-14,17}); + states[26] = new State(-34); + states[27] = new State(-32); + states[28] = new State(new int[]{21,-16,22,-16,4,-16,25,-16,9,-16,15,-16,11,-16,27,-16,19,-16,3,-16,5,-16,6,-16,8,-16,10,-16,12,-16,13,-16,16,-16,7,-16,14,-16,23,-35,24,-35}); + states[29] = new State(new int[]{21,-17,22,-17,4,-17,25,-17,9,-17,15,-17,11,-17,27,-17,19,-17,3,-17,5,-17,6,-17,8,-17,10,-17,12,-17,13,-17,16,-17,7,-17,14,-17,23,-36,24,-36}); + states[30] = new State(new int[]{23,15,24,25,21,-29,22,-29,4,-29,25,-29,9,-29,15,-29,11,-29,27,-29,19,-29,3,-29,5,-29,6,-29,8,-29,10,-29,12,-29,13,-29,16,-29,7,-29,14,-29}); + states[31] = new State(-6); + states[32] = new State(-7); + states[33] = new State(new int[]{19,18,17,29,26,20},new int[]{-15,34,-14,28,-17,30,-18,27}); + states[34] = new State(new int[]{21,13,22,23,19,18,3,4,5,33,6,37,8,42,10,48,12,55,13,60,16,67},new int[]{-5,35,-6,9,-14,10,-3,31,-7,32,-8,36,-9,41,-10,47,-11,54,-12,59,-13,66}); + states[35] = new State(-19); + states[36] = new State(-8); + states[37] = new State(new int[]{19,18,17,29,26,20},new int[]{-15,38,-14,28,-17,30,-18,27}); + states[38] = new State(new int[]{7,39,21,13,22,23}); + states[39] = new State(new int[]{19,18,3,4,5,33,6,37,8,42,10,48,12,55,13,60,16,67},new int[]{-5,40,-6,9,-14,10,-3,31,-7,32,-8,36,-9,41,-10,47,-11,54,-12,59,-13,66}); + states[40] = new State(-20); + states[41] = new State(-9); + states[42] = new State(new int[]{19,18,3,4,5,33,6,37,8,42,10,48,12,55,13,60,16,67},new int[]{-4,43,-5,46,-6,9,-14,10,-3,31,-7,32,-8,36,-9,41,-10,47,-11,54,-12,59,-13,66}); + states[43] = new State(new int[]{9,44,25,7}); + states[44] = new State(new int[]{19,18,17,29,26,20},new int[]{-15,45,-14,28,-17,30,-18,27}); + states[45] = new State(new int[]{21,13,22,23,4,-21,25,-21,9,-21,15,-21}); + states[46] = new State(-3); + states[47] = new State(-10); + states[48] = new State(new int[]{19,18},new int[]{-6,49,-14,10}); + states[49] = new State(new int[]{11,50}); + states[50] = new State(new int[]{19,18,17,29,26,20},new int[]{-15,51,-14,28,-17,30,-18,27}); + states[51] = new State(new int[]{7,52,21,13,22,23}); + states[52] = new State(new int[]{19,18,3,4,5,33,6,37,8,42,10,48,12,55,13,60,16,67},new int[]{-5,53,-6,9,-14,10,-3,31,-7,32,-8,36,-9,41,-10,47,-11,54,-12,59,-13,66}); + states[53] = new State(-22); + states[54] = new State(-11); + states[55] = new State(new int[]{26,56}); + states[56] = new State(new int[]{19,18,17,29,26,20},new int[]{-15,57,-14,28,-17,30,-18,27}); + states[57] = new State(new int[]{27,58,21,13,22,23}); + states[58] = new State(-23); + states[59] = new State(-12); + states[60] = new State(new int[]{19,18,17,29,26,20},new int[]{-15,61,-14,28,-17,30,-18,27}); + states[61] = new State(new int[]{14,62,21,13,22,23}); + states[62] = new State(new int[]{19,18,3,4,5,33,6,37,8,42,10,48,12,55,13,60,16,67},new int[]{-5,63,-6,9,-14,10,-3,31,-7,32,-8,36,-9,41,-10,47,-11,54,-12,59,-13,66}); + states[63] = new State(new int[]{15,64,4,-24,25,-24,9,-24}); + states[64] = new State(new int[]{19,18,3,4,5,33,6,37,8,42,10,48,12,55,13,60,16,67},new int[]{-5,65,-6,9,-14,10,-3,31,-7,32,-8,36,-9,41,-10,47,-11,54,-12,59,-13,66}); + states[65] = new State(-25); + states[66] = new State(-13); + states[67] = new State(new int[]{19,18},new int[]{-16,68,-14,71}); + states[68] = new State(new int[]{28,69,4,-28,25,-28,9,-28,15,-28}); + states[69] = new State(new int[]{19,18},new int[]{-14,70}); + states[70] = new State(-27); + states[71] = new State(-26); - rules[1] = new Rule(-2, new int[]{-1,3}); + rules[1] = new Rule(-2, new int[]{-1,2}); rules[2] = new Rule(-1, new int[]{-3}); rules[3] = new Rule(-4, new int[]{-5}); - rules[4] = new Rule(-4, new int[]{-4,11,-5}); + rules[4] = new Rule(-4, new int[]{-4,25,-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[]{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[]{7}); - rules[12] = new Rule(-3, new int[]{4,-4,5}); - rules[13] = new Rule(-7, new int[]{6,-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[]{19}); + rules[15] = new Rule(-6, new int[]{-14,20,-15}); + rules[16] = new Rule(-15, new int[]{-14}); + rules[17] = new Rule(-15, new int[]{17}); + rules[18] = new Rule(-3, new int[]{3,-4,4}); + rules[19] = new Rule(-7, new int[]{5,-15,-5}); + rules[20] = new Rule(-8, new int[]{6,-15,7,-5}); + rules[21] = new Rule(-9, new int[]{8,-4,9,-15}); + rules[22] = new Rule(-10, new int[]{10,-6,11,-15,7,-5}); + rules[23] = new Rule(-11, new int[]{12,26,-15,27}); + rules[24] = new Rule(-12, new int[]{13,-15,14,-5}); + rules[25] = new Rule(-12, new int[]{13,-15,14,-5,15,-5}); + rules[26] = new Rule(-16, new int[]{-14}); + rules[27] = new Rule(-16, new int[]{-16,28,-14}); + rules[28] = new Rule(-13, new int[]{16,-16}); + rules[29] = new Rule(-15, new int[]{-17}); + rules[30] = new Rule(-15, new int[]{-15,21,-17}); + rules[31] = new Rule(-15, new int[]{-15,22,-17}); + rules[32] = new Rule(-17, new int[]{-18}); + rules[33] = new Rule(-17, new int[]{-17,23,-18}); + rules[34] = new Rule(-17, new int[]{-17,24,-18}); + rules[35] = new Rule(-18, new int[]{-14}); + rules[36] = new Rule(-18, new int[]{17}); + rules[37] = new Rule(-18, new int[]{26,-15,27}); } protected override void Initialize() { @@ -108,17 +171,15 @@ 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 (aliases != null && aliases.ContainsKey(terminal)) - return aliases[terminal]; + if (aliasses != null && aliasses.ContainsKey(terminal)) + return aliasses[terminal]; else if (((Tokens)terminal).ToString() != terminal.ToString(CultureInfo.InvariantCulture)) return ((Tokens)terminal).ToString(); else diff --git a/Module5/SimpleYacc.lst b/Module5/SimpleYacc.lst index 6a40f2d376fe60dec695a3f3c5f547d9b46f6bf6..e0de0011a3a1933a45dce679ae8f2b5a660352f3 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: DESKTOP-H0VRSS3 +// DateTime: 16.10.2022 1:08:43 +// UserName: nikit // ========================================================================== @@ -16,19 +16,26 @@ %output = SimpleYacc.cs -%using System.IO; - %namespace SimpleParser -%token BEGIN END CYCLE INUM RNUM ID ASSIGN SEMICOLON +%token BEGIN END CYCLE WHILE DO REPEAT UNTIL FOR TO WRITE IF THEN ELSE VAR INUM RNUM ID ASSIGN SEMICOLON LEFT_BRACKET RIGHT_BRACKET COMMA %% -// 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} -// ------------------------------------------------------------------ +// Error: NonTerminal symbol "MINUS" has no productions +// Error: NonTerminal symbol "PLUS" has no productions +// Error: NonTerminal symbol "DIVIDE" has no productions +// Error: NonTerminal symbol "MULTIPLY" has no productions +// Warning: Terminating DIVIDE fixes the following size-1 NonTerminal set + // {DIVIDE} +// Warning: Terminating PLUS fixes the following size-1 NonTerminal set + // {PLUS} +// Error: There are 4 non-terminating NonTerminal Symbols + // {PLUS, MINUS, MULTIPLY, DIVIDE} +// Warning: Terminating MULTIPLY fixes the following size-1 NonTerminal set + // {MULTIPLY} +// Warning: Terminating MINUS fixes the following size-1 NonTerminal set + // {MINUS} +// ------------------------------------------------------------------------ progr : block ; @@ -39,7 +46,13 @@ stlist : statement statement: assign | block - | cycle + | cycle + | while + | repeat + | for + | write + | if + | var ; ident : ID @@ -55,10 +68,48 @@ expr : ident block : BEGIN stlist END ; -cycle : CYCLE expr st +cycle : CYCLE expr statement + ; + +while : WHILE expr DO statement ; +repeat : REPEAT stlist UNTIL expr + ; + +for : FOR assign TO expr DO statement + ; + +write : WRITE LEFT_BRACKET expr RIGHT_BRACKET + ; + +if : IF expr THEN statement + | IF expr THEN statement ELSE statement + ; + +idlist : ident + | idlist COMMA ident + ; + +var : VAR idlist + ; + +expr : T + | expr PLUS T + | expr MINUS T + ; + +T : F + | T MULTIPLY F + | T DIVIDE F + ; + +F : ident + | INUM + | LEFT_BRACKET expr RIGHT_BRACKET + ; + + %% - // ========================================================================== diff --git a/Module5/SimpleYacc.y b/Module5/SimpleYacc.y index 95947328e21b95b7a66c1d8f5fbd6990cff9fae3..c612ccc520930f81119815391a21bf165017a727 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 WHILE DO REPEAT UNTIL FOR TO WRITE IF THEN ELSE VAR INUM RNUM ID ASSIGN PLUS MINUS MULTIPLY DIVIDE SEMICOLON LEFT_BRACKET RIGHT_BRACKET COMMA %% @@ -20,7 +20,13 @@ stlist : statement statement: assign | block - | cycle + | cycle + | while + | repeat + | for + | write + | if + | var ; ident : ID @@ -38,5 +44,44 @@ 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_BRACKET expr RIGHT_BRACKET + ; + +if : IF expr THEN statement + | IF expr THEN statement ELSE statement + ; + +idlist : ident + | idlist COMMA ident + ; + +var : VAR idlist + ; + +expr : T + | expr PLUS T + | expr MINUS T + ; + +T : F + | T MULTIPLY F + | T DIVIDE F + ; + +F : ident + | INUM + | LEFT_BRACKET expr RIGHT_BRACKET + ; + + %% diff --git a/Module5/a.txt b/Module5/a.txt index 52ea170dd0fbc36b71ada44af123728b3b9eadf3..845f491a44f67f9398db96e8b58f914571d66c3d 100644 --- a/Module5/a.txt +++ b/Module5/a.txt @@ -3,6 +3,7 @@ begin a := 3; a := b; cycle 3 + while 2 do a:=2; begin a := c; c := 1