词法分析ver1.0

来源:互联网 发布:单机游戏下载软件 编辑:程序博客网 时间:2024/05/17 21:46

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 26string 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;//初始状态为0while (!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;}


0 0
原创粉丝点击