diff --git a/Module5/SimpleLex.cs b/Module5/SimpleLex.cs index af473da5e86ca6d0a581b5a1a65f18662ef3ba88..542cce1ef6c768ede07b07b56a482c9de1ea178c 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-DB9EH0OL +// DateTime: 02.10.2022 14:29:06 +// UserName: Anton +// 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_BRACET; break; case 7: +return (int)Tokens.RIGHT_BRACET; + 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.DIV; + 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); } @@ -747,11 +791,22 @@ class ScannerHelper keywords = new Dictionary<string,int>(); keywords.Add("begin",(int)Tokens.BEGIN); keywords.Add("end",(int)Tokens.END); - keywords.Add("cycle",(int)Tokens.CYCLE); + 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("while",(int)Tokens.WHILE); + 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..7f96de1d69dfa674eb519c8cfcd479ebe02310ab 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_BRACET; } +")" {return (int)Tokens.RIGHT_BRACET; } +"," {return (int)Tokens.COMMA; } +"+" {return (int)Tokens.PLUS; } +"-" {return (int)Tokens.MINUS; } +"*" {return (int)Tokens.MULT; } +"/" {return (int)Tokens.DIV; } [^ \r\n] { LexError(); @@ -62,7 +69,18 @@ class ScannerHelper keywords = new Dictionary<string,int>(); keywords.Add("begin",(int)Tokens.BEGIN); keywords.Add("end",(int)Tokens.END); - keywords.Add("cycle",(int)Tokens.CYCLE); + 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("while",(int)Tokens.WHILE); + 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..990e990340caaedfcfae18647ab5ae3a03f5af6e 100644 --- a/Module5/SimpleYacc.cs +++ b/Module5/SimpleYacc.cs @@ -1,102 +1,158 @@ // 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-DB9EH0OL +// DateTime: 02.10.2022 14:29:06 +// UserName: Anton +// 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,WHILE=11,DO=12, + REPEAT=13,UNTIL=14,FOR=15,TO=16,WRITE=17,LEFT_BRACET=18, + RIGHT_BRACET=19,IF=20,THEN=21,ELSE=22,COMMA=23,VAR=24, + PLUS=25,MINUS=26,DIV=27,MULT=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[35]; + private static State[] states = new State[68]; private static string[] nonTerms = new string[] { "progr", "$accept", "block", "stlist", "statement", "assign", "cycle", - "ident", "expr", }; + "while", "repeat", "for", "write", "if", "var", "ident", "idlst", "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[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[]{8,22,3,4,5,29,11,33,13,39,15,44,17,51,20,56,24,63,4,-18,10,-18},new int[]{-4,5,-5,37,-6,9,-14,10,-3,27,-7,28,-8,32,-9,38,-10,43,-11,50,-12,55,-13,62}); + states[5] = new State(new int[]{4,6,10,7}); + states[6] = new State(-27); + states[7] = new State(new int[]{8,22,3,4,5,29,11,33,13,39,15,44,17,51,20,56,24,63,4,-18,10,-18,14,-18,22,-18},new int[]{-5,8,-6,9,-14,10,-3,27,-7,28,-8,32,-9,38,-10,43,-11,50,-12,55,-13,62}); 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[]{9,11}); + states[11] = new State(new int[]{8,22,6,23,18,24},new int[]{-16,12,-14,21}); + states[12] = new State(new int[]{25,13,26,15,28,17,27,19,4,-19,10,-19,14,-19,22,-19,16,-19}); + states[13] = new State(new int[]{8,22,6,23,18,24},new int[]{-16,14,-14,21}); + states[14] = new State(new int[]{25,13,26,15,28,17,27,19,4,-22,10,-22,14,-22,22,-22,16,-22,19,-22,8,-22,3,-22,5,-22,11,-22,13,-22,15,-22,17,-22,20,-22,24,-22,12,-22,21,-22}); + states[15] = new State(new int[]{8,22,6,23,18,24},new int[]{-16,16,-14,21}); + states[16] = new State(new int[]{25,13,26,15,28,17,27,19,4,-23,10,-23,14,-23,22,-23,16,-23,19,-23,8,-23,3,-23,5,-23,11,-23,13,-23,15,-23,17,-23,20,-23,24,-23,12,-23,21,-23}); + states[17] = new State(new int[]{8,22,6,23,18,24},new int[]{-16,18,-14,21}); + states[18] = new State(new int[]{25,13,26,15,28,17,27,19,4,-25,10,-25,14,-25,22,-25,16,-25,19,-25,8,-25,3,-25,5,-25,11,-25,13,-25,15,-25,17,-25,20,-25,24,-25,12,-25,21,-25}); + states[19] = new State(new int[]{8,22,6,23,18,24},new int[]{-16,20,-14,21}); + states[20] = new State(new int[]{25,13,26,15,28,17,27,19,4,-26,10,-26,14,-26,22,-26,16,-26,19,-26,8,-26,3,-26,5,-26,11,-26,13,-26,15,-26,17,-26,20,-26,24,-26,12,-26,21,-26}); + states[21] = new State(-20); + states[22] = new State(-14); + states[23] = new State(-21); + states[24] = new State(new int[]{8,22,6,23,18,24},new int[]{-16,25,-14,21}); + states[25] = new State(new int[]{19,26,25,13,26,15,28,17,27,19}); + states[26] = new State(-24); + states[27] = new State(-6); + states[28] = new State(-7); + states[29] = new State(new int[]{8,22,6,23,18,24},new int[]{-16,30,-14,21}); + states[30] = new State(new int[]{25,13,26,15,28,17,27,19,8,22,3,4,5,29,11,33,13,39,15,44,17,51,20,56,24,63,4,-18,10,-18,14,-18,22,-18},new int[]{-5,31,-6,9,-14,10,-3,27,-7,28,-8,32,-9,38,-10,43,-11,50,-12,55,-13,62}); + states[31] = new State(-28); + states[32] = new State(-8); + states[33] = new State(new int[]{8,22,6,23,18,24},new int[]{-16,34,-14,21}); + states[34] = new State(new int[]{12,35,25,13,26,15,28,17,27,19}); + states[35] = new State(new int[]{8,22,3,4,5,29,11,33,13,39,15,44,17,51,20,56,24,63,4,-18,10,-18,14,-18,22,-18},new int[]{-4,36,-5,37,-6,9,-14,10,-3,27,-7,28,-8,32,-9,38,-10,43,-11,50,-12,55,-13,62}); + states[36] = new State(new int[]{10,7,4,-29,14,-29,22,-29}); + states[37] = new State(-3); + states[38] = new State(-9); + states[39] = new State(new int[]{8,22,3,4,5,29,11,33,13,39,15,44,17,51,20,56,24,63,14,-18,10,-18},new int[]{-4,40,-5,37,-6,9,-14,10,-3,27,-7,28,-8,32,-9,38,-10,43,-11,50,-12,55,-13,62}); + states[40] = new State(new int[]{14,41,10,7}); + states[41] = new State(new int[]{8,22,6,23,18,24},new int[]{-16,42,-14,21}); + states[42] = new State(new int[]{25,13,26,15,28,17,27,19,4,-30,10,-30,14,-30,22,-30}); + states[43] = new State(-10); + states[44] = new State(new int[]{8,22},new int[]{-6,45,-14,10}); + states[45] = new State(new int[]{16,46}); + states[46] = new State(new int[]{8,22,6,23,18,24},new int[]{-16,47,-14,21}); + states[47] = new State(new int[]{12,48,25,13,26,15,28,17,27,19}); + states[48] = new State(new int[]{8,22,3,4,5,29,11,33,13,39,15,44,17,51,20,56,24,63,4,-18,10,-18,14,-18,22,-18},new int[]{-4,49,-5,37,-6,9,-14,10,-3,27,-7,28,-8,32,-9,38,-10,43,-11,50,-12,55,-13,62}); + states[49] = new State(new int[]{10,7,4,-31,14,-31,22,-31}); + states[50] = new State(-11); + states[51] = new State(new int[]{18,52}); + states[52] = new State(new int[]{8,22,6,23,18,24},new int[]{-16,53,-14,21}); + states[53] = new State(new int[]{19,54,25,13,26,15,28,17,27,19}); + states[54] = new State(-32); + states[55] = new State(-12); + states[56] = new State(new int[]{8,22,6,23,18,24},new int[]{-16,57,-14,21}); + states[57] = new State(new int[]{21,58,25,13,26,15,28,17,27,19}); + states[58] = new State(new int[]{8,22,3,4,5,29,11,33,13,39,15,44,17,51,20,56,24,63,4,-18,10,-18,14,-18,22,-18},new int[]{-5,59,-6,9,-14,10,-3,27,-7,28,-8,32,-9,38,-10,43,-11,50,-12,55,-13,62}); + states[59] = new State(new int[]{22,60,4,-34,10,-34,14,-34}); + states[60] = new State(new int[]{8,22,3,4,5,29,11,33,13,39,15,44,17,51,20,56,24,63,4,-18,10,-18,14,-18,22,-18},new int[]{-5,61,-6,9,-14,10,-3,27,-7,28,-8,32,-9,38,-10,43,-11,50,-12,55,-13,62}); + states[61] = new State(-33); + states[62] = new State(-13); + states[63] = new State(new int[]{8,22},new int[]{-15,64,-14,67}); + states[64] = new State(new int[]{23,65,4,-17,10,-17,14,-17,22,-17}); + states[65] = new State(new int[]{8,22},new int[]{-14,66}); + states[66] = new State(-16); + states[67] = new State(-15); - 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[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[]{8}); + rules[15] = new Rule(-15, new int[]{-14}); + rules[16] = new Rule(-15, new int[]{-15,23,-14}); + rules[17] = new Rule(-13, new int[]{24,-15}); + rules[18] = new Rule(-13, new int[]{}); + rules[19] = new Rule(-6, new int[]{-14,9,-16}); + rules[20] = new Rule(-16, new int[]{-14}); + rules[21] = new Rule(-16, new int[]{6}); + rules[22] = new Rule(-16, new int[]{-16,25,-16}); + rules[23] = new Rule(-16, new int[]{-16,26,-16}); + rules[24] = new Rule(-16, new int[]{18,-16,19}); + rules[25] = new Rule(-16, new int[]{-16,28,-16}); + rules[26] = new Rule(-16, new int[]{-16,27,-16}); + rules[27] = new Rule(-3, new int[]{3,-4,4}); + rules[28] = new Rule(-7, new int[]{5,-16,-5}); + rules[29] = new Rule(-8, new int[]{11,-16,12,-4}); + rules[30] = new Rule(-9, new int[]{13,-4,14,-16}); + rules[31] = new Rule(-10, new int[]{15,-6,16,-16,12,-4}); + rules[32] = new Rule(-11, new int[]{17,18,-16,19}); + rules[33] = new Rule(-12, new int[]{20,-16,21,-5,22,-5}); + rules[34] = new Rule(-12, new int[]{20,-16,21,-5}); } protected override void Initialize() { @@ -108,17 +164,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 deleted file mode 100644 index 6a40f2d376fe60dec695a3f3c5f547d9b46f6bf6..0000000000000000000000000000000000000000 --- a/Module5/SimpleYacc.lst +++ /dev/null @@ -1,64 +0,0 @@ - -// ========================================================================== -// GPPG error listing for yacc source file <SimpleYacc.y> -// ========================================================================== -// Version: 1.3.6 -// Machine: SSM -// DateTime: 17.08.2014 10:25:15 -// UserName: Станислав -// ========================================================================== - - -%{ -// ГќГІГЁ îáúÿâëåГГЁГї äîáà âëÿþòñÿ Гў êëà ññ GPPGParser, ïðåäñòà âëÿþùèé ñîáîé ïà ðñåð, ГЈГҐГåðèðóåìûé ñèñòåìîé gppg - public Parser(AbstractScanner<int, LexLocation> scanner) : base(scanner) { } -%} - -%output = SimpleYacc.cs - -%using System.IO; - -%namespace SimpleParser - -%token BEGIN END CYCLE INUM RNUM ID ASSIGN SEMICOLON - -%% -// 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 - ; - -stlist : statement - | stlist SEMICOLON statement - ; - -statement: assign - | block - | cycle - ; - -ident : ID - ; - -assign : ident ASSIGN expr - ; - -expr : ident - | INUM - ; - -block : BEGIN stlist END - ; - -cycle : CYCLE expr st - ; - -%% - -// ========================================================================== - diff --git a/Module5/SimpleYacc.y b/Module5/SimpleYacc.y index 95947328e21b95b7a66c1d8f5fbd6990cff9fae3..e7619a893cfb19f84565415b3f0d3cb8c763ed6c 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_BRACET RIGHT_BRACET IF THEN ELSE COMMA VAR PLUS MINUS DIV MULT %% @@ -21,16 +21,35 @@ stlist : statement statement: assign | block | cycle + | while + | repeat + | for + | write + | if + | var ; ident : ID + ; + +idlst : ident + | idlst COMMA ident ; - + +var : VAR idlst + | + ; + assign : ident ASSIGN expr ; expr : ident | INUM + | expr PLUS expr + | expr MINUS expr + | LEFT_BRACET expr RIGHT_BRACET + | expr MULT expr + | expr DIV expr ; block : BEGIN stlist END @@ -39,4 +58,21 @@ block : BEGIN stlist END cycle : CYCLE expr statement ; +while : WHILE expr DO stlist + ; + +repeat : REPEAT stlist UNTIL expr + ; + +for : FOR assign TO expr DO stlist + ; + +write : WRITE LEFT_BRACET expr RIGHT_BRACET + ; + +if : IF expr THEN statement ELSE statement + | IF expr THEN statement + ; + + %%