Beispiel #1
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);
        }
Beispiel #2
0
        Lexem MinusOrLexem(ref int row, string substring)
        {
            LexemList.Add(new Lexem(row, "-", 22));
            substring = substring.Remove(0, 1);

            return(null);
        }
Beispiel #3
0
        protected virtual void AnalysExpression(LexemList lexems)
        {
            for (int i = 0; i < lexems.Count; i++)
            {
                var lexem = lexems[i];

                if (lexem is LexemBracket)
                {
                    i = this.SkipBrackets(lexems, i);
                }
                else if (lexem is LexemSwitch && lexem.ToString() == "?")
                {
                    var sw = lexem as LexemSwitch;

                    var simetric = sw.Simetric;
                    if (simetric == null)
                        throw new FormatException("Switch syntax should have simetric Else path");

                    int index = lexems.FindPosition(simetric);
                    if (index <= i)
                        throw new FormatException("Switch syntax balance is incorrect");

                    var condition = lexems.Range(0, i);
                    var then = lexems.Range(i + 1, index - i - 1);
                    var other = lexems.Range(index + 1, lexems.Count - index - 1);

                    this.OnSwitch(sw, condition, then, other);

                    return;
                }
            }

            this.AnalysLogical(lexems);
        }
Beispiel #4
0
        protected override void AnalysExpression(LexemList lexems)
        {
            var first = lexems == null ? null : lexems.FirstOrDefault() as LexemOperator;
            if (first != null && first.OperatorType == LexemOperator.OperationType.Arithmetic && first.Text == "-")
                lexems.Insert(0, 0);

            base.AnalysExpression(lexems);
        }
Beispiel #5
0
 private void outputLexem()
 {
     nextLexem.value = currentLexemString.ToString();
     LexemList.Add(nextLexem);
     currentLexemString.Clear();
     currentState = State.Input;
     return;
 }
Beispiel #6
0
        protected override void Analys(LexemList lexems)
        {
            if (lexems.Count > 2 && lexems[0] is LexemVariable && lexems[1] is LexemOperator && (lexems[1] as LexemOperator).Text == "=")
            {
                _variable = lexems[0] as LexemVariable;

                if (String.IsNullOrEmpty(_variable.Name))
                    throw new FormatException("Assignable Variable Name is empty");

                this.AnalysExpression(lexems.Range(2, lexems.Count - 2));
            }
            else
                this.AnalysExpression(lexems);
        }
Beispiel #7
0
        protected virtual void AnalysElement(LexemList lexems)
        {
            if (lexems == null || lexems.Count <= 0)
                throw new FormatException("Analys Element is empty");

            if (lexems.Count == 1)
            {
                this.OnElement(lexems[0]);
                return;
            }

            if (lexems[0] is LexemBracket && lexems.FindPosition((lexems[0] as LexemBracket).Simetric) == lexems.Count - 1)
            {
                this.OnElement(lexems);
                return;
            }

            this.Scan(lexems, LexemOperator.OperationType.Code, new string[] { "->", "." }, this.Error);
        }
Beispiel #8
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);
        }
Beispiel #9
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);
        }
Beispiel #10
0
 protected virtual void AnalysComparison(LexemList lexems)
 {
     this.Scan(lexems, LexemOperator.OperationType.Comparison, null, this.AnalysArithmetic);
 }
Beispiel #11
0
        protected int SkipBrackets(LexemList lexems, int index, bool forward = true)
        {
            var bracket = lexems[index] as LexemBracket;
            if (bracket == null)
                return index;

            var result = index;

            if (bracket.Simetric != null)
                result = lexems.FindPosition(bracket.Simetric);

            if (forward && result <= index || !forward && result >= index)
                throw new FormatException($"Brackets balance is incorrect: {lexems}");

            return result;
        }
Beispiel #12
0
 protected virtual void AnalysArithmeticMultiplication(LexemList lexems)
 {
     this.Scan(lexems, LexemOperator.OperationType.Arithmetic, new string[] { "*", "/" }, this.AnalysElement);
 }
Beispiel #13
0
        protected void Scan(LexemList lexems, LexemOperator.OperationType type, string[] operators, Action<LexemList> action, bool parseFromLeftToRight = false)
        {
            //обычный разбор выражения должен идти справа налево (<-)
            //например, попробуйте построить полиз для выражения: a-b+c или a/b*c
            //только разбирая одноуровневые операции справа налево получится корректный полиз!!!

            for (int i = parseFromLeftToRight ? 0 : lexems.Count - 1; 0 <= i && i < lexems.Count; i += (parseFromLeftToRight ? 1 : -1))
            {
                var lexem = lexems[i];

                if (lexem is LexemBracket)
                {
                    i = this.SkipBrackets(lexems, i, parseFromLeftToRight);
                }
                else if (lexem is LexemOperator)
                {
                    var op = lexem as LexemOperator;

                    if (op.OperatorType == type && (operators == null || operators.Contains(op.Text)))
                    {
                        this.OnOperator(op, lexems.Range(0, i), lexems.Range(i + 1, lexems.Count - i - 1));
                        return;
                    }
                }
            }

            if (action != null)
                action(lexems);
        }
Beispiel #14
0
 protected void AnalysArithmetic(LexemList lexems)
 {
     this.AnalysArithmeticAddition(lexems);
 }
Beispiel #15
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} ");
                    }
                }
            }
        }
Beispiel #16
0
 protected abstract void OnSwitch(LexemSwitch lexem, LexemList condition, LexemList then, LexemList other);
Beispiel #17
0
 protected void AnalysLogical(LexemList lexems)
 {
     this.AnalysLogicalOr(lexems);
 }
Beispiel #18
0
 protected virtual void AnalysLogicalAnd(LexemList lexems)
 {
     this.Scan(lexems, LexemOperator.OperationType.Logical, new string[] { "&&" }, this.AnalysComparison);
 }
Beispiel #19
0
 public static LexemPoliz Create(LexemList lexems)
 {
     var result = new LexemPoliz();
     result.Analys(lexems);
     return result;
 }
Beispiel #20
0
        protected override void AnalysExpression(LexemList lexems)
        {
            if (lexems != null && lexems.Count > 0 && lexems[0].ToString() == "-")
                lexems.Insert(0, 0);

            base.AnalysExpression(lexems);
        }
Beispiel #21
0
 protected LexemPoliz(LexemList lexems)
 {
     _lexems = lexems;
 }
Beispiel #22
0
        protected override void OnSwitch(LexemSwitch lexem, LexemList condition, LexemList then, LexemList other)
        {
            this.AnalysLogical(condition);

            _lexems.Add(lexem);

            this.AnalysExpression(then);

            _lexems.Add(lexem.Simetric);

            this.AnalysExpression(other);

            lexem.End = _lexems[_lexems.Count - 1];
        }
Beispiel #23
0
 protected LexemPoliz()
 {
     _lexems = new LexemList();
 }
Beispiel #24
0
 protected override void OnOperator(LexemOperator op, LexemList left, LexemList right)
 {
     switch (op.OperatorType)
     {
         case LexemOperator.OperationType.Logical:
             {
                 if (op.Text == "||")
                 {
                     this.AnalysLogicalOr(left);
                     this.AnalysLogicalAnd(right);
                     _lexems.Add(op);
                 }
                 else if (op.Text == "&&")
                 {
                     this.AnalysLogicalAnd(left);
                     this.AnalysComparison(right);
                     _lexems.Add(op);
                 }
                 return;
             }
         case LexemOperator.OperationType.Comparison:
             {
                 this.AnalysArithmetic(left);
                 this.AnalysArithmetic(right);
                 _lexems.Add(op);
                 return;
             }
         case LexemOperator.OperationType.Arithmetic:
             {
                 if (op.Text == "+" || op.Text == "-")
                 {
                     this.AnalysArithmeticAddition(left);
                     this.AnalysArithmeticMultiplication(right);
                     _lexems.Add(op);
                 }
                 else if (op.Text == "*" || op.Text == "/")
                 {
                     this.AnalysArithmeticMultiplication(left);
                     this.AnalysElement(right);
                     _lexems.Add(op);
                 }
                 return;
             }
         case LexemOperator.OperationType.Code:
             {
                 if (op.Text == "->" || op.Text == ".")
                 {
                     this.AnalysElement(left);
                     this.AnalysElement(right);
                     _lexems.Add(op);
                 }
                 return;
             }
     }
 }
Beispiel #25
0
 protected override void OnElement(LexemList lexems)
 {
     this.AnalysExpression(lexems.Range(1, lexems.Count - 2));
 }
Beispiel #26
0
 protected override void OnElement(LexemList lexems)
 {
     _lexems.Add(lexems[0]);
     this.AnalysExpression(lexems.Range(1, lexems.Count - 2));
     _lexems.Add(lexems[lexems.Count - 1]);
 }
Beispiel #27
0
 protected void Error(LexemList lexems)
 {
     throw new FormatException($"Expression syntax not recognized: {lexems}");
 }
Beispiel #28
0
 protected virtual void Analys(LexemList lexems)
 {
     this.AnalysExpression(lexems);
 }
Beispiel #29
0
 protected abstract void OnElement(LexemList lexems);
Beispiel #30
0
 protected virtual void AnalysLogicalOr(LexemList lexems)
 {
     this.Scan(lexems, LexemOperator.OperationType.Logical, new string[] { "||" }, this.AnalysLogicalAnd);
 }
Beispiel #31
0
 protected abstract void OnOperator(LexemOperator op, LexemList left, LexemList right);