コード例 #1
0
        //<常量定义> ::= <标识符>=<无符号整数>
        public void decconst()
        {
            Error        er = compiler.error;
            symbol_table st = compiler.st;

            if (sym == symlist.iden)
            {
                getsym();
                if (sym == symlist.equality || sym == symlist.becomes)
                {
                    if (sym == symlist.becomes)
                    {
                        er.adderror(1);
                    }
                    getsym();
                    if (sym == symlist.number)
                    {
                        st.add(symlist.constsym);  //加入符号表
                        getsym();
                    }
                    else//常数声明标识符后不是数字
                    {
                        er.adderror(2);
                    }
                }
                else//不是等号
                {
                    er.adderror(3);
                }
            }
            else   //不是标识符
            {
                er.adderror(4);
            }
        }
コード例 #2
0
        //<变量说明部分>::= var<标识符>{,<标识符>};
        public void decvar()
        {
            Error        er = compiler.error;
            symbol_table st = compiler.st;

            if (sym == symlist.iden)
            {
                st.add(symlist.varsym); //加入符号表
                dx[level]++;            //分配地址
                getsym();
            }
            else//不是标识符
            {
                er.adderror(4);
            }
        }
コード例 #3
0
        //<分程序>::=[<常量说明部分>][变量说明部分>][<过程说明部分>]<语句>
        public void subprogram(List <symlist> followlst)
        {
            //int point = compiler.st.point;
            //int pi = compiler.st.dx[point];

            List <symlist> nextlst = new List <symlist>();
            symbol_table   st = compiler.st;
            Pcode          pcode = compiler.pc;
            Error          er = compiler.error;
            int            ti, ch;

            level++;
            dx[level]         = 3;
            ti                = st.dx[st.point];
            st.stable[ti].adr = pcode.cx;
            pcode.gen("JMP", 0, 0);
            //判断是否分程序的层次超过最大层数3
            if (level > 3)
            {
                er.adderror(26);
            }
            do
            {
                //<常量说明部分> ::= const<常量定义>{,<常量定义>};
                if (sym == symlist.constsym)
                {
                    do
                    {
                        getsym();
                        decconst();
                    } while (sym == symlist.comma);
                    if (sym == symlist.semicolon)
                    {
                        getsym();
                    }
                    else//少了一个分号
                    {
                        er.adderror(5);
                    }
                }
                //<变量说明部分>::= var<标识符>{,<标识符>};
                if (sym == symlist.varsym)
                {
                    do
                    {
                        getsym();
                        decvar();
                    } while (sym == symlist.comma);
                    if (sym == symlist.semicolon)
                    {
                        getsym();
                    }
                    else//少了一个分号
                    {
                        er.adderror(5);
                    }
                }
                //<过程说明部分> ::= <过程首部><分程序>;{<过程说明部分>}
                //<过程首部> ::= procedure<标识符>;
                while (sym == symlist.proceduresym)
                {
                    getsym();
                    if (sym == symlist.iden)
                    {
                        st.add(symlist.proceduresym);//加入符号表
                        getsym();
                    }
                    else//不是标识符
                    {
                        er.adderror(4);
                    }
                    if (sym == symlist.semicolon)
                    {
                        getsym();
                    }
                    else
                    {
                        er.adderror(5);
                    }
                    nextlst.AddRange(followlst);
                    nextlst.Add(symlist.semicolon);
                    subprogram(nextlst);
                    if (sym == symlist.semicolon)
                    {
                        getsym();
                        nextlst.Clear();
                        nextlst.AddRange(followlst);
                        nextlst.Add(symlist.iden);
                        nextlst.Add(symlist.proceduresym);
                        check(nextlst, followlst, 6);
                    }
                    else
                    {
                        er.adderror(5);
                    }
                }
                nextlst.Clear();
                nextlst.AddRange(stateSymSet);
                nextlst.Add(symlist.iden);
                check(nextlst, declareSymSet, 7);
            } while (declareSymSet.Contains(sym));

            pcode.pcdeolst[st.stable[ti].adr].a = pcode.cx;
            st.stable[ti].adr  = pcode.cx;
            st.stable[ti].size = dx[level];
            ch = pcode.cx;
            pcode.gen("INT", 0, dx[level]);
            nextlst.Clear();
            nextlst.AddRange(followlst);
            nextlst.Add(symlist.semicolon);
            nextlst.Add(symlist.endsym);
            statement(nextlst);
            pcode.gen("OPR", 0, 0);
            check(nextlst, new List <symlist>(), 8);
            level--;
            int point = compiler.st.point;

            if (level < point)
            {
                if (point > 0)
                {
                    int delnum = compiler.st.dx[point] - compiler.st.dx[point - 1];
                    for (int i = 0; i < delnum; i++)
                    {
                        compiler.st.stable.RemoveAt(compiler.st.stable.Count - 1);
                    }
                    compiler.st.point--;
                }
            }

            //compiler.st.point = point;
            //compiler.st.dx[compiler.st.point] = pi;
        }