示例#1
0
        Lexem MinusOrLexem(ref int row, string substring)
        {
            LexemList.Add(new Lexem(row, "-", 22));
            substring = substring.Remove(0, 1);

            return(null);
        }
示例#2
0
        Lexem Constant(ref int row, string substring)
        {
            float tempFloat;

            if (float.TryParse(substring.Replace('.', ','), out tempFloat))
            {
                int tempInt;
                int index;
                if (int.TryParse(substring, out tempInt))
                {
                    index = AddConstant(tempInt, "int");
                }
                else
                {
                    index = AddConstant(tempFloat, "float");
                }

                LexemList.Add(new Lexem(row, substring, 38, indexConst: index));
            }
            else
            {
                throw new Exception($"Oops, I can`t understand constant {substring} in {row} row");
            }
            return(null);
        }
示例#3
0
 private void outputLexem()
 {
     nextLexem.value = currentLexemString.ToString();
     LexemList.Add(nextLexem);
     currentLexemString.Clear();
     currentState = State.Input;
     return;
 }
示例#4
0
        Lexem ReservedLexem(ref int row, string substring)
        {
            if (substring == Environment.NewLine)
            {
                LexemList.Add(new Lexem(row, "¶", 3));
                row++;
            }
            else
            {
                int code = Check.IsReservedLexem(substring);
                if (code != -1)
                {
                    LexemList.Add(new Lexem(row, substring, code));
                }
                else
                {
                    throw new Exception($"Oops, I can`t understand constant '{substring}' in {row} row");
                }
            }

            return(null);
        }
示例#5
0
        Lexem Identifier(ref int row, string substring)
        {
            Idnt tempIdnt = IdentifierList.Find(x => x.Name == substring);

            if (tempIdnt != null)
            {
                LexemList.Add(new Lexem(row, tempIdnt.Name, 34, indexIdnt: tempIdnt.Index)); return(null);
            }
            else
            {
                if (LexemList.Count == 0)
                {
                    throw new Exception("Identifier can`t be first");
                }

                Lexem temp = LexemList.Last <Lexem>();
                if (temp.Code == 30)
                {
                    IdentifierList.Add(new Idnt(substring, IdentifierList.Count, "int"));
                }
                else if (temp.Code == 31)
                {
                    IdentifierList.Add(new Idnt(substring, IdentifierList.Count, "float"));
                }
                else if (temp.Code == 0)
                {
                    IdentifierList.Add(new Idnt(substring, IdentifierList.Count, "program"));
                }
                else
                {
                    throw new Exception($"Opps,I see a problem.Maybe you forgot about type 'int', 'float'or 'program' type in row {row} near '{substring}'");
                }
            }
            LexemList.Add(new Lexem(row, substring, 34, indexIdnt: IdentifierList.Count - 1));
            return(null);
        }
示例#6
0
        private void ParserLine(List <LexicalUnit> lexUnitInRowList)
        {
            int   tempInt;
            float tempFloat;

            foreach (var unit in lexUnitInRowList)
            {
                //if we have this lexem in reserved lexem
                tempInt = Check.IsReservedLexem(unit.Substring);
                if (tempInt >= 0)
                {
                    LexemList.Add(new Lexem(unit.Row, unit.Substring, tempInt));
                    continue;
                }

                //if this lexem is a constant
                if (float.TryParse(unit.Substring.Replace('.', ','), out tempFloat))
                {
                    if (int.TryParse(unit.Substring, out tempInt))
                    {
                        var const_ = ConstantList.Find(x => x._Const == tempInt);

                        if (const_ != null)
                        {
                            LexemList.Add(new Lexem(unit.Row, unit.Substring, 38, indexConst: const_.Index));
                        }
                        else
                        {
                            ConstantList.Add(new Const(tempInt, ConstantList.Count, "int"));
                            LexemList.Add(new Lexem(unit.Row, unit.Substring, 38, indexConst: ConstantList.Count - 1));
                        }
                    }
                    else
                    {
                        var const_ = ConstantList.Find(x => x._Const == tempInt);

                        if (const_ != null)
                        {
                            LexemList.Add(new Lexem(unit.Row, unit.Substring, 38, indexConst: const_.Index));
                        }
                        else
                        {
                            ConstantList.Add(new Const(tempInt, ConstantList.Count, "float"));
                            LexemList.Add(new Lexem(unit.Row, unit.Substring, 38, indexConst: ConstantList.Count - 1));
                        }
                    }
                    continue;
                }

                //if this lexem is a idnt;
                Idnt tempIdnt = IdentifierList.Find(x => x.Name == unit.Substring);
                if (tempIdnt != null)
                {
                    LexemList.Add(new Lexem(unit.Row, tempIdnt.Name, 35, indexIdnt: tempIdnt.Index));
                }
                else
                {
                    if (lexUnitInRowList.Exists(x => (x.Substring.Contains("float"))) &&
                        (lexUnitInRowList.Exists(x => x.Substring.Contains("="))))
                    {
                        IdentifierList.Add(new Idnt(unit.Substring, IdentifierList.Count, "float"));
                        LexemList.Add(new Lexem(unit.Row, unit.Substring, 35, indexIdnt: IdentifierList.Count - 1));
                    }


                    else if (lexUnitInRowList.Exists(x => (x.Substring.Contains("int"))) &&
                             (lexUnitInRowList.Exists(x => x.Substring.Contains("="))))
                    {
                        IdentifierList.Add(new Idnt(unit.Substring, IdentifierList.Count, "int"));
                        LexemList.Add(new Lexem(unit.Row, unit.Substring, 35, indexIdnt: IdentifierList.Count - 1));
                    }


                    else if ((lexUnitInRowList.Exists(x => x.Substring.Contains("program"))))
                    {
                        IdentifierList.Add(new Idnt(unit.Substring, IdentifierList.Count, "program"));
                        LexemList.Add(new Lexem(unit.Row, unit.Substring, 35, indexIdnt: IdentifierList.Count - 1));
                    }

                    else
                    {
                        throw new Exception($"not defined variable {unit.Substring} ");
                    }
                }
            }
        }