expressionreader.cs

来自「编译原理语法分析和词法分析综合实验: 源程序、可执行程序、测试程序文件、程序运行」· CS 代码 · 共 72 行

CS
72
字号
using System;
using System.Collections.Generic;
using System.Text;

namespace syn
{
    class ExpressionReader
    {
        private string expression = null;
        private int curPos = 0; // Indicate where is the beginning of reading block
        private int movPos = 0; // Indicate where is the ending of reading block

        public ExpressionReader(string expression)
        {
            this.expression = expression;
        }

        public void ResetPointer()
        {
            this.curPos = this.movPos = 0;
        }

        

        public ExpressAtom NextAtom()
        {
            ExpressAtom atom = null;
            
            while (movPos < expression.Length)
            {
                // Here, I suppose all operands are single char, otherwise change it to the Number style below
                if (Operator.IsOperator(expression[movPos]))
                {
                    atom = new ExpressAtom(new Operator( expression[movPos]));
                  
                    break;

                }
                if(Operand.IsOperand(expression[movPos]))
                {
                    curPos = movPos;
                    do
                    {
                        movPos++;
                        if(movPos>=expression.Length)
                            break;
                    } while ( Operand.IsOperand(expression[movPos]));
                    atom = new ExpressAtom(new Operand( expression.Substring(curPos, movPos - curPos)));
                    movPos--;
                    break;
                }


                movPos++;
                curPos = movPos;
            }
            // adjust posision pointer
            movPos++;
            curPos = movPos;

            return atom;
        }



        

        

    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?