資源簡介
用C語言實現(xiàn)語法分析,有助于編譯器的生成。

代碼片段和文件信息
#include“stdio.h“
#include“stdlib.h“
#include“string.h“
struct?tablestruct
{
char?name[10];
char?kind[10];
????char?val[14];
int?level;
int?adr;
int?size;
};
char?*getsym(FILE*);
void?constdeclaration(tablestruct*int*FILE*FILE*char*);
void?vardeclaration(tablestruct*int*int*int*FILE*FILE*char*int*int);
void?statement(FILE*FILE*char*);
void?expression(FILE*FILE*char*);
void?term(FILE*FILE*char*);
void?factor(FILE*FILE*char*);
void?condition(FILE*FILE*char*);
void?main()
{???
tablestruct?table[100];
????FILE?*fp1*fp2;
????char?filename1[10]filename2[10];
char?sym[10];
int?j=0lev=0dx=3;
int?s=0procx=0;
printf(“請輸入pl/0單詞流文件名:\t“);
scanf(“%s“filename1);
getchar();
printf(“\n“);
??? printf(“請輸入語法分析結果要存儲到的文件名:\t“);
scanf(“%s“filename2);
getchar();
printf(“\n\n“);
printf(“語法分析結果為:“);
printf(“\n“);
????//*打開文件,分別以寫方式和讀方式*//
????fp2=fopen(filename2“w“);
if((fp1=fopen(filename1“r“))==NULL)
{
printf(“Cannot?open?file!“);
exit(0);
}
strcpy(symgetsym(fp1));????????????????????????????//獲取一個單詞
while(!feof(fp1))
{
if(!strcmp(sym“constsym“))????????????????????????????????????//常量聲明
{
strcpy(symgetsym(fp1));
do{
constdeclaration(table&jfp1fp2sym);
while(!strcmp(sym“comma“))
{
????strcpy(symgetsym(fp1));
??? constdeclaration(table&jfp1fp2sym);
}
if(!strcmp(sym“semicolon“))
{
????????strcpy(symgetsym(fp1));
}
else?
{
printf(“漏掉了逗號或分號!“);
}
}while(!strcmp(sym“ident“));
}
else?if(!strcmp(sym“procsym“))??????????????????????????????//程序體
{
????strcpy(symgetsym(fp1));
if(!strcmp(sym“ident“))
{
????????strcpy(symgetsym(fp1));
strcpy(table[j].namesym);
???????? strcpy(table[j].kind“procedure“);
???????? table[j].level=lev;
????????????????dx=3;
procx=j;
????????????????table[procx].size=s;
????strcpy(symgetsym(fp1));
}
else
{
printf(“procedure后應該是標識符“);
}
if(!strcmp(sym“semicolon“))
{
????strcpy(symgetsym(fp1));
}
else
{
printf(“漏掉了分號“);
}
???????? j++;
lev++;
}
????????else?if(!strcmp(sym“varsym“))????????????????????????????????????????//變量聲明
{
????strcpy(symgetsym(fp1));
do{
vardeclaration(table&j&lev&dxfp1fp2sym&sprocx);
while(!strcmp(sym“comma“))
{
???? ????strcpy(symgetsym(fp1));
?? vardeclaration(table&j&lev?&dxfp1fp2sym&sprocx);
}
if(!strcmp(sym“semicolon“))
{
????????strcpy(symgetsym(fp1));
}
else?
{
printf(“漏掉了逗號或分號!“);
}
}while(!strcmp(sym“ident“));
}
else?if(!strcmp(sym“ident“)||!strcmp(sym“callsym“)||!strcmp(sym“beginsym“)??????//語句處理
????||!strcmp(sym“ifsym“)||!strcmp(sym“whilesym“)||!strcmp(sym“readsym“)
||!strcmp(sym“writesym“))?
{
?????statement(fp1fp2sym);
}
else
{
if(
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件???????9850??2011-06-03?11:09??語法分析.cpp
-----------?---------??----------?-----??----
?????????????????9850????????????????????1
- 上一篇:編譯原理--語法分析 實驗 C++版
- 下一篇:Chan氏TDOA
評論
共有 條評論