?? typecheck.h
字號:
//TypeCheck.h
//inputfile: sentence.txt, wordtable2.txt
//outputfile: typecheck.txt
#ifndef TYPECHECK_H_
#define TYPECHECK_H_
#include <stdio.h>
#include <iostream>
#include <stdlib.h>
#include <string.h>
using namespace std;
/*****************************************************************************************
類型檢查器
所實現功能:
1。根據語法分析器所提供的每個語句,及標識符表進行分析,并顯示出錯誤信息。
2。類型檢查的內容包括:
(1)判斷等式兩邊是否類型相同或相容
(2)判斷賦值表達式兩邊是否類型相同或相容
(3)判斷函數與過程使用時的參數個數及類型是否正確
3。類型代碼如下:
整數 101
浮點數 102
布爾型 103
4。所需要分析的語句如下:
ids ;
ids ( ids_list ) ;
ids assignop simple ;
simple relop simple ;
write ( simple_list ) ;
simple_list -> simple
|
simple , simple_list
id_list -> id
|
id , id_list
simple -> factor
|
factor op simple
factor -> ids
|
number
|
not factor
|
true
|
false
|
( simple )
******************************************************************************************/
class typeCheck
{
private:
int CurrentLine; //記錄當前的行號信息,此信息是從語法分析結果中得到的
int Parament; //記錄當前語句所在的函數段
int VariantIndex;
int VariantCount;
int VariantKind[100];
int VariantType[100];
int VariantHave[100];
int VariantBelong[100];
char VariantName[100][256]; //定義變量信息
ifstream fin;
ifstream fin1;
ifstream fin2;
ifstream fintable;
ofstream fout; //文件指針的定義
char Errors[20][256]; //保存了各種錯誤代碼信息
int Error_Count; //記錄錯誤信息
char Word[100][256]; //用于記錄當前所讀到的單詞
int State; //記錄當前狀態
int Count; //記錄一個語句中的單詞數
int Index; //掃描各個單詞的指針
double Value[100]; //記錄各個變量的編號信息及數字的真實值
char SimpleWord[100][256];
double SimpleValue[100];
int SimpleCount;
int LeftType;
int RightType; //記錄左右兩邊的類型
protected:
void Initialize(); //初始化
int Check(); //對信息進行檢查
void CatchError(int num); //輸出錯誤信息
void DefineError(); //定義各種錯誤信息
void Prepare(); //首先將輸入文件進行轉化
int simpleCheck(); //對簡單表達式進行檢查
void TestVariant(); //判斷是否有重復變量
public:
int Compile(char* filename);
};
void typeCheck::Initialize()
{
Prepare(); //將輸入文件進行改寫
fintable.open("wordtable2.txt"); //打開單詞表文件
fin.open("temp2.txt");
Error_Count=0; //將各個變量進行初始化
DefineError();
while(fintable>>VariantIndex)
fintable>>VariantKind[VariantIndex]
>>VariantType[VariantIndex]
>>VariantName[VariantIndex]
>>VariantHave[VariantIndex]
>>VariantBelong[VariantIndex];
//將變量信息從單詞表中讀取出
VariantCount=VariantIndex;
}
void typeCheck::TestVariant()
{
for(int i=1;i<VariantCount;i++)
for(int j=i+1;j<=VariantCount;j++)
if(strcmp(VariantName[i],VariantName[j])==0)
if(VariantBelong[i]/100==VariantBelong[j]/100) //如果變量所屬于的位置都一樣則輸出錯誤信息
{
Value[1]=i;
CatchError(3);
}
}
void typeCheck::DefineError() //定義錯誤類型
{
strcpy(Errors[0]," is not a procedure. ");
strcpy(Errors[1]," parament number not matched. ");
strcpy(Errors[2]," assignment not adapt. ");
strcpy(Errors[3]," redeclared variant "); //重復定義變量,或是一個名稱變量有雙重意義
strcpy(Errors[6]," Equation can't match. "); //等式左右的變量類型不相同
strcpy(Errors[10]," information may missing. "); //warning信息
}
void typeCheck::CatchError(int num) //將錯誤信息直接輸出
{
if(num<6)
{
Error_Count++;
cout<<"Error ("<<Error_Count<<"): \""<<VariantName[int(Value[1])]<<"\" "<<Errors[num]<<" at line: "<<CurrentLine<<endl;
}
else if(num==6)
{
Error_Count++;
cout<<"Error ("<<Error_Count<<"): \"=\" "<<Errors[num]<<" at line: "<<CurrentLine<<endl;
}
else
{
cout<<"Warning : \""<<VariantName[int(Value[1])]<<"\" "<<Errors[num]<<" at line: "<<CurrentLine<<endl;
}
}
int typeCheck::simpleCheck() //返回一個表達式的值
{
int state=101;
int value;
for(int i=0;i<=SimpleCount;i++)
{
if(strcmp(SimpleWord[i],"ids")==0) //如果是變量則將其返回值
{
value=VariantType[int(SimpleValue[i])];
value=value%100+100;
}
else if(strcmp(SimpleWord[i],"number")==0)
{
if(SimpleValue[i]==int(SimpleValue[i])) //說明讀進來的 number是整數
value=101;
else //否則是浮點數
value=102;
}
else if(strcmp(SimpleWord[i],"op")==0)
{
if(SimpleValue[i]==4) //是除法
value=102;
else
value=101;
}
switch(state)
{
case 101:if(value==102) state=102;
break;
case 102:break;
case 103:if(value==102) state=102;
if(value==101) state=101;
break;
default:break;
}
}
return state;
}
void typeCheck::Prepare()
//此函數的主要作用是將帶有函數和過程名的代碼提取出來,單獨作為一行
{
fin1.open("sentence.txt"); //打開輸入文件
fout.open("temp2.txt"); //打開輸出文件
int state=0; //利用狀態表示當前是在寫入還是在記錄
double TempValue; //記錄變量的屬性
int TempCount;
char NowWord[256],NextWord[256]; //記錄當前詞和下一個詞
while(fin1>>NextWord)
{
if((strcmp(NextWord,"ids")==0)||(strcmp(NextWord,"number")==0))
fin1>>TempValue;
if(state==0) //當前為寫入狀態
{
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -