Example #1
0
        //添加到符号表中
        public void add(symlist sym)
        {
            syntax_Analysis sa = compiler.sa;

            if (sa.level > point)
            {
                point++;
                dx[point] = dx[point - 1];
            }
            else if (sa.level < point)
            {
                //int delnum = dx[point] - dx[point - 1];
                //for (int i = 0; i < delnum; i++)
                //    stable.RemoveAt(stable.Count - 1);
                //point--;
            }
            dx[point]++;
            record re = new record();

            re.name = compiler.la.word;
            if (sym == symlist.constsym)
            {
                re.kind = "constant";
                re.val  = compiler.la.num;
                stable.Add(re);
            }
            else if (sym == symlist.varsym)
            {
                re.kind  = "variable";
                re.level = sa.level;
                //获取地址还没写re.adr =
                re.adr = compiler.sa.getadd();
                stable.Add(re);
            }
            else if (sym == symlist.proceduresym)
            {
                re.kind  = "procedure";
                re.level = sa.level;
                stable.Add(re);
            }
        }
Example #2
0
        //<表达式> ::= [+|-]<项>{<加法运算符><项>}
        public void expression(List <symlist> followlst)
        {
            Pcode          pc      = compiler.pc;
            List <symlist> nextlst = new List <symlist>();

            nextlst.AddRange(followlst);
            nextlst.Add(symlist.plus);
            nextlst.Add(symlist.minus);
            symlist symm = sym;

            if (sym == symlist.minus || sym == symlist.plus)
            {
                getsym();
                item(nextlst);
                if (symm == symlist.minus)//取反
                {
                    pc.gen("OPR", 0, 1);
                }
            }
            else
            {
                item(nextlst);
            }
            while (sym == symlist.minus || sym == symlist.plus)
            {
                symm = sym;
                getsym();
                item(nextlst);
                if (symm == symlist.plus)
                {
                    pc.gen("OPR", 0, 2);
                }
                else
                {
                    pc.gen("OPR", 0, 3);
                }
            }
        }
Example #3
0
        //<条件> ::= <表达式><关系运算符><表达式>|odd<表达式>
        public void ifstatement(List <symlist> followlst)
        {
            Pcode          pc      = compiler.pc;
            List <symlist> nextlst = new List <symlist>();
            List <symlist> n2      = new List <symlist>();
            Error          er      = compiler.error;

            nextlst.Add(symlist.equality);
            nextlst.Add(symlist.LessThan);
            nextlst.Add(symlist.LessThanE);
            nextlst.Add(symlist.MoreThan);
            nextlst.Add(symlist.MoreThanE);
            nextlst.Add(symlist.inequality);
            if (sym != symlist.oddsym)
            {
                n2.AddRange(followlst);
                n2.AddRange(nextlst);
                expression(n2);
                if (!nextlst.Contains(sym))
                {
                    er.adderror(20);
                }
                else
                {
                    symlist symm = sym;
                    getsym();
                    expression(followlst);
                    switch (symm)
                    {
                    case symlist.equality:
                        pc.gen("OPR", 0, 8);
                        break;

                    case symlist.LessThan:
                        pc.gen("OPR", 0, 13);
                        break;

                    case symlist.LessThanE:
                        pc.gen("OPR", 0, 10);
                        break;

                    case symlist.MoreThan:
                        pc.gen("OPR", 0, 12);
                        break;

                    case symlist.MoreThanE:
                        pc.gen("OPR", 0, 11);
                        break;

                    case symlist.inequality:
                        pc.gen("OPR", 0, 9);
                        break;
                    }
                }
            }
            else if (sym == symlist.oddsym)
            {
                getsym();
                expression(followlst);
                pc.gen("OPR", 0, 6);
            }
        }
Example #4
0
 public void getsym()
 {
     sym = compiler.la.getsym();
 }
Example #5
0
        //int isconst(string word)
        //{
        //    for (int i = 0; i < (word.Length); i++)
        //    {
        //        if (word[i] > '9' || word[i] < '0') return 0;
        //    }
        //    return 33;
        //}
        //int issame(char f, char s)
        //{
        //    string str = "" + f + s;
        //    if(isoperator(str)>0)
        //    {
        //        return 1;
        //    }
        //    bool a = ((f >= '0' && f <= '9') || isletter(f)) ? true : false;
        //    bool b = ((s >= '0' && s <= '9') || isletter(s)) ? true : false;
        //    if (a == b)
        //        return 1;
        //    return 0;
        //}
        //bool isiden(string word)
        //{
        //    if (!isletter(word[0])) return false;
        //    for (int i = 1; i < word.Length; i++)
        //    {
        //        if (issame(word[i], word[i])==0) return false;
        //    }
        //    return true;
        //}
        //bool typeword(string word)
        //{
        //    int a;
        //    if ((a = isreserveword(word))>0) { syn = a; return true; }
        //    //else if(Isdelimiter(word)){syn = 2;}
        //    else if ((a = isconst(word))>0) { syn = a; return true; }
        //    else if ((a = isoperator(word))>0) { syn = a; return true; }
        //    //普通标识符
        //    else if (isiden(word)) { syn = 34; return true; }
        //    else
        //    {
        //        //Console.WriteLine(word + "compile error");
        //        return false;
        //    }
        //}
        //public string print(string word)
        //{
        //    string output="";
        //    if (syn < 17)
        //    {
        //        output = string.Format("{0,-12}", word) + string.Format("{0,-12}", "关键字") + string.Format("{0,-12}", word) + '\r' + '\n';
        //    }
        //    if(syn>=17&&syn<22)
        //    {
        //        output = string.Format("{0,-12}", word) + string.Format("{0,-12}", "分界符") + string.Format("{0,-12}", word) + '\r' + '\n';
        //    }
        //    if (syn >= 22 && syn <29)
        //    {
        //        output = string.Format("{0,-12}", word) + string.Format("{0,-12}", "单字符运算符") + string.Format("{0,-12}", word) + '\r' + '\n';
        //    }
        //    if (syn >= 29 && syn < 33)
        //    {
        //        output = string.Format("{0,-12}", word) + string.Format("{0,-12}", "双字符运算符") + string.Format("{0,-12}", word) + '\r' + '\n';
        //    }
        //    if (syn ==34)
        //    {
        //        output = string.Format("{0,-12}", word) + string.Format("{0,-12}", "标识符") + string.Format("{0,-12}", word) + '\r' + '\n';
        //    }
        //    return output;
        //}
        public symlist getsym()
        {
            Error er = compiler.error;
            int   syn_num;

            //word = "";

            if (i > code.Length - 1)
            {
                return(symlist.end);
            }
            char c = code[i];

            //跳过空格
            while (c == ' ' || c == '\n' || c == '\t' || c == '\r')
            {
                if (c == '\n')
                {
                    line++;
                }
                i++;
                if (i > code.Length - 1)
                {
                    return(symlist.end);
                }
                c = code[i];
            }
            //标识符
            if (isletter(c))
            {
                word = "";
                do
                {
                    word += c;
                    i++;
                    if (i > code.Length - 1)
                    {
                        return(symlist.end);
                    }
                    c = code[i];
                } while (isletter(c) || isnum(c));
                syn_num = isreserveword(word);
                if (syn_num > 0)
                {
                    sym = (symlist)syn_num;
                }
                else
                {
                    return(symlist.iden);
                }
            }

            //数字
            else if (isnum(c))
            {
                chnum = "";
                do
                {
                    chnum += c;
                    i++;
                    if (i > code.Length - 1)
                    {
                        return(symlist.end);
                    }
                    c = code[i];
                } while (isnum(c));
                try
                {
                    num = Convert.ToInt32(chnum);
                }
                catch (OverflowException e)
                {
                    er.adderror(31);
                    num = 0;
                }
                sym = symlist.number;
            }

            //分界符
            else if ((syn_num = isdelimiter(c)) > 0)
            {
                sym = (symlist)syn_num;
                i++;
            }
            //三个双目操作
            else if (c == ':')
            {
                i++;
                if (i > code.Length - 1)
                {
                    return(symlist.end);
                }
                c = code[i];
                if (c == '=')
                {
                    sym = symlist.becomes;
                }
                else
                {
                    sym = symlist.nul;
                }
                i++;
            }
            else if (c == '<') //"<>",">=","<=",":="
            {
                i++;
                if (i > code.Length - 1)
                {
                    return(symlist.end);
                }
                c = code[i];
                if (c == '>')
                {
                    sym = symlist.inequality;
                }
                else if (c == '=')
                {
                    sym = symlist.LessThan;
                }
                else
                {
                    sym = symlist.LessThanE; i--;
                }
                i++;
            }
            else if (c == '>')
            {
                i++;
                if (i > code.Length - 1)
                {
                    return(symlist.end);
                }
                c = code[i];
                if (c == '=')
                {
                    sym = symlist.MoreThan;
                }
                else
                {
                    sym = symlist.MoreThanE; i--;
                }
                i++;
            }
            //单目操作
            else if ((syn_num = isoperator(c)) > 0)
            {
                sym = (symlist)syn_num;
                i++;
            }

            return(sym);
        }