c语言的词法分析程序

整理文档很辛苦,赏杯茶钱您下走!

免费阅读已结束,点击下载阅读编辑剩下 ...

阅读已结束,您可以下载文档离线阅读编辑

资源描述

#includestdio.h#includestdlib.h#includestring.h#define_KEY_WORD_ENDwaitingfouyourexpanding/*定义关键字的结束标志*/typedefstruct{inttypenum;/*种别码*/char*word;}WORD;charinput[255];/*源程序缓字符冲区*/chartoken[255]=;/*单词缓冲区*/intp_input;/*源程序字符指针*/intp_token;/*单词缓冲区指针*/charch;char*rwtab[]={begin,if,then,while,do,end,_KEY_WORD_END};WORD*scaner();/*词法扫描函数,获得一个单词*/voidmain(){intover=1;intcount=0;WORD*oneword=newWORD;printf(EnterYourwords(endwith#):);scanf(%[^#]s,input);/*输入源程序字符串到缓冲区,以#结束*/p_input=0;printf(词法分析结果是:\n\n);while(over1000&&over!=-1){oneword=scaner();if(oneword-word==OVER)break;elseif(oneword-typenum1000)printf((%d,%s),oneword-typenum,oneword-word);over=oneword-typenum;count++;if(count%6==0)printf(\n);//每六行输出}}charm_getch()/*从输入源读一个字符到CH中*/{ch=input[p_input];p_input=p_input+1;returnch;}voidgetbc()/*去掉空白字符*/{while(ch==''||ch==10){ch=input[p_input];p_input=p_input+1;}}voidconcat()/*拼接单词*/{token[p_token]=ch;p_token=p_token+1;token[p_token]='\0';}intletter()/*判断是否是字母*/{if(ch='a'&&ch='z'||ch='A'&&ch='Z')return1;elsereturn0;}intdigit()/*判断是否是数字*/{if(ch='0'&&ch='9')return1;elsereturn0;}intreserve()/*检索关键字表格*/{inti=0;while(strcmp(rwtab[i],_KEY_WORD_END)){if(!strcmp(rwtab[i],token)){returni+1;}i=i+1;}return10;}voidretract()/*回退一个字符*/{p_input=p_input-1;}char*dtb(){returnNULL;}WORD*scaner()/*词法扫描程序*/{WORD*myword=newWORD;myword-typenum=10;myword-word=;p_token=0;m_getch();getbc();if(letter()){while(letter()||digit()){concat();m_getch();}retract();myword-typenum=reserve();myword-word=token;returnmyword;}elseif(digit()){while(digit()){concat();m_getch();}retract();myword-typenum=11;myword-word=token;returnmyword;}elseswitch(ch){case'=':m_getch();if(ch=='='){myword-typenum=29;myword-word===;returnmyword;}retract();myword-typenum=25;myword-word==;returnmyword;break;case'+':myword-typenum=13;myword-word=+;returnmyword;break;case'-':myword-typenum=14;myword-word=-;returnmyword;break;case'*':myword-typenum=15;myword-word=*;returnmyword;break;case'/':myword-typenum=16;myword-word=/;returnmyword;break;case'(':myword-typenum=27;myword-word=(;returnmyword;break;case')':myword-typenum=28;myword-word=);returnmyword;break;case'[':myword-typenum=30;myword-word=[;returnmyword;break;case']':myword-typenum=31;myword-word=];returnmyword;break;case'{':myword-typenum=32;myword-word={;returnmyword;break;case'}':myword-typenum=33;myword-word=};returnmyword;break;case',':myword-typenum=34;myword-word=,;returnmyword;break;case':':if(input[p_input]=='='){myword-typenum=18;myword-word=:=;returnmyword;}elsemyword-typenum=17;myword-word=:;returnmyword;break;case';':myword-typenum=26;myword-word=;;returnmyword;break;case'':m_getch();if(ch=='='){myword-typenum=24;myword-word==;returnmyword;}retract();myword-typenum=23;myword-word=;returnmyword;break;case'':m_getch();if(ch=='='){myword-typenum=22;myword-word==;returnmyword;}retract();myword-typenum=20;myword-word=;returnmyword;break;case'!':m_getch();if(ch=='='){myword-typenum=40;myword-word=!=;returnmyword;}retract();myword-typenum=-1;myword-word=ERROR;returnmyword;break;case'\0':myword-typenum=100;myword-word=OVER;returnmyword;break;default:myword-typenum=-1;myword-word=ERROR;returnmyword;}}

1 / 6
下载文档,编辑使用

©2015-2020 m.777doc.com 三七文档.

备案号:鲁ICP备2024069028号-1 客服联系 QQ:2149211541

×
保存成功