簡單詞法分析器

來源:互聯網
上載者:User

#include <stdio.h><br />#include <string.h><br />int recognizechar(char c) //識別字元種類<br />{ if(c>='0'&&c<='9')<br /> return 0;<br /> else if((c>='A'&&c<='Z')||(c>='a'&&c<='z'))<br /> return 1;<br /> else if((c=='+')||(c=='-')||(c=='*')||(c=='/')||(c=='!')||(c=='=')||(c=='>')||(c=='<'))<br /> return 4;<br /> else if(c==' ')<br /> return 3;<br /> else if(c==','||c==';'||c=='{'||c=='}'||c=='('||c==')')<br /> return 5;<br />else return 10;</p><p>}<br />void judgestring(char *str) //? 識別保留字:if、int、for、while、do、return、break、continue;單詞種別碼為1。<br />{ char strInt[]={'i','n','t','/0','/0','/0','/0','/0','/0','/0'};<br /> char strIf[]={'i','f','/0','/0','/0','/0','/0','/0','/0','/0'};<br /> char strFor[]={'f','o','/r','/0','/0','/0','/0','/0','/0','/0'};<br /> char strWhile[]={'w','h','i','l','e','/0','/0','/0','/0','/0'};<br /> char strDo[]={'d','o','/0','/0','/0','/0','/0','/0','/0','/0'};<br /> char strReturn[]={'i','f','/0','/0','/0','/0','/0','/0','/0','/0'};<br /> char strBreak[]={'b','r','e','a','k','/0','/0','/0','/0','/0'};<br /> char strElse[]={'e','l','s','e','/0','/0','/0','/0','/0','/0'};<br /> if(!(strcmp(str,strInt)&&strcmp(str,strIf)&&strcmp(str,strFor)&&<br /> strcmp(str,strWhile)&&strcmp(str,strDo)&&strcmp(str,strReturn)&&<br /> strcmp(str,strBreak)&&strcmp(str,strElse)))<br /> {<br /> printf("(1, %s )",str);<br /> for(int i=0;i<10;i++)<br /> str[i]='/0';<br /> printf("/n");<br /> }<br /> else<br /> {<br /> printf("(2, %s )",str);<br /> for(int i=0;i<10;i++)<br /> str[i]='/0';<br /> printf("/n");<br /> }</p><p>}<br />void stringclean(char *str) //將字串清空<br />{<br /> for(int i=0;i<10;i++)<br /> str[i]='/0';<br />}</p><p>int main()<br />{<br /> FILE *fp;<br /> fp=fopen("C://c.txt","a+");//檔案地址在C盤<br />int formword=0;<br /> int stringword=0;<br /> int spaceword=0;<br /> int numberword=0;<br /> char nextline='/n';<br />char caculate[10]={'/0'};<br /> char str[10]={'/0'};<br /> while(!feof(fp))<br /> {<br /> //putchar(ch);<br /> char ch=fgetc(fp);</p><p> char str1[]={ch,'/0'};<br /> char temp[]={'/0','/0','/0','/0','/0','/0','/0','/0','/0','/0'};<br /> int i=recognizechar(ch);<br />if((i!=4)&&strcmp(caculate,temp))<br />{<br /> printf("(4, %s )",caculate);<br /> stringclean(caculate);<br /> numberword=0;<br /> spaceword=0;<br /> printf("%c",nextline);<br />}<br /> if(i==0)<br /> {<br /> if(!strcmp(str,temp))<br /> {<br /> strcat(str,str1);<br /> numberword++;<br /> }<br /> else strcat(str,str1);<br /> spaceword=0;</p><p> }<br /> else if(i==1)<br /> {<br /> numberword=0;<br /> strcat(str,str1);<br /> spaceword=0;</p><p> }<br /> else if(i==4)<br /> {<br /> if(strcmp(str,temp))<br /> { if(numberword)<br /> {<br /> printf("(3, %s )",str);<br /> stringclean(str);<br /> numberword=0;<br /> printf("%c",nextline);</p><p> }<br /> else judgestring(str);<br /> }<br /> numberword=0;<br /> spaceword=0;<br />strcat(caculate,str1);<br /> }<br /> else if(i==3)<br /> {<br /> if(strcmp(str,temp))<br /> {if(numberword)<br /> {<br /> printf("(3, %s )",str);<br /> stringclean(str);<br /> printf("%c",nextline);<br /> }<br /> else<br /> {<br /> judgestring(str);<br /> }<br /> }</p><p> }<br /> else if(i==5)<br /> {<br /> if(strcmp(str,temp))<br /> { if(numberword)<br /> {<br /> printf("(3, %s )",str);<br /> stringclean(str);<br /> numberword=0;<br /> printf("%c",nextline);<br /> }<br /> else judgestring(str);<br /> }<br /> printf("(5, %c )",ch);<br /> printf("%c",nextline);</p><p> }<br /> }<br /> return 0;<br />}<br />

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.