2、lib.h>#defineWORDLEN13//保留字个数#defineMAXIDLEN50//标识符最长长度#defineSYMBOLNUM32//种别码个数typedefenumSYMBOL{NOL,IDENT,NUMBER,PLUS,MINUS,TIMES,SLASH,ODDSYM,EQL,NEQ,LSS,LEQ,GTR,GEQ,LPAREN,RPAREN,COMMA,SEMICOLON,PERIOD,BECOMES,BEGINSYM,ENDSYM,1FSYM,THENSYM,WHILESYM
3、,WRITESYM,READSYM,DOSYM,CALLSYM,CONSTSYM,VARSYM,PROCSYM}SYMBOL;//定义种别码voidGetchar(void);//取下一个字符voidGetbc(void);//如当前字符为白字符,则读字符至不为白字符时停止voidConcat(void)://将当前字符加入token字voidRetract(void);〃回退一字符intReserve(void)://判断token字中单词是否是保留字iniGctsym(void);//从当前文件
4、中识别出一单词,并给出其类型和值voidErrorsym(void);//打印错误信息#endif⑶Symbol.c^include"basedata,h"#inelude"symbol,h"#inelude#inelude#ineludechar*WORD[WORDLEN]二{"BEGIN",〃CALL〃,"CONST",〃D0〃,"END",〃TF“,〃ODD〃,"PROCEDURE",〃READ〃,"THEN〃,“VAR〃,〃WHILE〃
5、,〃WRITE"};//保留字字符串表,川于将保留字种别码转为字符串输出SYMBOLWSYM[WORDLEN]={BEGINSYM,CALLSYM,C0NSTSYM,DOSYM,ENDSYM,TFSYM,ODDSYM,PROCSYM,READSYM,THENSYM,VARSYM,WH1LESYM,WRITESYM};//保留字种别码表char*SNAME[SYMBOLNUM]={〃N0I/,〃1DENT〃,"NUMBER","PLUS","MINUS","TIMES","SLASH",〃ODDSYM
6、〃,〃EQL〃,"NEQ〃,〃LSS〃,〃LEQ〃,〃GTR〃,〃GEQ“,〃LPAREN〃,"RPAREN","COMMA",〃SEMTCOI,ON〃,"PERTOD","BECOMES",〃BEGTNSYM〃,〃ENDSYM〃,,zTFSYf,〃THENSYM〃,“WH1TESYW‘,〃WRITESYM",〃READSYM〃,〃DOSYM〃,〃CALLSYM〃,〃CONSTSY『,〃VARSYM〃,〃PROCSY『};//单词字符串表,用于将保留字种别码转为字符串输出SYMBOLsym;//最
7、近已识的单词种别码chartoken[MAX1DLEN+1];//最近已识别的单词intnum;//ft近已识别的数字值charch;//最近已识别的字符intcol=l,row=l;//当前行和列值FILE*fd;//指向待编译文件externFILE*fout;//指向存放结果文件voidGetchar(void){ch=fgetc(fd);if(ch!=E0F&&ch!=')col++;return;}voidGetbc(void){whi.1e(ch=SPACE
8、
9、ch=-FABLE
10、
11、
12、ch=-){if(ch=‘){row++;col=l;}Getchar();}//为空字符则一XL读至不为空字符voidRetract(void){fseek(fd,-11,SEEK_CUR);col—;}voidConcat(void){chartemp[2];temp[0]=ch;temp[l]=, *;strcat(token,temp);}intReserve(void){inti,j;chartenip[60];j=strlen(token);fo