詞法分析ver1.0

int main(){
int a/*hehe*/=100;//hehe
int b=100;
int c;
/*
xixi
*/
c = a+b;
char[] str1 = "miao";
char[] str2 = "mi
ao";
printf("%d",c);
scanf("%d",&c);
return 0;
}

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
#define _RESERVE_NUM 38
#define _DELIMILTER_NUM 26

string key[_RESERVE_NUM] = {//關鍵字表
	"void", "int", "char", "float", "double", "while",
	"do", "break", "case", "const", "continue", "default",
	"else", "enum", "extern", "for", "if", "long", "return",
	"short", "signed", "sizeof", "static", "struct", "switch",
	"typedef", "union", "unsigned", "volatile", "redister", "auto", "goto"
	"short", "long", "main", "return", "sizeof", "printf", "scanf"
};

string symbol[_DELIMILTER_NUM] = {//符號表
	"=", "+", "-", "*", "/", "++", "--", "+=", "-=",
	"*=", "/=", "==", "!=", ">", "<", ">=", "<=", "(",
	")", "[", "]", "{", "}", ",", ":", ";"
};

char ch;//存放剛剛讀取的一個數據
string strToken;       //存放構成單詞的字符串
string strline;        //存放讀取的一行
int pText = 0;        //搜索指示器
int line = 1;

int FindSymbol(string str){//尋找符號在符號表中的種別碼
	for (int i = 0; i < _DELIMILTER_NUM; i++){
		if (str == symbol[i])return i + 37;
	}
	return -1;
}

int Reserve()   //尋找字符串在關鍵字表中的種別碼,如果沒有則是標示符
{
	for (int i = 0; i < 37; i++)
	{
		if (strToken == key[i])  return i;
	}
	return -1;
}

bool IsLetter(){//檢查是否是字母
	if (((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z')))return true;
	return false;
}

bool IsDigit(){//檢查是否是數字
	if ((ch >= '0') && (ch <= '9'))return true;
	return false;
}

int FloatOrReal(string str){//檢查是浮點還是實數
	if (str.find("."))return 36;
	return 35;
}



int main(){
	ifstream in;
	in.open("input.txt");
	if (!in){
		cout << "打開文件失效。" << endl;
		return 1;
	}
	
	int sta = 0;//初始狀態爲0
	while (!in.eof()){
		getline(in,strline);//讀取一行
		cout << strline << endl;
		pText = 0;	
		while (1){//在行內分析
			sta = 0;
			strToken = "";
			ch = strline[pText];
			//cout << ch << endl;
			if ((ch == ' ') || (ch == '\t')){//跳過空格和進格
				sta = 0;
			}
			else if (IsLetter() || ch == '_'){//判斷字符串
				sta = 1;
				while (IsLetter() || ch == '_' || IsDigit()){
					strToken += ch;
					pText++;
					ch = strline[pText];
				}
				sta = 2;
				pText--;
				int code = Reserve();
				if (code){
					cout << "(" << 26+code << "," << strToken << ",line=" << line << ")" << endl;
				}
			}
			else if (IsDigit()){//判斷數字
				sta = 3;
				while (IsDigit()||ch == '.'){
					strToken += ch;
					pText++;
					ch = strline[pText];
				}
				sta = 4;
				pText--;
				int code = FloatOrReal(strToken);
				cout << "(" << code << "," << strToken << ",line=" << line << ")" << endl;
			}
			else if (ch == ';'){//判斷界符
				sta = 5;
				cout << "(" << 34 << ",;" << ",line=" << line << ")" << endl;
				
			}
			else if (ch == '(') {
				sta = 5;
				cout << "(" << 26 << ",(" << ",line=" << line << ")" << endl;
				
			}
			else if (ch == ')'){
				sta = 5;
				cout << "(" << 27 << ",)" << ",line=" << line << ")" << endl;
				
			}
			else if (ch == '['){
				sta = 5;
				cout << "(" << 28 << ",[" << ",line=" << line << ")" << endl;
			}
			else if (ch == ']'){
				sta = 5;
				cout << "(" << 29 << ",]" << ",line=" << line << ")" << endl;
			}
			else if (ch == '{') {
				sta = 5;
				cout << "(" << 30 << ",{" << ",line=" << line << ")" << endl;
				
			}
			else if (ch == '}')  {
				sta = 5;
				cout << "(" << 31 << ",}" << ",line=" << line << ")" << endl;
				
			}
			else if (ch == ',')  {
				sta = 5;
				cout << "(" << 32 << ",," << ",line=" << line << ")" << endl;
				
			}
			else if ((ch == '+')){//判斷++,+,+=
				sta = 7;
				strToken += ch;
				pText++;
				ch = strline[pText];
				if ((ch == '=')){
					sta = 8;
					strToken += ch;
				}
				else if (ch == '+'){
					sta = 9;
					strToken += ch;
				}
				else{
					sta = 7;
					pText--;
				}
				int code = FindSymbol(strToken);
				cout << "(" << code << "," << strToken << ",line=" << line << ")" << endl;
				
			}
			else if ((ch == '-')){//判斷--,-,-=
				sta = 11;
				strToken += ch;
				pText++;
				ch = strline[pText];
				if ((ch == '=')){
					sta = 12;
					strToken += ch;
				}
				else if (ch == '-'){
					sta = 13;
					strToken += ch;
				}
				else{
					sta = 11;
					pText--;
				}
				int code = FindSymbol(strToken);
				cout << "(" << code << "," << strToken << ",line=" << line << ")" << endl;
				
			}
			else if ((ch == '=')){//判斷=,==
				sta = 15;
				strToken += ch;
				pText++;
				ch = strline[pText];
				if (ch == '='){
					sta = 16;
					strToken += ch;
				}
				else{
					sta = 15;
					pText--;
				}
				int code = FindSymbol(strToken);
				cout << "(" << code << "," << strToken << ",line=" << line << ")" << endl;
			}
			else if (ch == '*'){//判斷*,*=
				sta = 18;
				strToken += ch;
				pText++;
				ch = strline[pText];
				if (ch == '='){
					sta = 19;
					strToken += ch;
				}
				else{
					sta = 18;
					pText--;
				}
				int code = FindSymbol(strToken);
				cout << "(" << code << "," << strToken << ",line=" << line << ")" << endl;
			}
			else if (ch == '/'){//判斷/,/=,//,/*
				sta = 21;
				strToken += ch;
				pText++;
				ch = strline[pText];
				if (ch == '='){
					sta = 22;
					strToken += ch;
				}
				else if (ch == '/'){//行註釋
					sta = 23;
					break;
				}
				else if (ch == '*'){//塊註釋
					sta = 24;
					pText++;
					while (1)
					{
						if (strline[pText] == '\0'){
							getline(in, strline);
							cout << strline << endl;
							line++;
							pText = 0;
						}
						if ((strline[pText] == '*') && (strline[pText + 1] == '/')){
							break;
						}
						pText++;
					}
					pText+=2;
					if (strline[pText] == '\0'){
						break;
					}
					continue;//進行塊註釋後面的內容
				}
				else{
					sta = 21;
					pText--;
				}
				int code = FindSymbol(strToken);
				cout << "(" << code << "," << strToken << ",line=" << line << ")" << endl;
			}
			else if (ch == '"'){//判斷字符常量
				sta = 24;
				pText++;
				while (strline[pText] != '"'){
					ch = strline[pText];
					strToken += ch;
					pText++;
					if (strline[pText] == '\0'){//字符常量換行處理
						getline(in, strline);
						cout << strline << endl;
						line++;
						pText = 0;
						while ((strline[pText] == ' ') || (strline[pText] == '\t')){
							pText++;
						}
					}
				}
				sta = 25;
				cout << "(-1" << "," << strToken << ",line=" << line << ")" << endl;
			}
			else if (ch == '&'){
				sta = 26;
				cout << "(65" << ",&,line=" << line << ")" << endl;
			}
			else if (ch == '\0'){//讀完一行
				break;
			}
			pText++;
		}
		line++;	
	}
	in.close();
	return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章