资源描述:
《词法分析_编译原理_C语言版》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、#include"stdio.h"#inckidc"string.h"#include"stdlib.h"#defineal10#definenorw13#definenmax14FILE*fin;FILE*fout;charfname[al],fwname[al],a[al+1],id[al+1],sym[20];staticcharsword[11]={'+;'・'严畀,T,#,';'};staticcharssym[ll][al]={”plus:'minusTtimes”,“slashTlpareiTT'rparen”,“eqlTco
2、mma”,“period”,“neq打'semicolon");staticcharword[13][al]={"begin",Hcall",,'const",,'do","end","if","odd",',procedure,',,'read","then","var'"while”,“write"};//保留关键字staticcharwsymri31[all={,,beginsym,callsyrn,uconstsyrn,7,dosymn/lendsyrn'7,ifsyrn,Vroddsyrn,procsym","read
3、sym”,"thensym","varsym","whilesym","writesym"};〃关键字类型intcc,11,ex,num;charline[81J;charch;intgctch(){if(CC=ll)〃缓冲是否被读取完毕{if(feof(fin)){retum-1;}11=0;cc=();ch=*while(ch!=10){if(EOF==fscanf(fin;,%cn,&ch))linc[ll]=0;break;line[ll]=ch;11++;//linecount++;//printf("开始分析程序的第%(1ti
4、nu,linecount);//fprintf(fout,"Jf始分析程序的第%<1ft'An",linecount);}ch=line[cc];cc++;return0;}//voiderror(introwcountjntwordcount)//{//printf(u!第%(1彳亍第%d个位置数字越界",rowcount,wordcount);//fprintf(fout,"!第%4彳亍第%d个位置数字越界",rowcount,wordcount);//}interror(intn){switch(n){case1:printf
5、C*常数说明中的写成“:=”。“);break;case2:printf(”常数说明中的“二”后应是数字。“);break;case3:printf(”常数说明中的标识符后应是“=”。W);break;case4:printf(nconst,var,procedure后应为标识符。n);break;case5:printf(”漏掉了“,”或者“);break;case6:printf(“过程说明后的符号不正确(应是语句开始符,或过程定义符)0-);break;case7:printf("应是语句开始符。“);brea
6、k;case8:printf("程序体内语句部分的后跟符不止确。");break;case9:printfC*程序结尾丢了“。”。“);break;case10:printfC*语句之间漏了“;”。”);break;case11:printf(”标识符未说明ou);break;case12:printf(n赋值语句中,赋值号左部标识符属性应是变量。“);break;case13:printf(”赋值语句左部标识符后应是赋值号“:=”。“);break;case14:printf(Mcall后应为标识符。“);br
7、eak;case15:printf(ncall后标识符属性应为过程oH);break;case16:printf(M条件语句中丢了"then”。”);break;case17:printf("丢了"end"或“;”。");break;case18:printf(Mwhile型循环语句中丢了“do”An");break;case19:piintf(”语句后的符号不正确。”);break;case20:printfC*应为关系运算符oH);break;case21:printf(”表达式内标识符属性不能为过程。“);br
8、eak;case22:printfC*表达式中漏掉了右括号“)”。");break;case23:printf(n因子后的非法符号°”);break;case24:pri