資源簡(jiǎn)介
遞歸下降語(yǔ)法分析程序的范例代碼...實(shí)驗(yàn)內(nèi)容及操作示范詳見實(shí)驗(yàn)指導(dǎo)書...

代碼片段和文件信息
#include?“stdio.h“
#include?“string.h“
#include?“stdlib.h“
#include?“conio.h“
/*關(guān)鍵字表每個(gè)關(guān)鍵字的種別碼為其下標(biāo)值+1*/
char?*keywords[33]=
{“and““array““begin““bool““call““case““char““constant““do““else““end““false“
“for““if““input““integer““not““of““or““output““procedure““program““read“
“real“?“repeat““set““then““to““true““until““var““while““write“??};
/*運(yùn)算符與界符表,每個(gè)符號(hào)的種別碼為其下標(biāo)值+38*/
char?*opts[]=
{“‘““(““)““*““+““““-““/““:““:=““;““<““<=““<>““=““>““>=““[““]“};
/*標(biāo)識(shí)符種別碼*/
#define?identsys?34??
/*整型常量種別碼*/
#define?intcontsys?35
/*實(shí)型常量種別碼*/
#define?realcontsys?36
/*字符常量種別碼*/
#define?charconsys?37
typedef?struct
{?char?name[20];
???int??kind;
???int??type;
}?idtype;?/*標(biāo)識(shí)符表的元素類型*/
typedef?struct
{?idtype?idwords[200];
??int?len;
}?idTtype;/*標(biāo)識(shí)符表類型*/
idTtype?idtable;?/*定義標(biāo)識(shí)符表*/
typedef?struct
{?int?intcons[200];
??int?len;
}?intconTtype;?/*整型常量表類型*/
intconTtype?intcontable;?/*定義整型常量表*/
/*實(shí)型常量表略*/
typedef?struct
{char?charcons[200];
?int?len;
}?charconTtype;?/*字符常量表類型*/
charconTtype?charcontable;?/*定義字符常量表*/
typedef?struct
{??int?kind;
???int?entry;
}tokentype;?/*Token字類型*/
tokentype?tokens[100];/*Token字表*/
void?wordcat(char?*wordchar?ch)?/*單詞連接函數(shù),將字符ch連接在word之后*/
{?char?*s=word;
???while(*s)?s++;
???*s=ch;*(++s)=‘\0‘;
}
void?clearword(char?*word)?/*將word清空*/
{?*word=‘\0‘;}
int?iskeyword(char?*word)?/*判斷單詞word是否為保留字,若是返回其種別碼,否則返回0*/
{??int?i;
???for(i=0;i<=30;i++)
??????if?(strcmp(wordkeywords[i])==0)?return?i+1;
???return?0;
}
int?operater(char?*word)?/*判斷單詞word是否為操作符,若是返回其種別碼,否則返回0*/
{?int?i;
???for(i=0;?i<21;?i++)
???if(strcmp(wordopts[i])==0)?return?i+38;
???return?0;
}
int?lookupid(char?*word)??/*查填標(biāo)識(shí)符表*/
{??int?i;
???for(i=0;i ???if(strcmp(wordidtable.idwords[i].name)==0)?return?i;
???strcpy(idtable.idwords[idtable.len].nameword);
???return?(idtable.len)++;
}
void?scanner(FILE?*fp1)
{?int?len=0;??/*Token字表初始為空*/
int?kindentry;
??char?word[30]=““chch1;
??idtable.len=0;
??intcontable.len=0;
??charcontable.len=0;
??ch=fgetc(fp1);
??while(ch!=EOF)
??{???while(ch==‘?‘||ch==‘\n‘)?ch=fgetc(fp1);/*濾除空格或回車符*/
??????if(ch>=‘a(chǎn)‘&&ch<=‘z‘||ch>=‘A‘&&ch<=‘Z‘)?/*處理標(biāo)識(shí)符與保留字*/
??????{??wordcat(wordch);?ch=fgetc(fp1);
?????while(ch>=‘a(chǎn)‘&&ch<=‘z‘||ch>=‘A‘&&ch<=‘Z‘||ch>=‘0‘&&ch<=‘9‘)
?????{??wordcat(wordch);?ch=fgetc(fp1);??}
?????kind=iskeyword(word);?
?????if(kind)?/*為保留字*/
???????{?tokens[len].kind=kind;?tokens[len].entry=0;len++;}
?????else{?entry=lookupid(word);?tokens[len].kind=identsys;
tokens[len].entry=entry;len++;}
?????clearword(word);
????????}
???????else?if(ch==‘:‘)?/*處理界限符“:“與操作符“:=“*/
???????{??wordcat(wordch);
??????????ch=fgetc(fp1);?
??????????if(ch!=‘=‘)?
{?kind=operater(word);?tokens[len].kind=kind;
tokens[len].entry=0;??len++;?}
??????????else?{?wordcat(wordch);?kind=operater(word);?tokens[len].kind=kind;
len++;tokens[len].entry=0;?ch=fgetc(fp1);?}
??????????clearword(word);
?
?屬性????????????大小?????日期????時(shí)間???名稱
-----------?---------??----------?-----??----
?????文件?????168010??2001-01-08?02:26??Recursive?descent?parser\Debug\text2.exe
?????文件?????206916??2001-01-08?02:26??Recursive?descent?parser\Debug\text2.ilk
?????文件??????34003??2001-01-08?02:26??Recursive?descent?parser\Debug\text2.obj
?????文件?????224512??2001-01-08?02:26??Recursive?descent?parser\Debug\text2.pch
?????文件?????459776??2001-01-08?02:26??Recursive?descent?parser\Debug\text2.pdb
?????文件??????33792??2001-01-08?02:26??Recursive?descent?parser\Debug\vc60.idb
?????文件??????53248??2001-01-08?02:26??Recursive?descent?parser\Debug\vc60.pdb
?????文件????????243??2001-01-08?02:21??Recursive?descent?parser\pro2.txt
?????文件???????9997??2001-01-08?02:22??Recursive?descent?parser\text2.cpp
?????文件???????3389??2001-01-08?02:26??Recursive?descent?parser\text2.dsp
?????文件????????535??2001-01-08?02:38??Recursive?descent?parser\text2.dsw
?????文件??????41984??2001-01-08?02:38??Recursive?descent?parser\text2.ncb
?????文件??????48640??2001-01-08?02:38??Recursive?descent?parser\text2.opt
?????文件???????1123??2001-01-08?02:26??Recursive?descent?parser\text2.plg
?????文件???????9997??2001-01-08?02:19??Recursive?descent?parser\text2.txt
?????目錄??????????0??2001-01-08?02:13??Recursive?descent?parser\Debug
?????目錄??????????0??2001-01-08?02:13??Recursive?descent?parser
-----------?---------??----------?-----??----
??????????????1296165????????????????????17
評(píng)論
共有 條評(píng)論