#include<stdio.h> #include<stdlib.h> #include<string.h> #include<ctype.h> #include<iostream> #include<fstream> using namespace std; struct symbol { char * str; int coding; }; char *keyword_list[34] = { "void", "char", "int", "float", "double", "short", "long", "signed", "unsigned", "struct", "union", "enum", "typedef", "sizeof", "auto", "static", "register", "extern", "const", "volatile", "return", "continue", "break", "goto", "if", "else", "switch", "case","default","for","do","while","scanf","printf"}; char *operator_list[44] = { "{","}","[","]","(",")",".","->","~","++","--", "!","&","*","/","%","+","-","<<",">>",">", ">=","<","<=","==","!=","^","|","&&", "||","?","=","/=","*=","%=","+=","-=","&=","^=","|=",",","#",";",":"}; char ch; //读入的字符 char strToken[20] = ""; //读入的字符串 int eof_flag = 0; int num = 1;//编码的数字(为了递增) int row = 1; struct symbol keywords[34]; struct symbol identifiers[44]; FILE *fp = NULL; FILE *fw = NULL; ofstream out; //给单词符号设定种别编码 void initialization() { //给关键字设定种别编码 for (int i = 0;i < 34;i++) { keywords[i].str = keyword_list[i]; keywords[i].coding = num; num++; } //给算符和界符设定种别编码 for (int i = 0;i < 44;i++) { identifiers[i].str = operator_list[i]; identifiers[i].coding = num; num++; } //数字79,标识符80 } //把下一个字符读入ch中 void getNextChar(FILE *ffp) { if ((ch = getc(ffp)) == EOF) { eof_flag = 1; } if (ch == ' ') row++; } //检查ch的字符是否为空白符、回车或者制表符,若是,则反复调用getNextChar (),直至ch中读入一非上述符号 void getbc(FILE * ffp) { while (ch == ' ' || ch == ' ' || ch == ' ') { getNextChar(ffp); } } //判断ch是否为字母 bool isLetter(char ch) { return isalpha(ch); } //判断ch是否为数字 bool isDigit(char ch) { return isdigit(ch); } //判断ch是否为下划线 bool isUnderline(char ch) { if (ch == '_') return 1; else return 0; } //将输入的字符ch连接到strToken void concat() { char * tmp = &ch; strcat(strToken, tmp); } //把搜索指针回调一个字符位置 void retract(FILE * ffp) { fseek(ffp, -1, SEEK_CUR); ch = ' '; } //对于strToken中的字符串判断它是否为保留字,若它是保留字则给出它的编码,否则返回0 int reserve_string(char * str) { for (int i = 0;i < 34;i++) { if ((strcmp(str, keywords[i].str)) == 0) { return keywords[i].coding; } } return 0; } //返回strToken中所识别出的算符和界符编码 int reserve_operator(char* ch) { for (int i = 0;i < 44;i++) { if ((strcmp(ch, identifiers[i].str)) == 0) { return identifiers[i].coding; } } return 0; } //出错处理 void error() { printf(" ********Error********************* "); printf(" row %d Invaild symbol ! ! ! ", row); printf(" ********Error********************* "); exit(0); } void write_result( int x,char *str ) { char data[50]; strcpy(data,"("); int m = x; char s[20]; char ss[20]; int i=0,j=0; if (x < 0)// 处理负数 { m = 0 - m; j = 1; ss[0] = '-'; } while (m>0) { s[i++] = m % 10 + '0'; m /= 10; } s[i] = '