-
大小: 46KB文件類型: .rar金幣: 2下載: 1 次發布日期: 2021-07-12
- 語言: 其他
- 標簽:
資源簡介
詞法分析、分析預測表、LL(1)文法程序和流程圖

代碼片段和文件信息
#include
#include
#include
void??main(void);
int?reserver(char?*);
void?main(void)
{
?FILE?*input*output;
?char?ch*token=““;
?int?i=0isReserver=0;
?int?count=0;
?/*打開文件*/
?if((input=fopen(“input.txt““rt“))==NULL)
?{?printf(“file?open?in?readonly?modebut?an?error?generate!\n“);
??exit(0);
?}
?if((output=fopen(“output.txt““wt“))==NULL)
?{
??printf(“file?open?in?new_create?modebut?an?error?generate!\n“);
??exit(0);
?}
?
?fprintf(output“%4s\t\%8s\t?%-16s)\t%4s\n““位置““符號類型““識別符““長度“);
?ch=fgetc(input);
?while(ch!=EOF)
?{
??i=0;
??/*去掉前導空格換行符*/
??while((ch==‘?‘||ch==‘\n‘||ch==‘\t‘)&&ch!=EOF)
???ch=fgetc(input);
??/*字母開頭*/
??if(isalpha(ch))
??{
??while(isalpha(ch)||isdigit(ch))
???{
???token[i++]=ch;
???ch=fgetc(input);
???}
??token[i]=‘\0‘;
??isReserver=reserver(token);
??if(isReserver==1)
???{count++;fprintf(output“%4d\t(標識符\t\t%-16s)\t%4d\n“counttokeni);}
??else?if(isReserver==0)
???{count++;fprintf(output“%4d\t(保留字\t\t%-16s)\t%4d\n“counttokeni);}
??else?if(isReserver==2)
???{count++;fprintf(output“%4d\t(算符\t\t%-16s)\t%4d\n“counttokeni);}
??continue;???
??}
??/*識別數字*/
??else?if(isdigit(ch))
???{
????while(isdigit(ch))
????{
????token[i++]=ch;
????ch=fgetc(input);
????}
???/*出現非數字且為點時*/???
???if?(ch==‘.‘)
????{
????token[i++]=ch;?/*將點加入*/
????ch=fgetc(input);/*讀入下一個字符*/
????if?(isdigit(ch))
?????{
??????while(isdigit(ch))?/*是數字時,收入,并將加一*/
??????{
??????token[i++]=ch;
??????ch=fgetc(input);
??????}
??????/*如果是數字加點再加數字再出現字母時,就是錯誤*/
?????????????if(isalpha(ch))
??????{
???????while(isdigit(ch)||isalpha(ch)||ch==‘.‘)
???????{
????????token[i++]=ch;
????????ch=fgetc(input);
???????}
???????token[i]=‘\0‘;
???????count++;fprintf(output“%4d\t(標識錯誤\t%-16s)\t%4d\n“counttokeni);
???????continue;?
??????}
??????/*當出現結束符時,就收入為實數內*/
??????else
??????{
??????token[i]=‘\0‘;
??????count++;fprintf(output“%4d\t(實數\t\t%-16s)\t%4d\n“counttokeni);
??????continue;
??????}
?????}
????}
???
???/*如果是字符則判斷為標識錯誤*/
???else?if(isalpha(ch))
????{
?????while(isdigit(ch)||isalpha(ch)||ch==‘.‘)??????
?????{
?????token[i++]=ch;
?????ch=fgetc(input);
?????}
?????token[i]=‘\0‘;
?????count++;fprintf(output“%4d\t(標識錯誤\t%-16s)\t%4d\n“token);
?????continue;?
????}
???/*如果是單詞段結束符時,就判斷為常數*/??
???else
????{
?????token[i]=‘\0‘;
?????count++;fprintf(output“%4d\t(常數\t\t%-16s)\t%4d\n“counttokeni);
?????continue;
????}
???}?????
??else?if(ch==‘(‘)
???{
????token[i++]=ch;
????ch=fgetc(input);
????token[i]=‘\0‘;
????count++;fprintf(output“%4d\t(右括號運算符\t%-16s)\t%4d\n“counttokeni);
????continue;
???}
??else?if(ch==‘)‘)
???{
???token[i++]=ch;
???ch=fgetc(input);
???token[i]=‘\0‘;
???count++;fprintf(output“%4d\t(左括號運算符\t%-16s)\t%4d\n“counttokeni);
???continue;
???}
??else?if(ch==‘[‘)
???{
???token[i++]=ch;
???ch=fgetc(input);
???token[i]=‘\0‘;
???count++;fprintf(output“%4d\t(右中括號運算符\t%
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????828??2009-10-14?17:27??編譯原理0\analysis_table.txt
?????文件?????????56??2009-10-14?17:06??編譯原理0\chanshengshi.txt
?????文件?????175104??2007-07-08?19:27??編譯原理0\編譯原理實驗指導書.doc
?????文件???????6875??2009-09-26?13:17??編譯原理0\OUTPUT.TXT
?????文件???????9376??2009-09-26?13:16??編譯原理0\shyan1.c
?????文件????????458??2009-09-26?13:12??編譯原理0\input.txt
?????文件????????225??2009-09-26?13:05??編譯原理0\keyword.txt
?????文件??????30148??2009-10-14?17:32??編譯原理0\實驗2預測分析表.cpp
?????文件??????77312??2007-04-09?14:36??編譯原理0\詞法分析程序流程圖.doc
?????目錄??????????0??2006-03-09?22:20??編譯原理0
-----------?---------??----------?-----??----
???????????????300382????????????????????10
- 上一篇:基于深度學習的交通擁堵預測模型研究 *
- 下一篇:哈工大編譯原理實驗三次合一
評論
共有 條評論