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..868c03ed8aac52c487673aa76226398614f3039f 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: LAPTOP-8RKO19AH +// DateTime: 18.10.2022 0:13:27 +// UserName: luspa +// 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 @@ -295,7 +291,8 @@ int NextState() { public Scanner(Stream file, string codepage) { SetSource(file, CodePageHandling.GetCodePage(codepage)); - } + } + #endif // !NOFILES public Scanner() { } @@ -323,7 +320,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 +373,9 @@ int NextState() { GetCode(); } +#if !NOFILES // ================ LineBuffer Initialization =================== + /// <summary> /// Create and initialize a LineBuff buffer object for this scanner /// </summary> @@ -390,7 +389,6 @@ int NextState() { GetCode(); } -#if !NOFILES // =============== StreamBuffer Initialization ================== /// <summary> @@ -492,12 +490,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 +509,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 +521,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 +541,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 +566,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: @@ -663,7 +655,7 @@ return (int)Tokens.RNUM; default: break; } -#pragma warning restore 162, 1522 +#pragma warning restore 162 #endregion } } @@ -676,7 +668,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 +699,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 +708,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 +741,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); } @@ -751,7 +767,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; @@ -807,7 +823,6 @@ class ScannerHelper return new LineBuffer(source); } -#if (!NOFILES) public static ScanBuff GetBuffer(Stream source) { return new BuildBuffer(source); @@ -818,8 +833,7 @@ class ScannerHelper { return new BuildBuffer(source, fallbackCodePage); } -#endif // !BYTEMODE -#endif // !NOFILES +#endif } #region Buffer classes @@ -942,7 +956,7 @@ class ScannerHelper { ix = lstart = 0; } - while (ix < numLines) + for (; ; ) { int len = line[ix].Length + 1; if (pos < lstart + len) break; @@ -1000,8 +1014,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 +1022,7 @@ class ScannerHelper public override string ToString() { return "LineBuffer"; } } -#if (!NOFILES) + // ============================================================== // ===== class BuildBuff : for unicode text files ======== // ============================================================== @@ -1250,13 +1263,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 +1479,6 @@ class ScannerHelper #endif // !BYTEMODE #endregion -#endif // !NOFILES // End of code copied from embedded resource diff --git a/Module5/SimpleYacc.cs b/Module5/SimpleYacc.cs index 0be791437a683b1c6147f0ca5e9e36fc68941431..a09c90871b0162a6aaa47aff63db4c35faf8fc65 100644 --- a/Module5/SimpleYacc.cs +++ b/Module5/SimpleYacc.cs @@ -1,56 +1,43 @@ // 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: LAPTOP-8RKO19AH +// DateTime: 18.10.2022 0:13:27 +// UserName: luspa +// 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,INUM=6, + RNUM=7,ID=8,ASSIGN=9,SEMICOLON=10}; // 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]; @@ -59,44 +46,42 @@ public class Parser: ShiftReduceParser<int, LexLocation> "ident", "expr", }; 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[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[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[7] = new State(new int[]{8,14,3,4,5,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[]{10,11}); - states[11] = new State(new int[]{9,14,7,15},new int[]{-9,12,-8,13}); + 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[]{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[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); - for (int sNo = 0; sNo < states.Length; sNo++) states[sNo].number = sNo; - - 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,10,-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[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[]{7}); - rules[12] = new Rule(-3, new int[]{4,-4,5}); - rules[13] = new Rule(-7, new int[]{6,-9,-5}); + 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}); } protected override void Initialize() { @@ -108,17 +93,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