资源描述:
《编译——词法分析》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、#include#include#include#includetypedefstructWord/*Word*//*thenumberoftheWord*//*ifletter*inoLA'~Zthenitisaletter*/{charchWd[20];intnN;structWord*next;JWord;intIsAlphaLetter(charcletter){if(cletter>=,A,&&cletterv=Z)return1;if(clcttc
2、r>='a'&&cletter<='z')return1;return0;intIsnumbcr(charenum){if(cnum>='0'&&cnum<='9')return1;return0;intIssign(charesg)/*AccordingtotheASCIItable*/{if(csg>='!*&&csg<=,/1)return1;if(csg>=':'&&csg<='@')return1;if(csg>=T&&csg<=,J)return1;if(csg>='{'&&csgv='~')return1;return0
3、;/*Printallthewords/*palHeadisthe/*pnumHeadis/*psghcadisthevoidPrint(Word*palHead.Word*pnumHead,Word*psgHead)recognizedinthequeueoflpWord*/{headofwords*/intnPos=0;theheadofnumbers*/headofsigns*/printf(nlnthesentense,words:n);while(palHead!=0){printf("NO.%d:%su,nPos+
4、1,palHead->chWd);palHead=palHead->next;nPos++;}nPos=0;printf(nlnthesentense,numbers:M);while(pnumHead!=0){printf("NO.%d:%s",nPos+1,pnumHcad->chWd);pnumHead=pnumHead->next;nPos++;}nPos=0;printf(ulnthesentense,signs:H);while(psgHead!=0){printf("NO.%d:%sn,nPos+l,ps
5、gHead->chWd);psgHcad=psgHcad->ncxt;nPos++;voidGetWords(charlpString[],Word*palHead,Word*pnumHead,Word*psgHead)/*Torecognizethewordsinlpstringtostoreintolpword*/{/*andstorewordstopalhead,numberstopnumHead,signstopsgHead*/WordwdCurrcnt;/*IpStringistostorethesentencetobere
6、congnisewords,numbers,signsfrom*/Word*p=0;intintintintnCharPos=0;nStringLcn;nFlg=0;nl=0;if(lpString[()l==、()'){printfC'NullString!");return;for(nl=O;;nl++)/*togetthelengthandendoflpstring*/if(lpString[nIl==A0,){nStringLen=nl;break;}nFlg=0;theendoftheword*//*'nFlg'istoma
7、rkwhilc(nCharPos8、*Haverecognisedanewp=(Word*)malloc(sizcof(Word));/*andthenmallocaunittostoreit*/printf(HMemoryLack!'*);exit(-l