Esempio n. 1
0
 public VarInfo(SemType type, Lexema location, string scope, int offset)
 {
     Type     = type;
     Location = location;
     Params   = new List <VarInfo>();
     FullName = $"{scope}/{location.Tok}{{{offset}}}";
 }
Esempio n. 2
0
        public override bool Equals(object obj)
        {
            //
            // See the full list of guidelines at
            //   http://go.microsoft.com/fwlink/?LinkID=85237
            // and also the guidance for operator== at
            //   http://go.microsoft.com/fwlink/?LinkId=85238
            //

            if (obj == null || GetType() != obj.GetType())
            {
                return(false);
            }

            // safe because of the GetType check
            Firma f = (Firma)obj;

            // use this pattern to compare reference members
            if (Lexema.Equals(f.Lexema))
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Esempio n. 3
0
 void do3A(Lexema lexema, ref int RCh, ref int RP, ref int RS, ref int RZn, out int newAutomatStateIndex)
 {
     RCh *= 10;
     RCh += (byte)lexema.value;
     RS++;
     newAutomatStateIndex = 3 - 1;
 }
Esempio n. 4
0
 public ParseException(Lexema unexpected, params LexType[] expected) :
     base( //TODO
         $"Unexpected {Enum.GetName(typeof(LexType), unexpected.Type)} at {unexpected.Line}:{unexpected.Symbol}" +
         (expected.Any()
             ? $", expected {string.Join(" or ", expected.Select(e => Enum.GetName(typeof(LexType), e)))}"
             : ""))
 {
     Lexema = unexpected;
 }
Esempio n. 5
0
        public override string ToString()
        {
            string l = Lexema.Replace("\n", "salto");

            l = (Lexema == " ") ? "blanco" : Lexema.ToString();
            string v = Numero + "\t" + l + "\t" + Sinonimo + "\t" + Nombre;

            return(v);
        }
Esempio n. 6
0
        private VarInfo FindVar(Lexema lex)
        {
            var res = TryFindVar(lex);

            if (res == null)
            {
                throw new SemanticException($"Undefined variable: {lex.Tok}", lex);
            }
            return(res);
        }
Esempio n. 7
0
    private int sourcePos; //index of current char

    #endregion Fields

    #region Constructors

    public Scanner(string source)
    {
        this.source = new char[source.Length + 1];
        source.CopyTo(0, this.source, 0, source.Length);
        this.source[source.Length] = '\0';
        this.sourcePos = 0;
        this.lexema = new Lexema(LexType.BeginFile, 0);
        this.nextLexema = new Lexema(LexType.BeginFile, 0);
        MoveNext();
    }
Esempio n. 8
0
        private static void Mismatch(SemType type, SemType eType, Lexema l)
        {
            var t  = type == SemType.LongLongInt ? SemType.Int : type;
            var tt = eType == SemType.LongLongInt ? SemType.Int : eType;

            if (t != tt)
            {
                throw new SemanticException("Expression type mismatch", l,
                                            $"cannot assign {eType.ToStr()} to {type.ToStr()}");
            }
        }
Esempio n. 9
0
        private VarInfo AddVar(Lexema var, SemType type)
        {
            var name         = var.Tok;
            var currentFrame = environment.Last();

            if (currentFrame.ContainsKey(name))
            {
                var prev = currentFrame[name];
                throw new SemanticException($"Cannot redefine variable: `{name}`", var,
                                            $"previous declaration at {prev.Location.Line}:{prev.Location.Symbol}");
            }
            return(currentFrame[name] = VarInfo.Of(type, var, ""));
        }
Esempio n. 10
0
        private void Function()
        {
            L(LexType.TvoidType);
            Lexema nameL = null;

            Or(() => { nameL = L(LexType.Tident); },
               () => { nameL = L(LexType.Tmain); });
            var fn = AddVar(nameL, SemType.Function);

            Sure(() =>
            {
                using (NewEnv())
                {
                    L(LexType.Tlparen);
                    Maybe(() => Params(fn));
                    L(LexType.Trparen);
                    Block();
                }
            });
        }
Esempio n. 11
0
        private static Offset <Sentence> BuildSingleFlatSent(FlatBufferBuilder builder, SentenceMap sentence, int order)
        {
            // Чтение слов
            var words = new Offset <Lexema> [sentence.Capasity];

            for (int i = 0; i < sentence.Capasity; i++)
            {
                var word      = sentence.GetWordByOrder(i);
                var EntryName = builder.CreateString(word.EntryName);
                var RealWord  = builder.CreateString(word.RealWord);

                // Чтение граммем
                var pairs    = word.GetPairs();
                var grammems = new Offset <Grammema> [pairs.Count];
                int j        = 0;
                foreach (var pair in pairs)
                {
                    grammems[j] = Grammema.CreateGrammema(builder, pair.Key, pair.Value);
                    j++;
                }
                var gramsCol = Lexema.CreateGrammemsVector(builder, grammems);
                words[i] = Lexema.CreateLexema(builder, i, EntryName, word.ID_Entry, word.ID_PartOfSpeech, gramsCol,
                                               0, RealWord, word.rcind);
            }
            var wordsCol = Sentence.CreateWordsVector(builder, words);

            // Чтение узлов
            var treelist = sentence.GetTreeList();
            var nodes    = new Offset <Node> [treelist.Count];

            for (int i = 0; i < treelist.Count; i++)
            {
                nodes[i] = Node.CreateNode(builder, treelist[i].ID, treelist[i].Level,
                                           treelist[i].index, treelist[i].linktype, treelist[i].parentind);
            }
            var nodesCol = Sentence.CreateNodesVector(builder, nodes);

            var sentVal = builder.CreateString("");

            return(Sentence.CreateSentence(builder, order, nodesCol, wordsCol, sentVal));
        }
Esempio n. 12
0
        public bool ChechInden(string line, string consanents, string numbers)
        {
            string consts = "+*=)";
            bool   check  = false;

            Inden = "";
            while (true)
            {
                if (consanents.Contains(Lexema))
                {
                    check  = true;
                    Inden += Lexema.ToString();
                }
                else if (check)
                {
                    while (numbers.Contains(Lexema))
                    {
                        Inden += Lexema.ToString();
                        if (Pos == line.Length)
                        {
                            Bail = 1;
                            return(true);
                        }
                        GetToken(line);
                    }
                    if (consts.Contains(Lexema) || Pos > line.Length)
                    {
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
                GetToken(line);
            }
        }
Esempio n. 13
0
        private VarInfo AddVar(Lexema var, SemType type)
        {
            var name         = var.Tok;
            var currentFrame = environment.Last();

            if (currentFrame.ContainsKey(name))
            {
                var prev = currentFrame[name];
                throw new SemanticException($"Cannot redefine variable: `{name}`", var,
                                            $"previous declaration at {prev.Location.Line}:{prev.Location.Symbol}");
            }

            var varInfo = VarInfo.Of(type, var, Scope, allocSize);

            if (type != SemType.Function)
            {
                allocSize += GetSize(type);
                allocMax   = Math.Max(allocSize, allocMax);
            }

            return(currentFrame[name] = varInfo);
        }
Esempio n. 14
0
        private void xmlLex()
        {
            string xml = "";

            xml += "<ListaLexemas>\n";
            for (int i = 0; i < Lexemas.Count; i++)
            {
                Lexema at = (Lexema)Lexemas[i];
                xml += "  <Error>\n";
                xml += "      <Identificador>" + at.getID() + "</Identificador>\n";
                xml += "      <Lexema>" + at.getCadena() + "</Lexema>\n";
                xml += "      <Evaluacion>" + at.getEstado() + "</Evaluacion>\n";

                xml += "  </Error>\n";


                xml += "</ListaLexemas>\n";
            }
            StreamWriter File = new StreamWriter("RLexemas.xml");

            File.Write(xml);
            File.Close();
        }
Esempio n. 15
0
        private void htmlLex()
        {
            string html = "";

            html += "<!DOCTYPE html>  \n";
            html += "<html>  \n";
            html += "<head><title>Reporte de tokens</title></head>  \n";
            html += "<body> \n";
            html += "<h1>Listado de cursos</h1>  \n";
            html += "<table border=\"1px\" solid #0000 border-collapse: collapse>   \n";
            html += "<tr>  \n";
            html += "<th border=\"1px\" solid #0000 border-collapse: collapse> Identificador</th>  \n";
            html += "<th border=\"1px\" solid #0000 border-collapse: collapse> Lexema</th>  \n";
            html += "<th border=\"1px\" solid #0000 border-collapse: collapse> Aceptacion</th>  \n";

            html += "</tr>  \n";
            for (int i = 0; i < Lexemas.Count; i++)
            {
                Lexema at = (Lexema)Lexemas[i];
                html += "<tr> \n";
                html += "<td border=\"1px\" solid #0000 border-collapse: collapse> " + at.getID() + "</td>  \n";
                html += "<td border=\"1px\" solid #0000 border-collapse: collapse> " + at.getCadena() + "</td>  \n";
                html += "<td border=\"1px\" solid #0000 border-collapse: collapse> " + at.getEstado() + "</td>  \n";


                html += "</tr> \n";
            }


            html += "</table> \n";
            html += "</body>  \n";
            html += "</html>  \n";
            StreamWriter FilaEThtml = new StreamWriter("RLexemas.html");

            FilaEThtml.Write(html);
            FilaEThtml.Close();
        }
Esempio n. 16
0
        private void buttonProcess_Click(object sender, EventArgs e)
        {
            Lexema[] lex = Transliterator.Do(textIn.Text, true);

            // Clean up!
            textIn.Clear();
            foreach (object val in Lexema.GetValues(lex))
            {
                textIn.Text += val;
            }

            textOut.Clear();

            //System.Collections.Generic.
            ActionOnLexem[] stats =
            {
                do1,
                do2A,do2B,
                do3A,do3B, do3C,
                do4A,do4B,
                do5,
                do6A,do6B, do6C,
                do7,
                doError
            };

            #region transitions table initialization

            int[,] njuTab =
            {
/*(0) 1*/ { Array.IndexOf(stats, do2A),    Array.IndexOf(stats, doError), Array.IndexOf(stats, do7),     Array.IndexOf(stats, doError) },
/*(1) 2*/ { Array.IndexOf(stats, do2B),    Array.IndexOf(stats, do4A),    Array.IndexOf(stats, do3C),    Array.IndexOf(stats, doError) },
/*(2) 3*/ { Array.IndexOf(stats, do3A),    Array.IndexOf(stats, do4B),    Array.IndexOf(stats, doError), Array.IndexOf(stats, doError) },
/*(3) 4*/ { Array.IndexOf(stats, do6A),    Array.IndexOf(stats, doError), Array.IndexOf(stats, doError), Array.IndexOf(stats, do5)     },
/*(4) 5*/ { Array.IndexOf(stats, do6B),    Array.IndexOf(stats, doError), Array.IndexOf(stats, doError), Array.IndexOf(stats, doError) },
/*(5) 6*/ { Array.IndexOf(stats, do6C),    Array.IndexOf(stats, doError), Array.IndexOf(stats, doError), Array.IndexOf(stats, doError) },
/*(6) 7*/ { Array.IndexOf(stats, do3B),    Array.IndexOf(stats, doError), Array.IndexOf(stats, doError), Array.IndexOf(stats, doError) },
/*(7) e*/ { Array.IndexOf(stats, doError), Array.IndexOf(stats, doError), Array.IndexOf(stats, doError), Array.IndexOf(stats, doError) },
            };
            #endregion

            auto = new AutomatNumConst(
                // A
                new object[] {
                new Lexema(Transliterator.KindOfSymbol.Digit, null),
                new Lexema(Transliterator.KindOfSymbol.E, null),
                new Lexema(Transliterator.KindOfSymbol.Dot, null),
                new Lexema(Transliterator.KindOfSymbol.Sign, null)
            },
                // Z
                new string[] { "error", "int", "float", "real" },
                // S
                stats,
                // nju
                njuTab,
                // zeta
                new int[] { 0, 1, 2, 0, 0, 3, 0, 0 }
                );

            object[] whatsDone;
            object[] outsSequence;
            object   constant             = null;
            string   formalRepresentation = "";

            StepCount = 1;
            dataGridView1.Rows.Clear();

            auto.Step      += new TerminalStepDelegate(auto_Step);
            auto.StateIndex = 0;
            auto.Process(lex, out whatsDone, out outsSequence);

            if (outsSequence.Length < 1)
            {
                MessageBox.Show("Empty constant", "Failed",
                                MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }

            switch (outsSequence[outsSequence.Length - 1] as string)
            {
            case "int":
                constant             = auto.Ч;
                formalRepresentation = auto.Ч.ToString();
                break;

            case "float":
                auto.П               = -auto.С;
                constant             = auto.Ч * Math.Pow(10, auto.П);
                formalRepresentation =
                    String.Format("{0}*10^{1}", auto.Ч, auto.П);
                break;

            case "real":
                auto.П              *= auto.З;
                auto.П              -= auto.С;
                constant             = auto.Ч * (Math.Pow(10, auto.П));
                formalRepresentation =
                    String.Format("{0}*10^{1}", auto.Ч, auto.П);
                break;

            default:
                MessageBox.Show("Cannot parse constant", "Failed",
                                MessageBoxButtons.OK, MessageBoxIcon.Stop);
                return;
            }

            textOut.Text = String.Format("{0} (={1}).",
                                         formalRepresentation, constant.ToString()
                                         );
        }
Esempio n. 17
0
 void do5(Lexema lexema, ref int RCh, ref int RP, ref int RS, ref int RZn, out int newAutomatStateIndex)
 {
     RZn = ((char)lexema.value == '+' ? 1 : -1);
     newAutomatStateIndex = 5 - 1;
 }
Esempio n. 18
0
        public void Check()
        {
            sc.PushState();
            var l = sc.Next();

            sc.PopState();

            var inps = table["program".Of()];

            if (!inps.ContainsKey(l.Type))
            {
                throw new ParseException(l, inps.Keys.ToArray());
            }

            magaz.AddRange(inps[l.Type].Reverse());
            while (magaz.Count > 0)
            {
                var term   = magaz.Last() as Term;
                var neterm = magaz.Last() as Neterm;
                var delta  = magaz.Last() as Delta;
                if (term != null)
                {
                    var ll = sc.Next();
                    if (ll.Type != term.Type)
                    {
                        throw new ParseException(ll, term.Type);
                    }

                    switch (ll.Type)
                    {
                    case LexType.Tident:
                        lastId = ll;
                        break;

                    case LexType.TlongIntType:
                    case LexType.TintType:
                    case LexType.TcharType:
                        ttype = new Dictionary <LexType, SemType>
                        {
                            [LexType.TlongIntType] = SemType.LongLongInt,
                            [LexType.TintType]     = SemType.Int,
                            [LexType.TcharType]    = SemType.Char
                        }[ll.Type];
                        break;

                    case LexType.Tintd:
                    case LexType.Tinth:
                    case LexType.Tinto:
                        lastConst = ll.IntValue;
                        break;

                    case LexType.Tchar:
                        lastConst = ll.Tok[1];
                        break;

                    case LexType.Tand:
                    case LexType.Tor:
                    case LexType.Tdiv:
                    case LexType.Tmod:
                    case LexType.Tmul:
                    case LexType.Tplus:
                    case LexType.Tminus:
                    case LexType.Tlshift:
                    case LexType.Trshift:
                    case LexType.Txor:
                    case LexType.Tnot:
                        ops.Push(ll);
                        break;
                    }

                    magaz.RemoveAt(magaz.Count - 1);
                }
                else if (neterm != null)
                {
                    sc.PushState();
                    var ll = sc.Next();
                    sc.PopState();
                    var inpss = table[neterm];
                    if (!inpss.ContainsKey(ll.Type))
                    {
                        throw new ParseException(ll, inpss.Keys.ToArray());
                    }

                    magaz.RemoveAt(magaz.Count - 1);
                    magaz.AddRange(inpss[ll.Type].Reverse());
                }
                else
                {
                    delta?.Action(this);
                    magaz.RemoveAt(magaz.Count - 1);
                }
            }
        }
Esempio n. 19
0
 public ParseException(string unexpected, Lexema u) :
     base(unexpected + $" at {u.Line}:{u.Symbol}")
 {
 }
Esempio n. 20
0
 void do6C(Lexema lexema, ref int RCh, ref int RP, ref int RS, ref int RZn, out int newAutomatStateIndex)
 {
     RP *= 10;
     RP += (byte)lexema.value;
     newAutomatStateIndex = 6 - 1;
 }
Esempio n. 21
0
 private VarInfo TryFindVar(Lexema lex)
 {
     return(TryFindVar(lex.Tok));
 }
Esempio n. 22
0
 public SemanticException(string message, Lexema location, string reason = null) :
     base($"{message} at {location.Line}:{location.Symbol}" +
          (reason != null ? $", {reason}" : ""))
 {
 }
Esempio n. 23
0
 void do4B(Lexema lexema, ref int RCh, ref int RP, ref int RS, ref int RZn, out int newAutomatStateIndex)
 {
     newAutomatStateIndex = 4 - 1;
     return;
 }
Esempio n. 24
0
 public override string ToString()
 {
     return(Lexema.ToString() + " [" + GetTagDescription() + "] Linha " + Linha);
 }
Esempio n. 25
0
 public LexemaNode(Lexema lex)
 {
     LexicalElement = lex;
     value          = 0;
 }
Esempio n. 26
0
 public LexemaNode(double _value)
 {
     LexicalElement = Lexema.Number;
     value          = _value;
 }
Esempio n. 27
0
 public static VarInfo Of(SemType type, Lexema location, string scope, int offset = 0)
 {
     return(new VarInfo(type, location, scope, offset));
 }
Esempio n. 28
0
 void doError(Lexema lexema, ref int RCh, ref int RP, ref int RS, ref int RZn, out int newAutomatStateIndex)
 {
     //MessageBox.Show("— doError invoked.\r\n— So what now?");
     newAutomatStateIndex = 8 - 1;
 }
Esempio n. 29
0
    public void MoveNext()
    {
        lexema = nextLexema;

        int length;
        SkipSpace();
        switch (source[sourcePos]) {
        case '\0':
            nextLexema = new Lexema(LexType.EOF, sourcePos);
            break;
        case '+':
            if (source[sourcePos + 1] == '=') {
                nextLexema = new Lexema(LexType.PlusEqual, sourcePos);
                sourcePos += 2;
                break;
            } else {
                nextLexema = new Lexema(LexType.Plus, sourcePos);
                sourcePos++;
                break;
            }
        case '-':
            if (source[sourcePos + 1] == '=') {
                nextLexema = new Lexema(LexType.MinusEqual, sourcePos);
                sourcePos += 2;
                break;
            } else {
                nextLexema = new Lexema(LexType.Minus, sourcePos);
                sourcePos++;
                break;
            }
        case '*':
            if (source[sourcePos + 1] == '=') {
                nextLexema = new Lexema(LexType.StarEqual, sourcePos);
                sourcePos += 2;
                break;
            } else {
                nextLexema = new Lexema(LexType.Star, sourcePos);
                sourcePos++;
                break;
            }
        case '/':
            if (source[sourcePos + 1] == '=') {
                nextLexema = new Lexema(LexType.SlashEqual, sourcePos);
                sourcePos += 2;
                break;
            } else {
                nextLexema = new Lexema(LexType.Slash, sourcePos);
                sourcePos++;
                break;
            }
        case '^':
            nextLexema = new Lexema(LexType.Power, sourcePos);
            sourcePos++;
            break;
        case '!':
            nextLexema = new Lexema(LexType.Exclamation, sourcePos);
            sourcePos++;
            break;
        case '(':
            nextLexema = new Lexema(LexType.OpenBrace, sourcePos);
            sourcePos++;
            break;
        case ')':
            nextLexema = new Lexema(LexType.CloseBrace, sourcePos);
            sourcePos++;
            break;
        case '"':
            nextLexema = new Lexema(LexType.DoubleQuote, sourcePos);
            sourcePos++;
            break;
        case ',':
            nextLexema = new Lexema(LexType.Comma, sourcePos);
            sourcePos++;
            break;
        case '$':
            length = ScanDigits(source, sourcePos + 1);
            if (length != 0) {
                nextLexema = new Lexema(LexType.Variable, sourcePos, new string(source, sourcePos + 1, length));
                sourcePos += 1 + length;
                break;
            }
            throw new CalcException("Syntax error", sourcePos, null);
        case ':':
            if (source[sourcePos + 1] == '=') {
                nextLexema = new Lexema(LexType.EqualSign, sourcePos);
                sourcePos += 2;
                break;
            } else {
                throw new CalcException("Syntax error", sourcePos, null);
            }
        default:
            length = ScanDouble(source, sourcePos);
            if (length != 0) {
                nextLexema = new Lexema(LexType.Number, sourcePos, new string(source, sourcePos, length));
                sourcePos += length;
                break;
            }
            length = ScanIdentifier(source, sourcePos);
            if (length != 0) {
                nextLexema = new Lexema(LexType.Identifier, sourcePos, new string(source, sourcePos, length));
                sourcePos += length;
                break;
            }
            throw new CalcException("Syntax error", sourcePos, null);
        }
    }
Esempio n. 30
0
 void do6A(Lexema lexema, ref int RCh, ref int RP, ref int RS, ref int RZn, out int newAutomatStateIndex)
 {
     RZn = 1;
     RP  = (byte)lexema.value;
     newAutomatStateIndex = 6 - 1;
 }
Esempio n. 31
0
 public override bool Equals(object obj)
 {
     return(Lexema.GetType() == obj.GetType() && Lexema.ToString().Equals(obj.ToString()));
 }
Esempio n. 32
0
 void do7(Lexema lexema, ref int RCh, ref int RP, ref int RS, ref int RZn, out int newAutomatStateIndex)
 {
     RS = 0;
     newAutomatStateIndex = 7 - 1;
 }