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

📄 runtime.h

📁 一个Pascal子集语言的编译器
💻 H
📖 第 1 页 / 共 2 页
字号:
        return 0;
    else
        return -1;
}

void runTime::GetWords()
{
    fin2.open("words.txt");             //定义文件读取指针 
    Layer=0;
    SegmentIndex=-1;
    VariantCount=0;
    
    int state=0;                        //在一般情况下,state值为0时,读到begin时层数需要向前滚
                                        //但当在读完program,procedure或是function后,读到的第一个
                                        //begin并不需要增加层数,故此时的state值为1 
    char TempWord[256];                 //临时变量,分别记录当前单词名称和其值 
    int TempValue;              
    
    while(fin2>>TempWord)
    {
        if(strcmp(TempWord,"@")==0) continue;      //对回车字符不加处理 
        
            
        if(strcmp(TempWord,"program")==0)       //如果遇到了程序声明符,则将头信息记录 
        {
            fin2>>TempWord;                     //读入 "id" 
            VariantCount++;                     //每当读入id后都需要将计数器加1 
            Segment[VariantCount]=++SegmentIndex;
            SegmentCount=SegmentIndex;
            
            WordCount[SegmentIndex]=0;          //单词个数的初始化,头信息不用记录到单词中 
            ParamentCount[SegmentIndex]=0;      //准备记录其参数的个数 
            fin2>>TempWord;                     //读入 "(" 
            
            fin2>>TempWord;                     //读入 "id"或是")" 
            
            if(TempWord[0]!=')')
                {
                VariantCount++; 
                ParamentCount[SegmentIndex]++;
                strcpy(Parament[SegmentIndex][ParamentCount[SegmentIndex]], VariantName[VariantCount]);
                                                //将变量名称存入参数信息中
                                                 
                fin2>>TempWord;                 //读入","或是")"
                while(TempWord[0]==',')
                    {
                    fin2>>TempWord;             //读入下一个参数"id" 
                    VariantCount++; 
                    ParamentCount[SegmentIndex]++;
                    strcpy(Parament[SegmentIndex][ParamentCount[SegmentIndex]], VariantName[VariantCount]);
                                                //将变量名称存入参数信息中
                                             
                    fin2>>TempWord;             //读入","或是")"
                    }
                }
            fin2>>TempWord;                     //读入";"
            
            state=1;                            //让其后的第一个begin不进行操作 
            
        }
        else if(strcmp(TempWord,"procedure")==0)//如果遇到了过程声明符,则基本和program处理相似 
        {
        
            Layer++;
            fin2>>TempWord;                     //读入 "id" 
            VariantCount++;                     //每当读入id后都需要将计数器加1 
            Segment[VariantCount]=++SegmentIndex;
            SegmentCount=SegmentIndex;
            
            WordCount[SegmentIndex]=0;          //单词个数的初始化,头信息不用记录到单词中 
            ParamentCount[SegmentIndex]=0;      //准备记录其参数的个数 
            fin2>>TempWord;                     //读入 "(" 
            
            fin2>>TempWord;                     //读入 "id"或是")" 
            
            if(TempWord[0]!=')')
                {
                VariantCount++; 
                ParamentCount[SegmentIndex]++;
                
                strcpy(Parament[SegmentIndex][ParamentCount[SegmentIndex]], VariantName[VariantCount]);
                                                //将变量名称存入参数信息中
                                                 
                fin2>>TempWord;                 //读入","或是":"
                while(TempWord[0]==',')
                    {
                    fin2>>TempWord;             //读入下一个参数"id" 
                    VariantCount++; 
                    ParamentCount[SegmentIndex]++;
                    strcpy(Parament[SegmentIndex][ParamentCount[SegmentIndex]], VariantName[VariantCount]);
                                                //将变量名称存入参数信息中
                                             
                    fin2>>TempWord;             //读入","或是":"
                    }
                fin2>>TempWord>>TempValue;      //读入 "def x"   
                fin2>>TempWord;                 //读入 ")"    
                    
                }
            fin2>>TempWord;                     //读入";"
            
            state=1;
        
        
        }
        else if(strcmp(TempWord,"function")==0) //如果遇到了函数声明符,则记录相应的信息 
        {
            Layer++;
            fin2>>TempWord;                     //读入 "id" 
            VariantCount++;                     //每当读入id后都需要将计数器加1 
            Segment[VariantCount]=++SegmentIndex;
            SegmentCount=SegmentIndex;
            
            WordCount[SegmentIndex]=0;          //单词个数的初始化,头信息不用记录到单词中 
            ParamentCount[SegmentIndex]=0;      //准备记录其参数的个数 
            fin2>>TempWord;                     //读入 "(" 
            
            fin2>>TempWord;                     //读入 "id"或是")" 
            
            if(TempWord[0]!=')')
                {
                VariantCount++; 
                ParamentCount[SegmentIndex]++;
                strcpy(Parament[SegmentIndex][ParamentCount[SegmentIndex]], VariantName[VariantCount]);
                                                //将变量名称存入参数信息中
                fin2>>TempWord;                 //读入","或是":"
                while(TempWord[0]==',')
                    {
                    fin2>>TempWord;             //读入下一个参数"id" 
                    VariantCount++; 
                    ParamentCount[SegmentIndex]++;
                    strcpy(Parament[SegmentIndex][ParamentCount[SegmentIndex]], VariantName[VariantCount]);
                                                //将变量名称存入参数信息中
                                             
                    fin2>>TempWord;             //读入","或是":"
                    }
                fin2>>TempWord>>TempValue;      //读入 "def x"   
                fin2>>TempWord;                 //读入 ")"    
                }
            fin2>>TempWord;                     //读入":"
            fin2>>TempWord>>TempValue;          //读入"def x"
            fin2>>TempWord;                     //读入";"
            state=1; 
        
        }
        else if(strcmp(TempWord,"begin")==0)
        {
            if(state==0)
                {
                Layer++;                            //将所在层数增加 
                strcpy(Word[SegmentIndex][WordCount[SegmentIndex]],TempWord);   
                                                    //并将其记录到数组中 
                WordCount[SegmentIndex]++;
                }
            else
                state=0;
            
            if(Layer==0)                            //是主程序的开始 
            {
                SegmentIndex=0;
            }                
        
        }
        else if(strcmp(TempWord,"end")==0)
        {
            Layer--;                                //将所在层数增加
                                                    //并在下面判断是否已经到达最底线 
            if(Layer==0)                            //说明已经写完一个函数或过程 
            {
                fin1>>TempWord;                     //读入每个函数或过程后面的";"
                state=1;                            //此时如果后面读到begin则不会被记录 
            }
            else if(Layer==-1)                      //已经到达文件的末尾 
            {
                fin1>>TempWord;                     //读入最后的"."
            }
        }
        else
        {
            strcpy(Word[SegmentIndex][WordCount[SegmentIndex]],TempWord);
                                                //如果是一般字符则将其送到数组中 
            if(
                                                //如果遇到这些词就将其后面所跟的属性读进来 
            (strcmp(TempWord,"number")==0)
            ||
            (strcmp(TempWord,"op")==0)
            ||
            (strcmp(TempWord,"def")==0)
            ||
            (strcmp(TempWord,"relop")==0)
            ||
            (strcmp(TempWord,"ids")==0)
            )
                fin2>>Value[SegmentIndex][WordCount[SegmentIndex]]; 
                              
        
            WordCount[SegmentIndex]++;
        
            if(strcmp(TempWord,"id")==0)    VariantCount++;
            
            
        }
        
        
        
    }


}

int runTime::Compile()
/*
    主要模块
    
    需要完成的工作:1。检查变量的作用域  
                    2。从文件将信息获取到变量  
                    3。再将变量中信息按照执行顺序存到目标文件 

*/
{
    Initialize();       
    if(CheckWords()!=0) return -1;
    //下面的工作是从文件中读取各个字符到变量中
     
    GetWords();
    
    
    
    return 0;
}


#endif

⌨️ 快捷键说明

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