{"id":26087,"date":"2016-04-02T22:54:46","date_gmt":"2016-04-02T19:54:46","guid":{"rendered":"http:\/\/studentpmr.ru\/?p=26087"},"modified":"2016-04-02T22:54:46","modified_gmt":"2016-04-02T19:54:46","slug":"%d1%82%d1%8f%d0%bf-%d0%bb%d0%b0%d0%b1%d0%be%d1%80%d0%b0%d1%82%d0%be%d1%80%d0%bd%d0%b0%d1%8f-%d1%80%d0%b0%d0%b1%d0%be%d1%82%d0%b0-2-%d0%b8%d1%81%d1%85%d0%be%d0%b4%d0%bd%d0%b8%d0%ba%d0%b8","status":"publish","type":"post","link":"https:\/\/studentpmr.ru\/?p=26087","title":{"rendered":"\u0422\u042f\u041f \u2014 \u043b\u0430\u0431\u043e\u0440\u0430\u0442\u043e\u0440\u043d\u0430\u044f \u0440\u0430\u0431\u043e\u0442\u0430 2. \u0418\u0441\u0445\u043e\u0434\u043d\u0438\u043a\u0438 &#171;\u0440\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u0442\u0435\u043b\u044f&#187;."},"content":{"rendered":"<p><strong>\u0424\u0430\u0439\u043b Token.cs<\/strong><\/p>\n<p>using System;<\/p>\n<p>using System.Collections.Generic;<br \/>\nusing System.Collections;<br \/>\nusing System.Text;<\/p>\n<p>namespace \u0422\u042f\u041f_\u043b\u04312_\u043c6<br \/>\n{<br \/>\nenum TokenType { ERROR =0, Num, Identificator, BracketLeft, BracketRight, Not, End, Assign, OperatorBoolean};<\/p>\n<p><!--more--><\/p>\n<p>enum LexType { Terminal = 0, Nonterminal};<\/p>\n<p>enum LexRelation { Empty=0, Equality, Before, After};<\/p>\n<p>delegate TokenType ChangeState(TokenType type);<\/p>\n<p>class Token<br \/>\n{<br \/>\n#region \u041f\u043e\u043b\u044f<br \/>\nprivate TokenType type;<br \/>\nprivate string name;<br \/>\n#endregion<\/p>\n<p>#region \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u044b<br \/>\npublic Token()<br \/>\n{<br \/>\ntype = TokenType.Identificator;<br \/>\nname = &#171;&#187;;<br \/>\n}<\/p>\n<p>public Token(TokenType type, string name)<br \/>\n{<br \/>\nthis.type = type;<br \/>\nthis.name = name;<br \/>\n}<br \/>\n#endregion<\/p>\n<p>#region \u0421\u0432\u043e\u0439\u0441\u0442\u0432\u0430<br \/>\npublic TokenType Type<br \/>\n{<br \/>\nget { return this.type; }<br \/>\nset { this.type = value; }<br \/>\n}<\/p>\n<p>public string Name<br \/>\n{<br \/>\nget { return this.name; }<br \/>\nset { this.name = value; }<br \/>\n}<br \/>\n#endregion<\/p>\n<p>#region \u041c\u0435\u0442\u043e\u0434\u044b<br \/>\nstatic bool IsLiter(char ch)<br \/>\n{<br \/>\nreturn ch &gt;= &#8216;a&#8217; &amp;&amp; ch&lt;=&#8217;z&#8217; || ch&gt;=&#8217;A&#8217; &amp;&amp; ch &lt;=&#8217;Z&#8217;|| ch== &#8216;_&#8217;;<br \/>\n}<\/p>\n<p>static bool IsDigit(char ch)<br \/>\n{<br \/>\nreturn ch==&#8217;0&#8242; || ch==&#8217;1&#8242;;<br \/>\n}<\/p>\n<p>public static Token[] GetTokens(string bufer)<br \/>\n{<br \/>\nList&lt;Token&gt; tokens = new List&lt;Token&gt;();<br \/>\nstring tempName = &#171;&#187;;<br \/>\nToken tempToken;<\/p>\n<p>for (int i = 0; i &lt; bufer.Length; i++)<br \/>\n{<br \/>\ntempName = &#171;&#187;;<\/p>\n<p>#region \u0411\u0443\u043a\u0432\u0435\u043d\u043d\u044b\u0435 \u0441\u043b\u043e\u0432\u0430<br \/>\nif(Token.IsLiter(bufer[i]))<br \/>\n{<br \/>\ndo<br \/>\n{<br \/>\ntempName += bufer[i];<br \/>\ni++;<br \/>\n}<br \/>\nwhile(i &lt; bufer.Length &amp;&amp; (Token.IsLiter(bufer[i]) || Char.IsDigit(bufer[i])));<\/p>\n<p>tempToken = new Token();<br \/>\ntempToken.Name = tempName;<br \/>\nswitch (tempName)<br \/>\n{<br \/>\ncase &#171;not&#187;:<br \/>\ntempToken.Type = TokenType.Not;<br \/>\nbreak;<br \/>\ncase &#171;or&#187;:<br \/>\ncase &#171;xor&#187;:<br \/>\ncase &#171;and&#187;:<br \/>\ntempToken.Type = TokenType.OperatorBoolean;<br \/>\nbreak;<br \/>\ndefault:<br \/>\ntempToken.Type = TokenType.Identificator;<br \/>\nbreak;<br \/>\n}<br \/>\ntokens.Add(tempToken);<br \/>\ntempName = &#171;&#187;;<br \/>\ni&#8212;;<br \/>\ncontinue;<br \/>\n}<br \/>\n#endregion<\/p>\n<p>#region \u0426\u0438\u0444\u0440\u043e\u0432\u044b\u0435 \u0441\u043b\u043e\u0432\u0430<br \/>\nif (Char.IsDigit(bufer[i]))<br \/>\n{<br \/>\ndo<br \/>\n{<br \/>\ntempName += bufer[i];<br \/>\ni++;<br \/>\n}<br \/>\nwhile (i &lt; bufer.Length &amp;&amp; Char.IsDigit(bufer[i]));<\/p>\n<p>if (tempName == &#171;0&#187; || tempName == &#171;1&#187;)<br \/>\n{<br \/>\ntempToken = new Token(TokenType.Num, tempName);<br \/>\ntokens.Add(tempToken);<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\ntempToken = new Token(TokenType.ERROR, tempName);<br \/>\ntokens.Add(tempToken);<br \/>\n}<br \/>\ntempName = &#171;&#187;;<br \/>\ni&#8212;;<br \/>\ncontinue;<br \/>\n}<br \/>\n#endregion<\/p>\n<p>tempName += bufer[i];<\/p>\n<p>switch(bufer[i])<br \/>\n{<br \/>\ncase &#8216;(&#8216;:<br \/>\ntempToken = new Token(TokenType.BracketLeft, tempName);<br \/>\ntokens.Add(tempToken);<br \/>\nbreak;<br \/>\ncase &#8216;)&#8217;:<br \/>\ntempToken = new Token(TokenType.BracketRight, tempName);<br \/>\ntokens.Add(tempToken);<br \/>\nbreak;<br \/>\ncase &#8216;;&#8217;:<br \/>\ntempToken = new Token(TokenType.End, tempName);<br \/>\ntokens.Add(tempToken);<br \/>\nbreak;<br \/>\ncase&#8217; &#8216;:<br \/>\ncase &#8216;\\n&#8217;:<br \/>\ncase &#8216;\\0&#8217;:<br \/>\nbreak;<br \/>\ncase &#8216;:&#8217;:<br \/>\nif (i+1 &lt; bufer.Length &amp;&amp; bufer[i+1] == &#8216;=&#8217;)<br \/>\n{<br \/>\ntempName += bufer[i+1];<br \/>\ni++;<br \/>\ntempToken = new Token(TokenType.Assign, tempName);<br \/>\ntokens.Add(tempToken);<br \/>\n}<br \/>\nelse<br \/>\n{<br \/>\ntempToken = new Token(TokenType.ERROR, tempName);<br \/>\ntokens.Add(tempToken);<br \/>\n}<br \/>\nbreak;<br \/>\ndefault:<br \/>\ntempToken = new Token(TokenType.ERROR, tempName);<br \/>\ntokens.Add(tempToken);<br \/>\nbreak;<br \/>\n}<br \/>\n}<br \/>\nreturn tokens.ToArray();<br \/>\n}<\/p>\n<p>public static bool ScanBool(Token[] tokens, int iStart, int iEnd)<br \/>\n{<br \/>\nif(iStart &gt;= tokens.Length || iStart &gt; iEnd)<br \/>\nreturn true;<br \/>\n\/\/<br \/>\nint brackets = 0;<br \/>\nTokenType currentState;<br \/>\nChangeState changeState = StateStartBool;<br \/>\n\/\/<br \/>\ncurrentState = changeState(tokens[iStart].type);<br \/>\nif (currentState == TokenType.BracketLeft) brackets++;<\/p>\n<p>for (int i = iStart+1; i &lt;= iEnd &amp;&amp; i &lt; tokens.Length; i++)<br \/>\n{<br \/>\nif (currentState == TokenType.ERROR)<br \/>\nbreak;<br \/>\nswitch (currentState)<br \/>\n{<br \/>\ncase TokenType.Identificator:<br \/>\nchangeState = StateIdentificator;<br \/>\nbreak;<br \/>\ncase TokenType.Num:<br \/>\nchangeState = StateNum;<br \/>\nbreak;<br \/>\ncase TokenType.Assign:<br \/>\nchangeState = StateAssign;<br \/>\nbreak;<br \/>\ncase TokenType.Not:<br \/>\nchangeState = StateNot;<br \/>\nbreak;<br \/>\ncase TokenType.BracketLeft:<br \/>\nchangeState = StateBracketLeft;<br \/>\nbreak;<br \/>\ncase TokenType.BracketRight:<br \/>\nchangeState = StateBracketRight;<br \/>\nbreak;<br \/>\ncase TokenType.OperatorBoolean:<br \/>\nchangeState = StateOperatorBool;<br \/>\nbreak;<br \/>\n}<br \/>\ncurrentState = changeState(tokens[i].type);<br \/>\nif (currentState == TokenType.BracketLeft) brackets++;<br \/>\nif (currentState == TokenType.BracketRight) brackets&#8212;;<br \/>\nif (brackets &lt; 0)<br \/>\n{<br \/>\ncurrentState = TokenType.ERROR;<br \/>\nbreak;<br \/>\n}<br \/>\n}<br \/>\n\/\/<br \/>\nif (brackets != 0)<br \/>\nreturn false;<br \/>\nif (currentState == TokenType.BracketRight)<br \/>\nreturn true;<br \/>\nif (currentState == TokenType.Num)<br \/>\nreturn true;<br \/>\nif (currentState == TokenType.Identificator)<br \/>\nreturn true;<br \/>\nreturn false;<br \/>\n}<\/p>\n<p>public static bool ScanGeneral(Token[] tokens, int iStart, int iEnd)<br \/>\n{<br \/>\nbool result = true;<br \/>\nint iPoint;<br \/>\nint i = iStart;<br \/>\n\/\/<br \/>\nwhile (i &lt; iEnd &amp;&amp; i &lt; tokens.Length &amp;&amp; result)<br \/>\n{<br \/>\niPoint = Token.IndexOf(TokenType.End, tokens, i, tokens.Length &#8212; 1);<br \/>\nif ((iPoint &#8212; i) == 0) { i++; continue; }<br \/>\nif (iPoint &lt; 3) return false;<\/p>\n<p>result &amp;= tokens[i].Type == TokenType.Identificator;<br \/>\ni++;<br \/>\nresult &amp;= tokens[i].Type == TokenType.Assign;<br \/>\ni++;<br \/>\nresult &amp;= ScanBool(tokens, i, iPoint &#8212; 1);<br \/>\ni = iPoint+1;<br \/>\n}<\/p>\n<p>return result;<br \/>\n}<\/p>\n<p>public static int IndexOf(TokenType tokenType, Token[] tokens, int iStart, int iEnd)<br \/>\n{<br \/>\nfor (int i = iStart; i &lt;= iEnd &amp;&amp; i &lt; tokens.Length; i++)<br \/>\nif (tokens[i].Type == tokenType)<br \/>\nreturn i;<br \/>\nreturn -1;<br \/>\n}<\/p>\n<p>#region \u0421\u043e\u0441\u0442\u043e\u044f\u043d\u0438\u044f<br \/>\npublic static TokenType StateStartBool(TokenType type)<br \/>\n{<br \/>\nTokenType result;<br \/>\nswitch (type)<br \/>\n{<br \/>\ncase TokenType.Not:<br \/>\nresult = TokenType.Not;<br \/>\nbreak;<br \/>\ncase TokenType.Identificator:<br \/>\nresult = TokenType.Identificator;<br \/>\nbreak;<br \/>\ncase TokenType.Num:<br \/>\nresult = TokenType.Num;<br \/>\nbreak;<br \/>\ncase TokenType.BracketLeft:<br \/>\nresult = TokenType.BracketLeft;<br \/>\nbreak;<br \/>\n\/\/case TokenType.BracketRight:<br \/>\n\/\/ result = TokenType.BracketRight;<br \/>\n\/\/ break;<br \/>\ndefault:<br \/>\nresult = TokenType.ERROR;<br \/>\nbreak;<br \/>\n}<br \/>\nreturn result;<br \/>\n}<\/p>\n<p>public static TokenType StateIdentificator(TokenType type)<br \/>\n{<br \/>\nTokenType result;<br \/>\nswitch (type)<br \/>\n{<br \/>\ncase TokenType.OperatorBoolean:<br \/>\nresult = TokenType.OperatorBoolean;<br \/>\nbreak;<br \/>\ncase TokenType.Assign:<br \/>\nresult = TokenType.Assign;<br \/>\nbreak;<br \/>\ncase TokenType.BracketRight:<br \/>\nresult = TokenType.BracketRight;<br \/>\nbreak;<br \/>\ndefault:<br \/>\nresult = TokenType.ERROR;<br \/>\nbreak;<br \/>\n}<br \/>\nreturn result;<br \/>\n}<\/p>\n<p>public static TokenType StateAssign(TokenType type)<br \/>\n{<br \/>\nTokenType result;<br \/>\nswitch (type)<br \/>\n{<br \/>\ncase TokenType.Not:<br \/>\nresult = TokenType.Not;<br \/>\nbreak;<br \/>\ncase TokenType.Identificator:<br \/>\nresult = TokenType.Identificator;<br \/>\nbreak;<br \/>\ncase TokenType.Num:<br \/>\nresult = TokenType.Num;<br \/>\nbreak;<br \/>\ncase TokenType.BracketLeft:<br \/>\nresult = TokenType.BracketLeft;<br \/>\nbreak;<br \/>\ndefault:<br \/>\nresult = TokenType.ERROR;<br \/>\nbreak;<br \/>\n}<br \/>\nreturn result;<br \/>\n}<\/p>\n<p>public static TokenType StateBracketLeft(TokenType type)<br \/>\n{<br \/>\nTokenType result;<br \/>\nresult = StateStartBool(type);<br \/>\nreturn result;<br \/>\n}<\/p>\n<p>public static TokenType StateBracketRight(TokenType type)<br \/>\n{<br \/>\nTokenType result;<br \/>\nswitch (type)<br \/>\n{<br \/>\ncase TokenType.OperatorBoolean:<br \/>\nresult = TokenType.OperatorBoolean;<br \/>\nbreak;<br \/>\ncase TokenType.BracketRight:<br \/>\nresult = TokenType.BracketRight;<br \/>\nbreak;<br \/>\ndefault:<br \/>\nresult = TokenType.ERROR;<br \/>\nbreak;<br \/>\n}<br \/>\nreturn result;<br \/>\n}<\/p>\n<p>public static TokenType StateNot(TokenType type)<br \/>\n{<br \/>\nTokenType result;<br \/>\nswitch (type)<br \/>\n{<br \/>\ncase TokenType.Identificator:<br \/>\nresult = TokenType.Identificator;<br \/>\nbreak;<br \/>\ncase TokenType.Num:<br \/>\nresult = TokenType.Num;<br \/>\nbreak;<br \/>\ncase TokenType.BracketLeft:<br \/>\nresult = TokenType.BracketLeft;<br \/>\nbreak;<br \/>\ndefault:<br \/>\nresult = TokenType.ERROR;<br \/>\nbreak;<br \/>\n}<br \/>\nreturn result;<br \/>\n}<\/p>\n<p>public static TokenType StateOperatorBool(TokenType type)<br \/>\n{<br \/>\nTokenType result;<br \/>\nswitch (type)<br \/>\n{<br \/>\ncase TokenType.Not:<br \/>\nresult = TokenType.Not;<br \/>\nbreak;<br \/>\ncase TokenType.Identificator:<br \/>\nresult = TokenType.Identificator;<br \/>\nbreak;<br \/>\ncase TokenType.Num:<br \/>\nresult = TokenType.Num;<br \/>\nbreak;<br \/>\ncase TokenType.BracketLeft:<br \/>\nresult = TokenType.BracketLeft;<br \/>\nbreak;<br \/>\ndefault:<br \/>\nresult = TokenType.ERROR;<br \/>\nbreak;<br \/>\n}<br \/>\nreturn result;<br \/>\n}<\/p>\n<p>public static TokenType StateNum(TokenType type)<br \/>\n{<br \/>\nTokenType result;<br \/>\nswitch (type)<br \/>\n{<br \/>\ncase TokenType.OperatorBoolean:<br \/>\nresult = TokenType.OperatorBoolean;<br \/>\nbreak;<br \/>\ncase TokenType.BracketRight:<br \/>\nresult = TokenType.BracketRight;<br \/>\nbreak;<br \/>\ndefault:<br \/>\nresult = TokenType.ERROR;<br \/>\nbreak;<br \/>\n}<br \/>\nreturn result;<br \/>\n}<br \/>\n#endregion<\/p>\n<p>public override string ToString()<br \/>\n{<br \/>\nreturn this.type.ToString()+&#187; &#171;+this.name;<br \/>\n}<br \/>\n#endregion<br \/>\n}<\/p>\n<p>class Lex<br \/>\n{<br \/>\n#region \u041f\u043e\u043b\u044f<br \/>\nprivate Token _token;<br \/>\nprivate string _name;<br \/>\nprivate LexType _type;<br \/>\n#endregion<\/p>\n<p>#region \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u044b<br \/>\npublic Lex()<br \/>\n{<br \/>\nthis._name = null;<br \/>\nthis._token = null;<br \/>\nthis._type = LexType.Nonterminal;<br \/>\n}<\/p>\n<p>public Lex(string name, LexType type)<br \/>\n{<br \/>\nthis._name = name;<br \/>\nthis._token = null;<br \/>\nthis._type = type;<br \/>\n}<\/p>\n<p>public Lex(Token token)<br \/>\n{<br \/>\nthis._token = token;<br \/>\nthis._name = token.Name;<br \/>\nthis._type = LexType.Terminal;<br \/>\n}<br \/>\n#endregion<\/p>\n<p>#region \u0421\u0432\u043e\u0439\u0441\u0442\u0432\u0430<br \/>\npublic Token Token<br \/>\n{<br \/>\nget { return this._token; }<br \/>\nset<br \/>\n{<br \/>\nthis._token = value;<br \/>\nif (value != null)<br \/>\n{<br \/>\nthis._type = LexType.Terminal;<br \/>\nthis._name = value.Name;<br \/>\n}<br \/>\n}<br \/>\n}<\/p>\n<p>public LexType Type<br \/>\n{<br \/>\nget { return this._type; }<br \/>\nset { this._type = value; }<br \/>\n}<\/p>\n<p>public string Name<br \/>\n{<br \/>\nget { return this._name; }<br \/>\nset { this._name = value; }<br \/>\n}<br \/>\n#endregion<\/p>\n<p>#region \u041c\u0435\u0442\u043e\u0434\u044b<br \/>\npublic override string ToString()<br \/>\n{<br \/>\nif (this._token == null) return this._name;<br \/>\nelse return this.Token.Name;<br \/>\n}<\/p>\n<p>public Lex Clone()<br \/>\n{<br \/>\nLex clone = new Lex();<br \/>\nclone._token = this._token;<br \/>\nclone._name = this._name;<br \/>\nclone._type = this._type;<br \/>\nreturn clone;<br \/>\n}<\/p>\n<p>static public int[] GetRules(Token[] tokens, LexRuleTable ruleTable, LexRelationTable relationTable, Lex lexStart, Lex lexEnd)<br \/>\n{<br \/>\nList&lt;int&gt; ruleList;<br \/>\nint ruleNumber;<br \/>\nList&lt;Lex&gt; tempList;<br \/>\nStack&lt;Lex&gt; bufer, magazine;<br \/>\nLex tempCur, tempNext;<br \/>\nLexRelation relation;<br \/>\n\/\/<br \/>\nruleList = new List&lt;int&gt;();<br \/>\ntempList = new List&lt;Lex&gt;();<br \/>\nbufer = new Stack&lt;Lex&gt;();<br \/>\nmagazine = new Stack&lt;Lex&gt;();<br \/>\n\/\/<br \/>\nmagazine.Push(lexStart);<br \/>\nbufer.Push(lexEnd);<br \/>\nfor (int i = tokens.Length &#8212; 1; i &gt;= 0; i&#8212;)<br \/>\n{<br \/>\ntempCur = new Lex(tokens[i]);<br \/>\nif (tempCur.Token.Type == TokenType.Num || tempCur.Token.Type == TokenType.Identificator)<br \/>\ntempCur.Name = &#171;a&#187;;<br \/>\nbufer.Push(tempCur);<br \/>\n}<\/p>\n<p>while (bufer.Count &gt; 0)<br \/>\n{<br \/>\ntempCur = magazine.Peek();<br \/>\ntempNext = bufer.Peek();<br \/>\nrelation = relationTable.GetRelation(tempCur.Name, tempNext.Name);<br \/>\nswitch (relation)<br \/>\n{<br \/>\ncase LexRelation.Before:<br \/>\ncase LexRelation.Equality:<br \/>\nmagazine.Push(bufer.Pop());<br \/>\nbreak;<br \/>\ncase LexRelation.After:<br \/>\ntempNext = magazine.Pop();<br \/>\ntempCur = magazine.Peek();<br \/>\ntempList.Add(tempNext);<br \/>\nwhile (relationTable.GetRelation(tempCur.Name, tempNext.Name) == LexRelation.Equality)<br \/>\n{<br \/>\ntempNext = magazine.Pop();<br \/>\ntempCur = magazine.Peek();<br \/>\ntempList.Insert(0, tempNext);<br \/>\n}<br \/>\ntempCur = ruleTable.GetParient(tempList.ToArray(), out ruleNumber);<br \/>\ntempList.Clear();<br \/>\nif (tempCur == null) return ruleList.ToArray();<br \/>\nruleList.Add(ruleNumber);<br \/>\nmagazine.Push(tempCur);<br \/>\nbreak;<br \/>\ndefault:<br \/>\nreturn ruleList.ToArray();<br \/>\nbreak;<br \/>\n}<br \/>\n}<br \/>\n\/\/<br \/>\nreturn ruleList.ToArray();<br \/>\n}<br \/>\n#endregion<br \/>\n}<\/p>\n<p>class Tree<br \/>\n{<br \/>\n#region \u041f\u043e\u043b\u044f<br \/>\nprivate Lex _current;<br \/>\nprivate List&lt;Tree&gt; _children;<br \/>\n#endregion<\/p>\n<p>#region \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u044b<br \/>\npublic Tree()<br \/>\n{<br \/>\nthis._current = new Lex();<br \/>\nthis._children = new List&lt;Tree&gt;(3);<br \/>\n}<\/p>\n<p>public Tree(Tree parient)<br \/>\n{<br \/>\nthis._current = new Lex();<br \/>\nthis._children = new List&lt;Tree&gt;(3);<br \/>\n}<\/p>\n<p>public Tree(Lex lex)<br \/>\n{<br \/>\nthis._current = lex;<br \/>\nthis._children = new List&lt;Tree&gt;(3);<br \/>\n}<\/p>\n<p>public Tree(Lex lex, Tree parient)<br \/>\n{<br \/>\nthis._current = lex;<br \/>\nthis._children = new List&lt;Tree&gt;(3);<br \/>\n}<br \/>\n#endregion<\/p>\n<p>#region \u0421\u0432\u043e\u0439\u0441\u0442\u0432\u0430<br \/>\npublic Lex Current<br \/>\n{<br \/>\nget { return this._current; }<br \/>\nset { this._current = value; }<br \/>\n}<\/p>\n<p>public List&lt;Tree&gt; Children<br \/>\n{<br \/>\nget { return this._children; }<br \/>\nset { this._children = value; }<br \/>\n}<br \/>\n#endregion<\/p>\n<p>#region \u041c\u0435\u0442\u043e\u0434\u044b<br \/>\nprivate int GenerateNumber(Random rand, Lex lex, LexRuleTable table, int[] count, int[] decrements)<br \/>\n{<br \/>\nfloat[] p = new float[count.Length];<br \/>\nint maxindex;<br \/>\nint[] ruleNums;<br \/>\nfloat a;<br \/>\nLex tempLex;<br \/>\nfor (int i = 0; i &lt; p.Length; i++)<br \/>\n{<br \/>\na = (float)rand.NextDouble() * 100;<br \/>\np[i] = a \/ (float)count[i];<br \/>\n}<\/p>\n<p>tempLex = lex;<br \/>\nruleNums = table.GetRuleNums(tempLex);<br \/>\nif (ruleNums == null)<br \/>\nreturn -1;<br \/>\nmaxindex = ruleNums[0];<\/p>\n<p>for (int i = 1; i &lt; ruleNums.Length; i++)<br \/>\n{<br \/>\nif (p[ruleNums[i]] &gt; p[maxindex])<br \/>\n{<br \/>\nmaxindex = ruleNums[i];<br \/>\n}<br \/>\n}<br \/>\ncount[maxindex] += decrements[maxindex];<br \/>\nreturn maxindex;<br \/>\n}<\/p>\n<p>private void GenerateChildren(LexRuleTable table, int[] count, int[] decrements, Random rand)<br \/>\n{<br \/>\nif (this._current.Type == LexType.Terminal || table == null) return;<\/p>\n<p>if (this._children == null) this._children = new List&lt;Tree&gt;(3);<\/p>\n<p>int ruleNum = this.GenerateNumber(rand, this._current, table, count, decrements);<\/p>\n<p>if (ruleNum &lt; 0) return;<\/p>\n<p>List&lt;Lex&gt; tempListLex;<\/p>\n<p>this._children = new List&lt;Tree&gt;();<br \/>\ntempListLex = table.GetChildren(this._current, ruleNum);<br \/>\nfor (int i = 0; i &lt; tempListLex.Count;i++ )<br \/>\nthis._children.Add(new Tree(tempListLex[i]));<br \/>\n}<\/p>\n<p>public void GenerateTree(LexRuleTable table, int[] count, int[] decrements)<br \/>\n{<br \/>\nif (this._current == null || this._current.Type == LexType.Terminal)<br \/>\nreturn;<\/p>\n<p>Random rand = new Random((int)DateTime.Now.Millisecond);<\/p>\n<p>this.GenerateChildren(table, count, decrements, rand);<\/p>\n<p>for (int i = this._children.Count-1; i &gt;= 0; i&#8212;)<br \/>\nthis._children[i].GenerateTree(table, count, decrements);<br \/>\n}<\/p>\n<p>public void GetLexes(List&lt;Lex&gt; lexes)<br \/>\n{<br \/>\nfor (int i = 0; i &lt; this._children.Count; i++)<br \/>\nif (this._children[i]._current.Type == LexType.Nonterminal)<br \/>\nthis._children[i].GetLexes(lexes);<br \/>\nelse<br \/>\nlexes.Add(this._children[i]._current);<br \/>\n}<\/p>\n<p>public void BuildTree(Stack&lt;int&gt; rules, LexRuleTable ruleTable, Stack&lt;Token&gt; tokens)<br \/>\n{<br \/>\nif (this._current.Type == LexType.Terminal || rules.Count &lt;=0) return;<\/p>\n<p>Lex tempLex;<br \/>\nint i;<br \/>\nint ruleNumber = rules.Pop();<br \/>\nList&lt;Lex&gt; children = ruleTable.GetChildren(this._current, ruleNumber);<\/p>\n<p>if (children == null)<br \/>\n{<br \/>\nrules.Push(ruleNumber);<br \/>\nreturn;<br \/>\n}<\/p>\n<p>if (this._children == null)<br \/>\nthis._children = new List&lt;Tree&gt;(children.Count);<\/p>\n<p>for (i = 0; i &lt; children.Count; i++)<br \/>\n{<br \/>\ntempLex = children[i].Clone();<br \/>\nthis._children.Add(new Tree(tempLex));<br \/>\n}<\/p>\n<p>for (i = this._children.Count &#8212; 1; i &gt;= 0; i&#8212;)<br \/>\nif (this._children[i].Current.Type == LexType.Nonterminal)<br \/>\nthis._children[i].BuildTree(rules, ruleTable, tokens);<br \/>\nelse<br \/>\nif (tokens.Count &gt; 0)<br \/>\nthis._children[i].Current.Token = tokens.Pop();<br \/>\n}<\/p>\n<p>public void OptimizeTranzetive()<br \/>\n{<br \/>\nwhile (this.Children.Count == 1 &amp;&amp; this.Children[0] != null)<br \/>\n{<br \/>\nif (this.Children[0].Current.Type == LexType.Nonterminal)<br \/>\nthis.Children = this.Children[0].Children;<br \/>\nelse<br \/>\n{<br \/>\nthis.Current = this.Children[0].Current;<br \/>\nthis.Children = null;<br \/>\nreturn;<br \/>\n}<br \/>\n}<\/p>\n<p>for (int i = 0; i &lt; this.Children.Count; i++)<br \/>\nif (this.Children[i] != null &amp;&amp; this.Children[i].Current.Type == LexType.Nonterminal)<br \/>\nthis.Children[i].OptimizeTranzetive();<br \/>\n}<\/p>\n<p>public void OptimizeBrackets()<br \/>\n{<br \/>\nwhile (this.Children.Count == 3 &amp;&amp; this.Children[0] != null &amp;&amp; this.Children[0].Current.Type == LexType.Terminal &amp;&amp; this.Children[0].Current.Token.Type == TokenType.BracketLeft)<br \/>\n{<br \/>\nif (this.Children[1].Current.Type == LexType.Nonterminal)<br \/>\nthis.Children = this.Children[1].Children;<br \/>\nelse<br \/>\n{<br \/>\nthis.Current = this.Children[1].Current;<br \/>\nthis.Children = null;<br \/>\nreturn;<br \/>\n}<br \/>\n}<\/p>\n<p>while (this.Children.Count == 4 &amp;&amp; this.Children[0] != null &amp;&amp; this.Children[0].Current.Type == LexType.Terminal &amp;&amp; this.Children[0].Current.Token.Type == TokenType.Not)<br \/>\n{<br \/>\nthis.Children.RemoveRange(3, 1);<br \/>\nthis.Children.RemoveRange(1, 1);<br \/>\n}<\/p>\n<p>for (int i = 0; i &lt; this.Children.Count; i++)<br \/>\nif (this.Children[i] != null &amp;&amp; this.Children[i].Current.Type == LexType.Nonterminal)<br \/>\nthis.Children[i].OptimizeBrackets();<br \/>\n}<br \/>\n#endregion<br \/>\n}<\/p>\n<p>class LexRuleTable<br \/>\n{<br \/>\n#region \u041f\u043e\u043b\u044f<br \/>\nprivate List&lt;int&gt; _number;<br \/>\nprivate List&lt;Lex&gt; _lexesParient;<br \/>\nprivate List&lt;List&lt;Lex&gt;&gt; _lexesChildren;<br \/>\n#endregion<\/p>\n<p>#region \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u044b<br \/>\npublic LexRuleTable()<br \/>\n{<br \/>\nthis._number = new List&lt;int&gt;();<br \/>\nthis._lexesParient = new List&lt;Lex&gt;();<br \/>\nthis._lexesChildren = new List&lt;List&lt;Lex&gt;&gt;();<br \/>\n}<br \/>\n#endregion<\/p>\n<p>#region \u041c\u0435\u0442\u043e\u0434\u044b<br \/>\npublic void Add(Lex parient, List&lt;Lex&gt; children, int ruleNumber)<br \/>\n{<br \/>\nthis._number.Add(ruleNumber);<br \/>\nthis._lexesParient.Add(parient);<br \/>\nthis._lexesChildren.Add(children);<br \/>\n}<\/p>\n<p>public void Add(Lex parient, Lex[] children, int ruleNumber)<br \/>\n{<br \/>\nthis._number.Add(ruleNumber);<br \/>\nthis._lexesParient.Add(parient);<br \/>\nList&lt;Lex&gt; tempList = new List&lt;Lex&gt;(children.Length);<br \/>\nfor (int i = 0; i &lt; children.Length;i++ )<br \/>\ntempList.Add(children[i]);<br \/>\nthis._lexesChildren.Add(tempList);<br \/>\n}<\/p>\n<p>public void Remove(Lex parient, Lex[] children, int ruleNumber)<br \/>\n{<br \/>\nint i = 0, j = 0;<br \/>\nfor (i = 0; i &lt; this._lexesParient.Count; i++)<br \/>\n{<br \/>\nif (this._lexesChildren[i].Count == children.Length)<br \/>\nfor (j = 0; j &lt; children.Length; j++)<br \/>\nif (this._lexesChildren[i][j].Name != children[j].Name)<br \/>\nbreak;<br \/>\nif (j &gt;= children.Length)<br \/>\n{<br \/>\nthis._number.RemoveRange(i, 1);<br \/>\nthis._lexesParient.RemoveRange(i, 1);<br \/>\nthis._lexesChildren.RemoveRange(i, 1);<br \/>\nbreak;<br \/>\n}<br \/>\n}<br \/>\n}<\/p>\n<p>public void Remove(Lex parient, List&lt;Lex&gt; children, int ruleNumber)<br \/>\n{<br \/>\nint i = 0, j = 0;<br \/>\nfor (i = 0; i &lt; this._lexesParient.Count; i++)<br \/>\n{<br \/>\nif (this._lexesChildren[i].Count == children.Count)<br \/>\nfor (j = 0; j &lt; children.Count; j++)<br \/>\nif (this._lexesChildren[i][j].Name != children[j].Name)<br \/>\nbreak;<br \/>\nif (j &gt;= children.Count)<br \/>\n{<br \/>\nthis._number.RemoveRange(i, 1);<br \/>\nthis._lexesParient.RemoveRange(i, 1);<br \/>\nthis._lexesChildren.RemoveRange(i, 1);<br \/>\nbreak;<br \/>\n}<br \/>\n}<br \/>\n}<\/p>\n<p>public Lex GetParient(Lex[] children, out int ruleNumber)<br \/>\n{<br \/>\nint i = 0, j = 0;<br \/>\nfor (i = 0; i &lt; this._lexesParient.Count; i++)<br \/>\n{<br \/>\nif (this._lexesChildren[i].Count == children.Length)<br \/>\nfor (j = 0; j &lt; children.Length; j++)<br \/>\nif (this._lexesChildren[i][j].Name != children[j].Name)<br \/>\nbreak;<br \/>\nif (j == children.Length)<br \/>\n{<br \/>\nruleNumber = this._number[i];<br \/>\nreturn this._lexesParient[i];<br \/>\n}<br \/>\n}<br \/>\nruleNumber = -1;<br \/>\nreturn null;<br \/>\n}<\/p>\n<p>public Lex GetParient(List&lt;Lex&gt; children, out int ruleNumber)<br \/>\n{<br \/>\nint i = 0, j = 0;<br \/>\nfor (i = 0; i &lt; this._lexesParient.Count; i++)<br \/>\n{<br \/>\nif (this._lexesChildren[i].Count == children.Count)<br \/>\nfor (j = 0; j &lt; children.Count; j++)<br \/>\nif (this._lexesChildren[i][j].Name != children[j].Name)<br \/>\nbreak;<br \/>\nif (j == children.Count)<br \/>\n{<br \/>\nruleNumber = this._number[i];<br \/>\nreturn this._lexesParient[i];<br \/>\n}<br \/>\n}<br \/>\nruleNumber = -1;<br \/>\nreturn null;<br \/>\n}<\/p>\n<p>public int GetRuleNum(Lex[] children)<br \/>\n{<br \/>\nint i = 0, j = 0;<br \/>\nfor (i = 0; i &lt; this._lexesParient.Count; i++)<br \/>\n{<br \/>\nif (this._lexesChildren[i].Count == children.Length)<br \/>\nfor (j = 0; j &lt; children.Length; j++)<br \/>\nif (this._lexesChildren[i][j].Name != children[j].Name)<br \/>\nbreak;<br \/>\nif (j == children.Length)<br \/>\n{<br \/>\nreturn this._number[i];<br \/>\n}<br \/>\n}<br \/>\nreturn -1;<br \/>\n}<\/p>\n<p>public int GetRuleNum(List&lt;Lex&gt; children)<br \/>\n{<br \/>\nint i = 0, j = 0;<br \/>\nfor (i = 0; i &lt; this._lexesParient.Count; i++)<br \/>\n{<br \/>\nif (this._lexesChildren[i].Count == children.Count)<br \/>\nfor (j = 0; j &lt; children.Count; j++)<br \/>\nif (this._lexesChildren[i][j].Name != children[j].Name)<br \/>\nbreak;<br \/>\nif (j == children.Count)<br \/>\n{<br \/>\nreturn this._number[i];<br \/>\n}<br \/>\n}<br \/>\nreturn -1;<br \/>\n}<\/p>\n<p>public List&lt;Lex&gt; GetChildren(Lex parient, int ruleNumber)<br \/>\n{<br \/>\nint i = 0;<br \/>\nfor (i = 0; i &lt; this._lexesParient.Count; i++)<br \/>\n{<br \/>\nif (this._number[i] == ruleNumber &amp;&amp; this._lexesParient[i].Name == parient.Name)<br \/>\nreturn this._lexesChildren[i];<br \/>\n}<br \/>\nreturn null;<br \/>\n}<\/p>\n<p>public int[] GetRuleNums(Lex parient)<br \/>\n{<br \/>\nList&lt;int&gt; result = new List&lt;int&gt;();<br \/>\nint i = 0;<br \/>\nfor (i = 0; i &lt; this._lexesParient.Count; i++)<br \/>\n{<br \/>\nif (this._lexesParient[i].Name == parient.Name)<br \/>\nresult.Add(this._number[i]);<br \/>\n}<br \/>\nif (result.Count == 0)<br \/>\nreturn null;<br \/>\nelse<br \/>\nreturn result.ToArray();<br \/>\n}<\/p>\n<p>public override string ToString()<br \/>\n{<br \/>\nstring outline= &#171;\u041f\u0440\u0430\u0432\u0438\u043b\u0430&#187;;<br \/>\nfor (int i = 0; i &lt; this._number.Count; i++)<br \/>\n{<br \/>\noutline += &#171;\\n&#187; + this._number[i] + &#187; : &#187; + this._lexesParient[i].Name + &#187; -&gt;&#187;;<br \/>\nfor (int j = 0; j &lt; this._lexesChildren[i].Count; j++)<br \/>\noutline += &#187; &#187; + this._lexesChildren[i][j].Name;<br \/>\n}<br \/>\nreturn outline;<br \/>\n}<br \/>\n#endregion<br \/>\n}<br \/>\n\/\/<br \/>\nclass LexRelationTable<br \/>\n{<br \/>\nprivate List&lt;string&gt; _lexCur, _lexNext;<br \/>\nprivate List&lt;LexRelation&gt; _relations;<\/p>\n<p>public LexRelationTable()<br \/>\n{<br \/>\nthis._lexCur = new List&lt;string&gt;();<br \/>\nthis._lexNext = new List&lt;string&gt;();<br \/>\nthis._relations = new List&lt;LexRelation&gt;();<br \/>\n}<\/p>\n<p>public int Count<br \/>\n{<br \/>\nget { return this._relations.Count; }<br \/>\n}<\/p>\n<p>public void Add(string lexCurrent, string lexNext, LexRelation relation)<br \/>\n{<br \/>\nthis._lexCur.Add(lexCurrent);<br \/>\nthis._lexNext.Add(lexNext);<br \/>\nthis._relations.Add(relation);<br \/>\n}<\/p>\n<p>public void Add(Lex lexCurrent, Lex lexNext, LexRelation relation)<br \/>\n{<br \/>\nthis._lexCur.Add(lexCurrent.Name);<br \/>\nthis._lexNext.Add(lexNext.Name);<br \/>\nthis._relations.Add(relation);<br \/>\n}<\/p>\n<p>public LexRelation GetRelation(string lexCurrent, string lexNext)<br \/>\n{<br \/>\nfor (int i = 0; i &lt; this.Count; i++)<br \/>\nif (this._lexCur[i] == lexCurrent &amp;&amp; this._lexNext[i] == lexNext)<br \/>\nreturn this._relations[i];<br \/>\nreturn LexRelation.Empty;<br \/>\n}<\/p>\n<p>public void RemoveRelation(string lexCurrent, string lexNext)<br \/>\n{<br \/>\nfor (int i = 0; i &lt; this.Count; i++)<br \/>\nif (this._lexCur[i] == lexCurrent &amp;&amp; this._lexNext[i] == lexNext)<br \/>\n{<br \/>\nthis._lexCur.RemoveRange(i, 1);<br \/>\nthis._lexNext.RemoveRange(i, 1);<br \/>\nthis._relations.RemoveRange(i, 1);<br \/>\nreturn;<br \/>\n}<br \/>\n}<br \/>\n}<br \/>\n\/\/<br \/>\nclass Triad<br \/>\n{<br \/>\n#region \u041f\u043e\u043b\u044f<br \/>\nToken _oper, _obj1, _obj2;<br \/>\nint _tread1, _tread2;<br \/>\n#endregion<\/p>\n<p>#region \u041a\u043e\u043d\u0441\u0442\u0440\u0443\u043a\u0442\u043e\u0440\u044b<br \/>\npublic Triad()<br \/>\n{<br \/>\nthis._obj1 = this._obj2 = this._oper = null;<br \/>\nthis._tread1 = this._tread2 = 0;<br \/>\n}<\/p>\n<p>public Triad(Token oper, Token obj1, Token obj2)<br \/>\n{<br \/>\nthis._oper = oper;<br \/>\nthis._obj1 = obj1;<br \/>\nthis._obj2 = obj2;<br \/>\nthis._tread1 = this._tread2 = 0;<br \/>\n}<\/p>\n<p>public Triad(Token oper, int tread1, int tread2)<br \/>\n{<br \/>\nthis._oper = oper;<br \/>\nthis._tread1 = tread1;<br \/>\nthis._tread2 = tread2;<br \/>\nthis._obj1 = null;<br \/>\nthis._obj2 = null;<br \/>\n}<br \/>\n#endregion<\/p>\n<p>#region \u0421\u0432\u043e\u0439\u0441\u0442\u0432\u0430<br \/>\npublic Token Operator<br \/>\n{<br \/>\nget { return this._oper; }<br \/>\nset { this._oper = value; }<br \/>\n}<\/p>\n<p>public Token Operand1<br \/>\n{<br \/>\nget { return this._obj1; }<br \/>\nset<br \/>\n{<br \/>\nthis._obj1 = value;<br \/>\nthis._tread1 = 0;<br \/>\n}<br \/>\n}<\/p>\n<p>public Token Operand2<br \/>\n{<br \/>\nget { return this._obj2; }<br \/>\nset<br \/>\n{<br \/>\nthis._obj2 = value;<br \/>\nthis._tread2 = 0;<br \/>\n}<br \/>\n}<\/p>\n<p>public int Tread1<br \/>\n{<br \/>\nget { return this._tread1; }<br \/>\nset<br \/>\n{<br \/>\nthis._tread1 = value;<br \/>\nthis._obj1 = null;<br \/>\n}<br \/>\n}<\/p>\n<p>public int Tread2<br \/>\n{<br \/>\nget { return this._tread2; }<br \/>\nset<br \/>\n{<br \/>\nthis._tread2 = value;<br \/>\nthis._obj2 = null;<br \/>\n}<br \/>\n}<br \/>\n#endregion<\/p>\n<p>#region \u041c\u0435\u0442\u043e\u0434\u044b<br \/>\nstatic public void GetTriads(Tree tree, List&lt;Triad&gt; result, ref int num)<br \/>\n{<br \/>\nTriad tempTriad = new Triad();<\/p>\n<p>switch (tree.Children.Count)<br \/>\n{<br \/>\ncase 2:\/\/ not F \u0438\u043b\u0438 not a<br \/>\nif (tree.Children[0] != null)<br \/>\ntempTriad._oper = tree.Children[0].Current.Token;<br \/>\nif (tree.Children[1] != null)<br \/>\nif (tree.Children[1].Current.Type == LexType.Terminal)<br \/>\ntempTriad._obj1 = tree.Children[1].Current.Token;<br \/>\nelse<br \/>\n{<br \/>\nTriad.GetTriads(tree.Children[1], result, ref num);<br \/>\ntempTriad._tread1 = num;<br \/>\n}<br \/>\nelse<br \/>\ntempTriad._obj1 = null;<br \/>\ntempTriad._obj2 = null;<br \/>\nresult.Add(tempTriad);<br \/>\nnum++;<br \/>\nbreak;<br \/>\ncase 3:\/\/ \u0435\u0441\u043b\u0438 \u044d\u0442\u043e \u043e\u043f\u0435\u0440\u0430\u0442\u043e\u0440<br \/>\nif(tree.Children[1] != null)<br \/>\n{<br \/>\nif (tree.Children[1].Current.Type == LexType.Terminal &amp;&amp; tree.Children[1].Current.Token.Type == TokenType.OperatorBoolean)<br \/>\n{<br \/>\ntempTriad._oper = tree.Children[1].Current.Token;<br \/>\nif (tree.Children[0].Current != null)<br \/>\nif (tree.Children[0].Current.Type == LexType.Nonterminal)<br \/>\n{<br \/>\nTriad.GetTriads(tree.Children[0], result, ref num);<br \/>\ntempTriad._tread1 = num;<br \/>\n}<br \/>\nelse<br \/>\ntempTriad._obj1 = tree.Children[0].Current.Token;<br \/>\nif (tree.Children[2].Current != null)<br \/>\nif (tree.Children[2].Current.Type == LexType.Nonterminal)<br \/>\n{<br \/>\nTriad.GetTriads(tree.Children[2], result, ref num);<br \/>\ntempTriad._tread2 = num;<br \/>\n}<br \/>\nelse<br \/>\ntempTriad._obj2 = tree.Children[2].Current.Token;<br \/>\n}<br \/>\n}<br \/>\nresult.Add(tempTriad);<br \/>\nnum++;<br \/>\nbreak;<br \/>\ncase 4:<br \/>\ntempTriad._oper = tree.Children[1].Current.Token;<br \/>\ntempTriad._obj1 = tree.Children[0].Current.Token;<br \/>\nif (tree.Children[2].Current.Type == LexType.Nonterminal)<br \/>\n{<br \/>\nTriad.GetTriads(tree.Children[2], result, ref num);<br \/>\ntempTriad._tread2 = num;<br \/>\n}<br \/>\nelse<br \/>\ntempTriad._obj2 = tree.Children[2].Current.Token;<br \/>\nresult.Add(tempTriad);<br \/>\nnum++;<br \/>\nbreak;<br \/>\ndefault:<br \/>\nbreak;<br \/>\n}<br \/>\n}<\/p>\n<p>static public string[] GetAssemblerCode(List&lt;Triad&gt; triads)<br \/>\n{<br \/>\nstring tempCommand;<br \/>\nList&lt;string&gt; commandsList = new List&lt;string&gt;();<br \/>\nfor (int i = 0; i &lt; triads.Count; i++)<br \/>\n{<br \/>\ntempCommand = &#171;&#187;;<br \/>\nswitch (triads[i]._oper.Type)<br \/>\n{<br \/>\ncase TokenType.OperatorBoolean:<br \/>\nif (triads[i]._obj1 == null)<br \/>\ntempCommand += &#171;pop AX&#187;;<br \/>\nelse<br \/>\ntempCommand += &#171;move AX &#187; + triads[i]._obj1.Name;<br \/>\ntempCommand += &#171;\\n&#187;;<br \/>\nif (triads[i]._obj2 == null)<br \/>\ntempCommand += &#171;pop DX&#187;;<br \/>\nelse<br \/>\ntempCommand += &#171;move DX &#187; + triads[i]._obj2.Name;<br \/>\ntempCommand += &#171;\\n&#187;;<br \/>\ntempCommand += triads[i]._oper.Name.ToLower()+&#187; AX DX&#187;;<br \/>\ntempCommand += &#171;\\n&#187;;<br \/>\ntempCommand+= &#171;push AX&#187;;<br \/>\nbreak;<br \/>\ncase TokenType.Assign:<br \/>\nif (triads[i]._obj2 == null)<br \/>\ntempCommand += &#171;pop AX&#187;;<br \/>\nelse<br \/>\ntempCommand += &#171;move AX&#187; + triads[i]._obj2.Name;<br \/>\ntempCommand += &#171;\\n&#187;;<br \/>\ntempCommand += &#171;move &#171;+triads[i]._obj1.Name+&#187; AX&#187;;<br \/>\ntempCommand += &#171;\\n&#187;;<br \/>\ntempCommand += &#171;push AX&#187;;<br \/>\nbreak;<br \/>\ncase TokenType.Not:<br \/>\nif (triads[i]._obj1 == null)<br \/>\ntempCommand += &#171;pop AX&#187;;<br \/>\nelse<br \/>\ntempCommand += &#171;move AX &#187; + triads[i]._obj1.Name;<br \/>\ntempCommand += &#171;\\n&#187;;<br \/>\ntempCommand += &#171;not AX&#187;;<br \/>\ntempCommand += &#171;\\n&#187;;<br \/>\ntempCommand += &#171;push AX&#187;;<br \/>\nbreak;<br \/>\ndefault:<br \/>\nbreak;<br \/>\n}<br \/>\ncommandsList.Add(tempCommand);<br \/>\n}<br \/>\nreturn commandsList.ToArray();<br \/>\n}<\/p>\n<p>public override string ToString()<br \/>\n{<br \/>\nstring outStr = &#171;(&#187; + this._oper.Name + &#171;, &#171;;<br \/>\nif (this._obj1 == null)<br \/>\noutStr += &#171;^&#187;+this._tread1.ToString();<br \/>\nelse<br \/>\noutStr += this._obj1.Name;<br \/>\noutStr += &#171;, &#171;;<br \/>\nif (this._obj2 == null)<br \/>\noutStr += &#171;^&#187;+this._tread2.ToString();<br \/>\nelse<br \/>\noutStr += this._obj2.Name;<br \/>\noutStr += &#171;)&#187;;<br \/>\nreturn outStr;<br \/>\n}<br \/>\n#endregion<br \/>\n}<br \/>\n}<\/p>\n<p><strong>\u0424\u0430\u0439\u043b\u00a0Form1.cs<\/strong><\/p>\n<p>using System;<br \/>\nusing System.Collections.Generic;<br \/>\nusing System.ComponentModel;<br \/>\nusing System.Data;<br \/>\nusing System.Drawing;<br \/>\nusing System.Text;<br \/>\nusing System.IO;<br \/>\nusing System.Windows.Forms;<\/p>\n<p>namespace \u0422\u042f\u041f_\u043b\u04312_\u043c6<br \/>\n{<br \/>\npublic partial class Form1 : Form<br \/>\n{<br \/>\nTree genTree;<br \/>\nLexRelationTable relationTable;<br \/>\nLexRuleTable ruleTable;<br \/>\nLex S, F, T, E, a, assigne, or, xor, and, lb, rb, not, semicolon, start, end;<\/p>\n<p>public Form1()<br \/>\n{<br \/>\nInitializeComponent();<br \/>\nList&lt;Lex&gt; tempList;<\/p>\n<p>S = new Lex(&#171;S&#187;, LexType.Nonterminal);<br \/>\nF = new Lex(&#171;F&#187;, LexType.Nonterminal);<br \/>\nT = new Lex(&#171;T&#187;, LexType.Nonterminal);<br \/>\nE = new Lex(&#171;E&#187;, LexType.Nonterminal);<br \/>\na = new Lex(&#171;a&#187;, LexType.Terminal);<br \/>\nor = new Lex(&#171;or&#187;, LexType.Terminal);<br \/>\nxor = new Lex(&#171;xor&#187;, LexType.Terminal);<br \/>\nand = new Lex(&#171;and&#187;, LexType.Terminal);<br \/>\nlb = new Lex(&#171;(&#171;, LexType.Terminal);<br \/>\nrb = new Lex(&#171;)&#187;, LexType.Terminal);<br \/>\nnot = new Lex(&#171;not&#187;, LexType.Terminal);<br \/>\nassigne = new Lex(&#171;:=&#187;, LexType.Terminal);<br \/>\nsemicolon = new Lex(&#171;;&#187;, LexType.Terminal);<br \/>\nstart = new Lex(@&#187;\\s&#187;, LexType.Terminal);<br \/>\nend = new Lex(@&#187;\\e;&#187;, LexType.Terminal);<\/p>\n<p>#region \u0417\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043f\u0440\u0430\u0432\u0438\u043b<br \/>\nruleTable = new LexRuleTable();<\/p>\n<p>tempList = new List&lt;Lex&gt;();<br \/>\ntempList.Add(a);<br \/>\ntempList.Add(assigne);<br \/>\ntempList.Add(S);<br \/>\ntempList.Add(semicolon);<br \/>\nruleTable.Add(S, tempList, 0);<\/p>\n<p>tempList = new List&lt;Lex&gt;();<br \/>\ntempList.Add(F);<br \/>\nruleTable.Add(S, tempList, 1);<\/p>\n<p>tempList = new List&lt;Lex&gt;();<br \/>\ntempList.Add(T);<br \/>\ntempList.Add(or);<br \/>\ntempList.Add(F);<br \/>\nruleTable.Add(F, tempList, 2);<\/p>\n<p>tempList = new List&lt;Lex&gt;();<br \/>\ntempList.Add(T);<br \/>\ntempList.Add(xor);<br \/>\ntempList.Add(F);<br \/>\nruleTable.Add(F, tempList, 3);<\/p>\n<p>tempList = new List&lt;Lex&gt;();<br \/>\ntempList.Add(T);<br \/>\nruleTable.Add(F, tempList, 4);<\/p>\n<p>tempList = new List&lt;Lex&gt;();<br \/>\ntempList.Add(E);<br \/>\ntempList.Add(and);<br \/>\ntempList.Add(T);<br \/>\nruleTable.Add(T, tempList, 5);<\/p>\n<p>tempList = new List&lt;Lex&gt;();<br \/>\ntempList.Add(E);<br \/>\nruleTable.Add(T, tempList, 6);<\/p>\n<p>tempList = new List&lt;Lex&gt;();<br \/>\ntempList.Add(not);<br \/>\ntempList.Add(lb);<br \/>\ntempList.Add(F);<br \/>\ntempList.Add(rb);<br \/>\nruleTable.Add(E, tempList, 7);<\/p>\n<p>tempList = new List&lt;Lex&gt;();<br \/>\ntempList.Add(lb);<br \/>\ntempList.Add(F);<br \/>\ntempList.Add(rb);<br \/>\nruleTable.Add(E, tempList, 8);<\/p>\n<p>tempList = new List&lt;Lex&gt;();<br \/>\ntempList.Add(not);<br \/>\ntempList.Add(a);<br \/>\nruleTable.Add(E, tempList, 9);<\/p>\n<p>tempList = new List&lt;Lex&gt;();<br \/>\ntempList.Add(a);<br \/>\nruleTable.Add(E, tempList, 10);<br \/>\n#endregion<\/p>\n<p>this.outputBox.Text = ruleTable.ToString();<\/p>\n<p>#region \u0417\u0430\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0435 \u0442\u0430\u0431\u043b\u0438\u0446\u044b \u043e\u0442\u043d\u043e\u0448\u0435\u043d\u0438\u0439 \u043f\u0440\u0435\u0434\u0448\u0435\u0441\u0442\u0432\u043e\u0432\u0430\u043d\u0438\u044f<br \/>\nrelationTable = new LexRelationTable();<br \/>\nrelationTable.Add(or, not, LexRelation.Before);<br \/>\nrelationTable.Add(or, lb, LexRelation.Before);<br \/>\nrelationTable.Add(or, a, LexRelation.Before);<br \/>\nrelationTable.Add(or, E, LexRelation.Before);<br \/>\nrelationTable.Add(or, T, LexRelation.Before);<br \/>\nrelationTable.Add(or, F, LexRelation.Equality);<\/p>\n<p>relationTable.Add(xor, not, LexRelation.Before);<br \/>\nrelationTable.Add(xor, lb, LexRelation.Before);<br \/>\nrelationTable.Add(xor, a, LexRelation.Before);<br \/>\nrelationTable.Add(xor, E, LexRelation.Before);<br \/>\nrelationTable.Add(xor, T, LexRelation.Before);<br \/>\nrelationTable.Add(xor, F, LexRelation.Equality);<\/p>\n<p>relationTable.Add(and, not, LexRelation.Before);<br \/>\nrelationTable.Add(and, lb, LexRelation.Before);<br \/>\nrelationTable.Add(and, a, LexRelation.Before);<br \/>\nrelationTable.Add(and, E, LexRelation.Before);<br \/>\nrelationTable.Add(and, T, LexRelation.Equality);<\/p>\n<p>relationTable.Add(not, lb, LexRelation.Equality);<br \/>\nrelationTable.Add(not, a, LexRelation.Equality);<\/p>\n<p>relationTable.Add(lb, not, LexRelation.Before);<br \/>\nrelationTable.Add(lb, lb, LexRelation.Before);<br \/>\nrelationTable.Add(lb, a, LexRelation.Before);<br \/>\nrelationTable.Add(lb, E, LexRelation.Before);<br \/>\nrelationTable.Add(lb, T, LexRelation.Before);<br \/>\nrelationTable.Add(lb, F, LexRelation.Equality);<\/p>\n<p>relationTable.Add(rb, or, LexRelation.After);<br \/>\nrelationTable.Add(rb, xor, LexRelation.After);<br \/>\nrelationTable.Add(rb, and, LexRelation.After);<br \/>\nrelationTable.Add(rb, rb, LexRelation.After);<br \/>\nrelationTable.Add(rb, semicolon, LexRelation.After);<br \/>\nrelationTable.Add(rb, end, LexRelation.After);<\/p>\n<p>relationTable.Add(a, or, LexRelation.After);<br \/>\nrelationTable.Add(a, xor, LexRelation.After);<br \/>\nrelationTable.Add(a, and, LexRelation.After);<br \/>\nrelationTable.Add(a, rb, LexRelation.After);<br \/>\nrelationTable.Add(a, semicolon, LexRelation.After);<br \/>\nrelationTable.Add(a, assigne, LexRelation.Equality);<br \/>\nrelationTable.Add(a, end, LexRelation.Equality);<\/p>\n<p>relationTable.Add(assigne, not, LexRelation.Before);<br \/>\nrelationTable.Add(assigne, lb, LexRelation.Before);<br \/>\nrelationTable.Add(assigne, a, LexRelation.Before);<br \/>\nrelationTable.Add(assigne, T, LexRelation.Before);<br \/>\nrelationTable.Add(assigne, E, LexRelation.Before);<br \/>\nrelationTable.Add(assigne, F, LexRelation.Before);<br \/>\nrelationTable.Add(assigne, S, LexRelation.Equality);<\/p>\n<p>relationTable.Add(S, semicolon, LexRelation.Equality);<\/p>\n<p>relationTable.Add(F, rb, LexRelation.Equality);<br \/>\nrelationTable.Add(F, semicolon, LexRelation.After);<br \/>\nrelationTable.Add(F, end, LexRelation.After);<\/p>\n<p>relationTable.Add(T, or, LexRelation.Equality);<br \/>\nrelationTable.Add(T, xor, LexRelation.Equality);<br \/>\nrelationTable.Add(T, rb, LexRelation.After);<br \/>\nrelationTable.Add(T, semicolon, LexRelation.After);<br \/>\nrelationTable.Add(T, end, LexRelation.After);<\/p>\n<p>relationTable.Add(E, or, LexRelation.After);<br \/>\nrelationTable.Add(E, xor, LexRelation.After);<br \/>\nrelationTable.Add(E, and, LexRelation.Equality);<br \/>\nrelationTable.Add(E, rb, LexRelation.After);<br \/>\nrelationTable.Add(E, semicolon, LexRelation.After);<br \/>\nrelationTable.Add(E, end, LexRelation.After);<\/p>\n<p>relationTable.Add(semicolon, end, LexRelation.After);<br \/>\nrelationTable.Add(semicolon, semicolon, LexRelation.After);<\/p>\n<p>relationTable.Add(start, a, LexRelation.Before);<br \/>\nrelationTable.Add(start, F, LexRelation.Before);<br \/>\n#endregion<br \/>\n}<\/p>\n<p>private void buttonRecognize_Click(object sender, EventArgs e)<br \/>\n{<br \/>\nthis.outputBox.Clear();<br \/>\nstring line = this.inputBox.Text;<br \/>\nToken[] tokens = Token.GetTokens(line);<br \/>\nif (tokens != null)<br \/>\n{<br \/>\nfor (int i = 0; i &lt; tokens.Length; i++)<br \/>\nthis.outputBox.Text += tokens[i].ToString() + &#171;\\n&#187;;<br \/>\n}<\/p>\n<p>bool result = Token.ScanGeneral(tokens, 0, tokens.Length &#8212; 1); \/\/Token.ScanBool(tokens, 0, tokens.Length &#8212; 1);<br \/>\nthis.outputBox.Text += &#171;\\n\\n&#187; + result;<br \/>\n}<\/p>\n<p>private void buttonLoad_Click(object sender, EventArgs e)<br \/>\n{<br \/>\nstring filename = Directory.GetCurrentDirectory() + @&#187;\\compiletext.txt&#187;;<br \/>\nStreamReader reader = new StreamReader(filename, Encoding.GetEncoding(1251));<br \/>\nthis.inputBox.Text = reader.ReadToEnd();<br \/>\n}<\/p>\n<p>private void buttonGenerate_Click(object sender, EventArgs e)<br \/>\n{<br \/>\nTree tree = new Tree(new Lex(&#171;S&#187;, LexType.Nonterminal));<br \/>\nint rulesCount = 11;<br \/>\nint[] maxCount = new int[rulesCount];<br \/>\nint[] decrements = new int[rulesCount];<br \/>\nfor (int i = 0; i &lt; maxCount.Length; i++)<br \/>\nmaxCount[i] = 1;<\/p>\n<p>#region \u0428\u0442\u0440\u0430\u0444\u044b<br \/>\ndecrements[0] = 7;<br \/>\ndecrements[1] = 0;<br \/>\ndecrements[2] = 3;<br \/>\ndecrements[3] = 3;<br \/>\ndecrements[4] = 0;<br \/>\ndecrements[5] = 3;<br \/>\ndecrements[6] = 0;<br \/>\ndecrements[7] = 4;<br \/>\ndecrements[8] = 4;<br \/>\ndecrements[9] = 0;<br \/>\ndecrements[10] = 0;<br \/>\n#endregion<\/p>\n<p>tree.GenerateTree(ruleTable, maxCount, decrements);<\/p>\n<p>List&lt;Lex&gt; leses = new List&lt;Lex&gt;();<br \/>\ntree.GetLexes(leses);<\/p>\n<p>string line = &#171;&#187;;<br \/>\nfor (int i = 0; i &lt; leses.Count; i++)<br \/>\nline += leses[i].Name + &#187; &#171;;<\/p>\n<p>this.inputBox.Text = line;<\/p>\n<p>this.treeViewOut.Nodes.Clear();<br \/>\nthis.treeViewOut.Nodes.Add(tree.Current.Name);<br \/>\nthis.GetTree(tree, this.treeViewOut.Nodes[0]);<\/p>\n<p>}<\/p>\n<p>private void GetTree(Tree tree, TreeNode treeNode)<br \/>\n{<br \/>\nTreeNode temp;<br \/>\nfor (int i = 0; i &lt; tree.Children.Count; i++)<br \/>\nif (tree.Children[i].Current.Type == LexType.Nonterminal)<br \/>\n{<br \/>\ntemp = new TreeNode(tree.Children[i].Current.Name);<br \/>\nGetTree(tree.Children[i], temp);<br \/>\ntreeNode.Nodes.Add(temp);<br \/>\n}<br \/>\nelse<br \/>\ntreeNode.Nodes.Add(tree.Children[i].Current.Name);<br \/>\n}<\/p>\n<p>private void button1_Click(object sender, EventArgs e)<br \/>\n{<br \/>\nTree tree = new Tree(S);<br \/>\nint[] rules;<br \/>\nstring line;<br \/>\nStack&lt;int&gt; stackRule = new Stack&lt;int&gt;();<br \/>\nStack&lt;Token&gt; stackToken = new Stack&lt;Token&gt;();<\/p>\n<p>this.outputBox.Clear();<br \/>\nthis.outputBox.Text= this.ruleTable.ToString()+&#187;\\n\\n&#187;;<\/p>\n<p>line = this.inputBox.Text;<br \/>\nToken[] tokens = Token.GetTokens(line);<br \/>\nif (tokens == null)<br \/>\n{<br \/>\nthis.outputBox.Text += &#171;\\nError&#187;;<br \/>\nreturn;<br \/>\n}<br \/>\nfor (int i = 0; i &lt; tokens.Length; i++)<br \/>\nthis.outputBox.Text += tokens[i].ToString() + &#171;\\n&#187;;<br \/>\nbool result = Token.ScanGeneral(tokens, 0, tokens.Length &#8212; 1);<br \/>\nthis.outputBox.Text += &#171;\\n\\n&#187; + result;<\/p>\n<p>rules = Lex.GetRules(tokens, ruleTable, relationTable, start, end);<\/p>\n<p>if (rules == null)<br \/>\n{<br \/>\nthis.outputBox.Text += &#171;\\nError&#187;;<br \/>\nreturn;<br \/>\n}<br \/>\noutputBox.Text += &#171;\\n&#187;;<br \/>\nfor (int i = rules.Length &#8212; 1; i &gt;= 0; i&#8212;) this.outputBox.Text += rules[i] + &#187; &#171;;<\/p>\n<p>for (int i = 0; i &lt; rules.Length; i++) stackRule.Push(rules[i]);<br \/>\nfor (int i = 0; i &lt; tokens.Length; i++) stackToken.Push(tokens[i]);<\/p>\n<p>tree.BuildTree(stackRule, ruleTable, stackToken);<br \/>\ngenTree = tree;<br \/>\n\/\/<br \/>\nthis.treeViewOut.Nodes.Clear();<br \/>\nthis.treeViewOut.Nodes.Add(tree.Current.Name);<br \/>\nthis.GetTree(tree, this.treeViewOut.Nodes[0]);<br \/>\n}<\/p>\n<p>private void optimizeButton_Click(object sender, EventArgs e)<br \/>\n{<br \/>\ngenTree.OptimizeTranzetive();<br \/>\ngenTree.OptimizeBrackets();<br \/>\nthis.treeViewOut.Nodes.Add(genTree.Current.Name);<br \/>\nthis.GetTree(genTree, this.treeViewOut.Nodes[1]);<\/p>\n<p>int num=0;<br \/>\nList&lt;Triad&gt; triats = new List&lt;Triad&gt;();<\/p>\n<p>Triad.GetTriads(genTree, triats, ref num);<\/p>\n<p>outputBox.Text += &#171;\\n\\n\u0422\u0440\u0438\u0430\u0434\u044b:\\n&#187;;<br \/>\nfor (int i = 0; i &lt; triats.Count; i++)<br \/>\noutputBox.Text += (i+1)+&#187;) &#171;+triats[i].ToString()+&#187;\\n&#187;;<\/p>\n<p>outputBox.Text += &#171;\\n\\n\u041a\u043e\u0434 \u0430\u0441\u0441\u0435\u043c\u0431\u043b\u0435\u0440\u0430:\\n&#187;;<br \/>\nstring[] commands = Triad.GetAssemblerCode(triats);<br \/>\nfor (int i = 0; i &lt; triats.Count; i++)<br \/>\noutputBox.Text+= commands[i]+&#187;\\n&#187;;<br \/>\n}<br \/>\n}<br \/>\n}<\/p>\n<p>\u0421\u043a\u0440\u0438\u043d\u0448\u043e\u0442 \u041f\u041e\u00a0\u0420\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u0442\u0435\u043b\u044c:<\/p>\n<p><a href=\"http:\/\/studentpmr.ru\/foto\/2016\/04\/Raspoznavatel.jpg\" rel=\"attachment wp-att-26089\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-medium wp-image-26089\" src=\"http:\/\/studentpmr.ru\/foto\/2016\/04\/Raspoznavatel-300x168.jpg\" alt=\"\u0420\u0430\u0441\u043f\u043e\u0437\u043d\u0430\u0432\u0430\u0442\u0435\u043b\u044c\" width=\"300\" height=\"168\" srcset=\"https:\/\/studentpmr.ru\/foto\/2016\/04\/Raspoznavatel-300x168.jpg 300w, https:\/\/studentpmr.ru\/foto\/2016\/04\/Raspoznavatel-768x429.jpg 768w, https:\/\/studentpmr.ru\/foto\/2016\/04\/Raspoznavatel.jpg 880w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<p>compiletext.txt<\/p>\n<p>var1 := b or ( c and \u00a0d);<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u0424\u0430\u0439\u043b Token.cs using System; using System.Collections.Generic; using System.Collections; using System.Text; namespace \u0422\u042f\u041f_\u043b\u04312_\u043c6 { enum TokenType { ERROR =0, Num, Identificator, BracketLeft, BracketRight, Not, End, Assign, OperatorBoolean};<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[],"_links":{"self":[{"href":"https:\/\/studentpmr.ru\/index.php?rest_route=\/wp\/v2\/posts\/26087"}],"collection":[{"href":"https:\/\/studentpmr.ru\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/studentpmr.ru\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/studentpmr.ru\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/studentpmr.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=26087"}],"version-history":[{"count":1,"href":"https:\/\/studentpmr.ru\/index.php?rest_route=\/wp\/v2\/posts\/26087\/revisions"}],"predecessor-version":[{"id":26090,"href":"https:\/\/studentpmr.ru\/index.php?rest_route=\/wp\/v2\/posts\/26087\/revisions\/26090"}],"wp:attachment":[{"href":"https:\/\/studentpmr.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=26087"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studentpmr.ru\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=26087"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studentpmr.ru\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=26087"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}