2、lib.h>#defineWORDLEN13//保留字個(gè)數(shù)#defineMAXIDLEN50//標(biāo)識(shí)符最長(zhǎng)長(zhǎng)度#defineSYMBOLNUM32//種別碼個(gè)數(shù)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);//取下一個(gè)字符voidGetbc(void);//如當(dāng)前字符為白字符,則讀字符至不為白字符時(shí)停止voidConcat(void)://將當(dāng)前字符加入token字voidRetract(void);〃回退一字符intReserve(void)://判斷token字中單詞是否是保留字iniGctsym(void);//從當(dāng)前文件
4、中識(shí)別出一單詞,并給出其類型和值voidErrorsym(void);//打印錯(cuò)誤信息#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"};//保留字字符串表,川于將保留字種別碼轉(zhuǎn)為字符串輸出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『};//單詞字符串表,用于將保留字種別碼轉(zhuǎn)為字符串輸出SYMBOLsym;//最
7、近已識(shí)的單詞種別碼chartoken[MAX1DLEN+1];//最近已識(shí)別的單詞intnum;//ft近已識(shí)別的數(shù)字值charch;//最近已識(shí)別的字符intcol=l,row=l;//當(dāng)前行和列值FILE*fd;//指向待編譯文件externFILE*fout;//指向存放結(jié)果文件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]=,