Пример #1
0
        /*
        在main.cs中调用,格式化输出词法分析结果
        */
        public void formateResult(string codeIn, string lResult)
        {
            string[] lineArr = Regex.Split(codeIn.Trim(), "\r\n", RegexOptions.IgnoreCase);
            string[] resultArr = Regex.Split(lResult, "\r\n", RegexOptions.IgnoreCase);

            int resultIndex = 0;
            Regex regex;
            for (int i = 0; i < lineArr.Length; i++)
            {
                //创建一行,并设置属性
                DataGridViewRow dr = new DataGridViewRow();
                dr.CreateCells(lGrid);
                dr.Cells[0].Value = "行:" + (i + 1);
                dr.Cells[1].Value = lineArr[i].Trim();
                dr.Height = 20;
                if (lineArr[i].Trim() != "")
                    dr.DefaultCellStyle.BackColor = System.Drawing.Color.Silver;
                lGrid.Rows.Add(dr);

                regex = new Regex("行:" + (i+1)+".*");
                while (regex.IsMatch(resultArr[resultIndex]))
                {
                    string[] sublineArr = Regex.Split(resultArr[resultIndex], "\t\t", RegexOptions.IgnoreCase);
                    DataGridViewRow drSub = new DataGridViewRow();
                    drSub.CreateCells(lGrid);
                    drSub.Cells[0].Value = "   " + (i + 1);
                    drSub.Cells[1].Value = sublineArr[1].Trim();
                    drSub.Cells[2].Value = sublineArr[2].Trim();
                    drSub.Height = 18;
                    lGrid.Rows.Add(drSub);
                    resultIndex++;
                }
            }

            //人为加入一个Token:EOF,作为结束符号
            Token temp = new Token("EOF", keyTable["EOF"], line);
            Global.tokenArr.Add(temp);
        }
Пример #2
0
 public Token(Token tmp)
 {
     this.src = tmp.src;
     this.type = tmp.type;
     this.lineNum = tmp.lineNum;
 }
        //匹配return语句并返回 返回值类型
        private void Return(int id)
        {
            int return_record = n;
            ++n;//return

            int type = Global.tokenArr[n].type;
            if (isExpr(type))
            {
                Expr();
                HandleExp();
            }
            Token t = new Token("" + Global.idTable[Global.tokenArr[id].src].numOfVar, (int)Symbol.INTEGER, Global.idTable[Global.tokenArr[id].src].numOfVar);
            exprArr.Add(t);//参数个数
            exprArr.Add(Global.tokenArr[return_record]);
            if (!isInMain)
                HandleExp();
            Reset();
            ++n;//;
        }
Пример #4
0
        /*
        将读取的符号放入Token数组中(在此处判断sym的类型)
        */
        private void addToToken(string s)
        {
            //值、类型、行号
            Token temp = new Token(s, -1, -1);//缺省类型为-1(错误类型)

            if (keyTable.ContainsKey(s)) //为关键字、操作符、非空界符
            {
                temp.type = keyTable[s];
            }
            else if (isIdent(s))                //为标识符
            {
                temp.type = (int)Symbol.IDENT;
            }
            else if (isInt(s))                  //为整数
            {
                temp.type = (int)Symbol.INTEGER;
            }
            else if (isReal(s))                 //为实数
            {
                temp.type = (int)Symbol.REALNUM;
            }

            temp.lineNum = line;
            Global.tokenArr.Add(temp);
        }
 private void printMidcode_ArrAsn()
 {
     List<Token> tmpExprArr = new List<Token>(exprArr);
     int indexNum = 0;
     Token arr_id = new Token(exprArr[exprArr.Count - 1]);//不能直接赋值,要深复制
     Token asn = new Token("=", (int)Symbol.BECOMES, arr_id.lineNum);
     while (tmpExprArr.Count > 1)//到最后只剩一个id
     {
         Reset();
         Token index = new Token("" + indexNum, (int)Symbol.INTEGER, arr_id.lineNum);
         Token id = new Token(arr_id);
         exprArr.Add(index);
         exprArr.Add(id);
         exprArr.Add(tmpExprArr[0]);
         exprArr.Add(asn);
         HandleExp();
         tmpExprArr.RemoveAt(0);
         ++indexNum;
     }
 }
 private bool isReal(Token token)
 {
     bool result = false;
     if (token.type == (int)Symbol.IDENT && Global.idTable.ContainsKey(token.src))
         result = Global.idTable[token.src].type == (int)Symbol.REAL;//为1说明是浮点类型
     else if(token.src[0] == '@' && token.src.Length > 1)
         result = token.src[1] == 'R';//为临时real变量
     else
         result = token.type == (int)Symbol.REALNUM;
     return result;
 }
 private void HandleId(Token token, int type, int len = -1, int assign = -1)
 {
     ID id = new ID(token.src, ID.GetCount(), type, len, assign);
     Global.idTable.Add(token.src, id);
 }
 private string GetFrontFix(Token t)
 {
     if (isReal(t))
         return "R";
     else
         return "I";
 }
Пример #9
0
        private void HandleId(Token token, int type, int len = -1, int assign = -1)
        {
            if (Global.idTable.ContainsKey(token.src))
            {
                ++errNum;
                errStr += "line " + token.lineNum + " : \t变量 \"" + token.src + "\" 重复定义\r\n";
                return;
            }

            ID id = new ID(token.src, ID.GetCount(), type, len, assign);
            Global.idTable.Add(token.src, id);
            //ID.RefreshCount(len);
        }