资源描述:
《编译原理_词法分析代码.doc》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、/*1保留字2标识符3整数4符号*/#include#include#include#include#include#defineWORD1#defineINT2charTOKEN[20];chars[2J;intN=0;structWord{intnum;charwordname[20J;structWord*next;};typedefWord*LinkList;/*尾部插入新结点勺voidinsert(LinkListL.char*x){N
2、++;Word*prenode,*newnode;prenode=L;newnc>de=(Wbrd*)rnalloc(sizeof(Wbrd));while(prenode->next)prenode=preext;newnode->next=prenode->next;prenode->nexEewnode;strcpy(newnode->wordname,x);nevvnode->num=N;/*检查是否曾经出现过,是返回N,否返回0*/intinquire(LinkListL,char*x){Word*p;p=L;p=p->nex
3、t;while(p!=NULL)if(strcmp(p->wordname,x)==0)returnp->num;elsep=p->next;return0;/*将链表内容输出到屏幕上*/voidprint(LinkListL){LinkListp;P=L;while(p->next!=NULL){p=p->next;p「intf(,f%d,%s,p->num,p->wordname);}printf(un);/*将数字字符串转化为与Z等同的数字*/inichange(charstr[20J)inti=0,a[20Linteg
4、er=(),j=(Xn;while(str[i])a[i]=int(str[i])-48;i++;n=i;while(j<=n)integer=integer+a[j]*pow(10,i);j++;i-;returninteger;/*保制字表,若查到返冋保留字类别码*/intlookupkey(char*token)char*keyword[44]={“prog怕m“,“varTand”,“integer”,”boolcn“,leal",“airay”,“begin”,”end“,“div”,“if“,”else”,”case“,“c
5、onst”,“do”,“downto”,”to”,“file",“for”,“function”,“goto”,“mod",“nil”,“not”,“ofV'or",,,packed,,/Fprocedure,”record”,”repe"tTthen“,”to“,“type“,“until",“while",”with“,“rewrite”,“reset”,“put”,“write”,“writein”,,'read,,,,,readln,,,,,in,'};for(intj=O;jv=43;j++)if(strcmp(token>
6、keyvvord
7、j])==0)return什3;return0;}/*输出二元式到文件和屏幕勺voidout(inti,intj,char*token,FILE*outf){fprintf(outf,n(%d,%d)%sM,i,j,token);printf(n(%d,%d)%s",i,j,token);/*报错函数*/report_error(void){printf(”**error(l)**°);}/*扫描程序旬voidscanner(FILE*fp,LinkListL,FILE*outf)//{charch;int
8、i,c,j;ch=fgetc(fp);for(j=0;j<20;j++)TOKEN[j]=W;if(int(ch)s4c-ar;^^^"^@^~5*旨進卡while(isdigsch))TOKEN=ncmi++chufgetc(fp
9、);一if(isa一pha(ch))(rcpoltlelroo八whiAisa-pha(ch))ohufgeAfp);)fscck(fp「1J)注釁-•歹朋淫之庞姿禅也J淫—之recrn二TOKEN三=A0