⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 firstpass.cs

📁 c--解释器
💻 CS
📖 第 1 页 / 共 5 页
字号:
using System;
using System.Collections;
using System.Text;

namespace compiler
{
    class FirstPass
    {
        // set keywords

        Hashtable setKey = new Hashtable();

        // token序列
       ArrayList tokens = new ArrayList();

        //源代码
        string[] read;
        int row = 0;
        int col = 0;


        //要读取的一个字符
        char ch;

        //token序列的index
        int index = 0;

         
        //语法树
        Tree theTree;

        
        //树根
        TreeNode the_root;

        //错误列表
        ArrayList errorList = new ArrayList();

        //constructor
        public FirstPass()
        {
            setKey.Add("const", "const");
            setKey.Add("int", "int");
            setKey.Add("real", "real");
            setKey.Add("while", "while");
            setKey.Add("read", "read");
            setKey.Add("write", "write");
            setKey.Add("if", "if");
            setKey.Add("else", "else");
            setKey.Add("return", "return");

        }


        //constructor with reference read

        public FirstPass(string[] readfrom)
        {
            setKey.Add("const", "const");
            setKey.Add("int", "int");
            setKey.Add("real", "real");
            setKey.Add("while", "while");
            setKey.Add("read", "read");
            setKey.Add("write", "write");
            setKey.Add("if", "if");
            setKey.Add("else", "else");
            setKey.Add("function", "function");
            setKey.Add("void", "void");
            setKey.Add("return", "return");
            this.read = readfrom;


        }
        
        
        
        //match key words
        private bool matchKey(string wordfrom)
        {
            try
            {
                return this.setKey.ContainsValue(wordfrom);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return false;
            }




        }


        //语法分析程序
        public bool parser()
        {
            try
            {
                

                //程序节点
                this.the_root = new TreeNode(0);


                do
                {
                    //全局声明部分
                    if (this.matchToken("const") || this.matchToken("int") || this.matchToken("real"))
                    {
                        //调用全局声明
                        this.glo_decl_part();

                    }

                     //方法部分
                    else if (this.matchToken("function"))
                    {
                        this.getNextToken();

                        //调用方法部分
                        this.function_part();
                    }

                    else
                    {
                        //报错
                        //记录错误
                        Token theError = this.getToken();
                        throw new ComException(theError.getRow(), theError.getColumn(), 0);
                    }


                    if (!(this.index < tokens.Count)) { break;}

                } while (this.matchToken("const") || this.matchToken("int") || this.matchToken("real") || this.matchToken("function"));


                TreeNode the_top = new TreeNode(-1);
                the_top.addChildren(the_root);

                this.theTree = new Tree(the_top,the_root);
                return true;
            }
           
            catch (ComException ce) 
            {
                //假如错误队列
                this.errorList.Add(ce.getMessage());
                return false;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return false;
            }
        }

        
        
        //条件部分 下
        TreeNode condition_condi_part() 
        {
            try 
            { 
                //一个条件节点
                TreeNode the_condition_condi = new TreeNode(24);

                //第一个表达式
                TreeNode the_expression1 = this.expression_part();
                //作为条件部分的孩子
                the_condition_condi.addChildren(the_expression1);

                //关系运算符
                Token the_relation_sbl = this.getToken();

                //匹配关系运算符
                if(the_relation_sbl.getRel()>=6 && the_relation_sbl.getRel()<=11 )
                {
                    //关系运算符作为 条件部分的孩子
                    the_condition_condi.addChildren(the_relation_sbl);

                    this.getNextToken();
                    
                    //第二个表达式
                    TreeNode the_expression2 = this.expression_part();
                    //作为条件部分的孩子
                    the_condition_condi.addChildren(the_expression2);

                    return the_condition_condi;
                }
                else
                { //关系运算符错误
                    //记录错误
                    Token theError = this.getToken();

                    this.getNextToken();

                    //跳到  ) 
                    while (!this.matchToken(")"))
                    {
                        this.getNextToken();
                    }

                    //抛出
                    throw new ComException(theError.getRow(), theError.getColumn(), 7);
                }



 
            }
            catch (ComException ce)
            {   //假如错误队列
                this.errorList.Add(ce.getMessage());
                return null;
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                return null;

            }

        } 


        //方法部分 下
        void function_part() 
        {
            try 
            {
                //返回类型为int 或 int[]
               // if (this.matchToken("int")) 
               // {

                    // 一个分程序节点
                    TreeNode the_partPro = new TreeNode(1);
                    //加入树根为孩子
                    this.the_root.addChildren(the_partPro);

                    //一个方法部分节点
                    TreeNode the_function = new TreeNode(9);
                    //加入分程序为孩子
                    the_partPro.addChildren(the_function);

                    //一个返回类型节点
                    TreeNode the_return_type =this.return_type_part();
                    //作为方法部分的孩子
                    the_function.addChildren(the_return_type);

                       
                       //匹配标识符
                       if (this.getToken().isIdentifier())
                       {
                           //标识符作为方法部分的孩子
                           the_function.addChildren(this.getToken());

                           this.getNextToken();

                           if (this.matchToken("("))
                           {
                               this.getNextToken();
                               
                               //无参数
                               if (this.matchToken(")"))
                               {
                                   //什么都不做
                                   this.getNextToken();
                               }
                               //有参数
                               else 
                               {
                                   //一个参数说明节点
                                   TreeNode the_para_exp = this.para_exp_part();
                                   //参数说明节点作为方法部分的孩子
                                   the_function.addChildren(the_para_exp);
                                   
                                   //方法头结束
                                   if (this.matchToken(")"))
                                   {
                                       //什么都不做
                                       this.getNextToken();
                                   }
                                   else 
                                   {
                                       //记录错误
                                       Token theError = this.getToken();

                                       this.getNextToken();
                                       //跳过整个方法部分
                                       while (!this.matchToken("}"))
                                       {
                                           this.getNextToken();
                                       }
                                       //跳过 }
                                       this.getNextToken();

                                       //抛出
                                       throw new ComException(theError.getRow(), theError.getColumn(), 6);
                                   }
                               }

                               //方法体部分开始
                               if (this.matchToken("{"))
                               {
                                   //一个方法体节点
                                   TreeNode the_function_in =this.function_in_part();
                                  
                                   //作为方法部分的孩子
                                   the_function.addChildren(the_function_in);
                               }
                               else 
                               {
                                   //记录错误
                                   Token theError = this.getToken();

                                   this.getNextToken();
                                   //跳过整个方法部分
                                   while (!this.matchToken("}"))
                                   {
                                       this.getNextToken();
                                   }

                                  //跳过 }
                                   this.getNextToken();
                                   
                                   //抛出
                                   throw new ComException(theError.getRow(), theError.getColumn(), 7);
                               }


                           }
                           else 
                           {
                               //记录错误
                               Token theError = this.getToken();

                               this.getNextToken();
                               
                               //跳过整个方法部分
                               while (!this.matchToken("}"))
                               {
                                   this.getNextToken();
                               }
                               //跳过 }
                               this.getNextToken();

                               //抛出
                               throw new ComException(theError.getRow(), theError.getColumn(), 6);
                        
                           }


                       }
                       else 
                       {
                           //记录错误
                           Token theError = this.getToken();

                           this.getNextToken();

⌨️ 快捷键说明

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