语法分析器错误代码求教!
编了一个语法分析器,但是运行时说错误了。找不出在哪里,请高手们帮帮忙。#include<stdio.h>
#include<iostream.h>
#include<string.h>
#define NULL 0
FILE *p;
char b=' ';
void zhuprogram();
void son_function();//子函数
//void error();//报错
void function();//函数
int DataType();
void Biaozhifu();//标识符
void xingcan();//形参
void function_body();//函数体
void definate_val();//定义变量
void operate_centence();//执行语句
void centence();//语句
void fuzhi();//赋值语句
void function_diaoyong();
void biaodashi();
void item();//项
void itemrec();
void factrec();
void factor();
void shican();
void condition();
int guanxi();
void condition_centence();
void while_circle();
void function_centence();
void fuhecentence();
void empty();
void read_centence();
void write_centence();
int row=1;
char *sym;
char str1[]={"void"};//字符串
char str2[]={"int"};
void Getsymbol(char s[])
{
int i=0;
char a;
a=b;
if (b!=' '&&b!=9&&b!='\n')
s[i++]=b;
while(!feof(p))
{
a=getc(p);
while(a=' '||a==9)//制表符为9
a=getc(p);
if ((a>='a'&&a<='z')||(a>='A'&&a<='Z'))
{
s[i++]=a;
a=getc(p);
while((a>='a'&&a<='z')||(a>='A'&&a<='Z')||(a>='0'&&a<='9')||a=='-')
{
a=getc(p);
s[i++]=a;
}
s[i]='\0';
b=a;
break;
}
else if (a>='0'&&a<='9')
{
s[i++]=a;
a=getc(p);
while (a>='0'&&a<='9')
{
s[i++]=a;
a=getc(p);
}
s[i]='\0';
b=a;
break;
}
else if(a=='>'||a=='<'||a=='='||a=='!')
{
s[i++]=a;
a=getc(p);
while(a=='=')
{
s[i++]=a;
a=getc(p);
}
s[i]='\0';
b=a;
break;
}
else if (a=='+'||a=='-'||a=='*'||a=='/'||a=='('||a==')'||a=='{'||a=='}'||a==';'||a==','||a=='"')
{
s[i++]=a;
a=getc(p);
s[i]='\0';
b=a;
break;
}
else if (a=='%')
{
s[i++]=a;
a=getc(p);
while(a=='d')
{
s[i++]=a;
a=getc(p);
}
s[i]=a;
b=a;
break;
}
else if (a=='\n')
{
row++;
while(a=='\n')
{
row++;
a=getc(p);
}
b=a;
break;
}
else break;
}
}
void main()
{
p=fopen("a.txt","r");
if(p==NULL)
printf("cannot open file\n");
sym=new char[100];
zhuprogram();
Getsymbol(sym);
delete sym;
fclose (p);
}
void zhuprogram()
{
son_function();//可选
zhuprogram();
}
void son_function()//子函数
{
function();
}
void function()//函数
{
DataType();
Biaozhifu();
if(!strcmp(sym,"("))
{
Getsymbol(sym);
xingcan();//可选
if(!strcmp(sym,")"))
{
Getsymbol(sym);
if(!strcmp(sym,"{"))
{
Getsymbol(sym);
function_body();
if(!strcmp(sym,"}"))
{
Getsymbol(sym);
}
else cout<<" } "<<endl;
}
else cout<<" { ";
}
else cout<<" ) ";
}
else cout<<" ( ";
}
int DataType()
{
if(!strcmp(sym,str1)||strcmp(sym,str2))//字符串表示
return 1;
else
{
cout<<"数据类型错误"<<endl;
return 0;
}
}
void Biaozhifu()//标识符
{
char zimu[52][2]={"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p",
"q","r","s","r","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I",
"J","K","L","M","N","O","P","Q","R","S","R","U","V","W","X","Y","Z"};
// char shuzi[10]={"0","1","2","3","4","5","6","7","8","9"};
for(int i=0;i<52;i++)
{
if(!strcmp(sym,zimu[i]))
{
Getsymbol(sym);
Biaozhifu();
}
else cout<<" 定义错误"<<endl;
}
}
void xingcan()//形参
{
if(!strcmp(sym,str2))
{
Getsymbol(sym);
Biaozhifu();
}
else cout<<" 定义错误"<<endl;
}
void function_body()//函数体
{
definate_val();//定义变量
operate_centence();//执行语句
}
void definate_val()//定义变量
{
if(!strcmp(sym,str2))
{
Biaozhifu();
}
else cout<<" 定义错误"<<endl;
}
void operate_centence()//执行语句
{
centence();
}
void centence()//语句
{
if ((!strcmp(sym,str1))&&(!strcmp(sym,str2)))
fuzhi();
else if (!(!strcmp(sym,"if")))
condition_centence();
else if(!(!strcmp(sym,"while")))
while_circle();
else if (!(!strcmp(sym,"{")))
{
Getsymbol(sym);
if (!(!strcmp(sym,"}")))
empty();
else fuhecentence();
}
else if (!(!strcmp(sym,"scanf")))
read_centence();
else if(!(!strcmp(sym,"printf")));
else
function_diaoyong();
Getsymbol(sym);
}
void fuzhi()//赋值语句
{
// Biaozhifu()=biaodashi;
}
void biaodashi()
{
item();
itemrec();
}
void item()//项
{
factor();
factrec();
}
void itemrec()
{
if(!strcmp(sym,"+")||strcmp(sym,"-"))
Getsymbol(sym);
item();
itemrec();
}
void factrec()
{
if(!strcmp(sym,"*")||strcmp(sym,"/"))
Getsymbol(sym);
factor();
factrec();
}
void factor()
{
if(!(*sym>='A'&&*sym<='Z') && !(*sym>='a'&&*sym<='z')
&& !(*sym>='0'&&*sym<='9') && strcmp(sym,"("))
cout<<"非法"<<endl;
else
if(!strcmp(sym,"("))
{
Getsymbol(sym);
biaodashi();
if(!strcmp(sym,")"))
cout<<" ) "<<endl;
}
Getsymbol(sym);
}
void condition_centence()//条件语句
{
if(!strcmp(sym,"if"))
{
Getsymbol(sym);
if(!strcmp(sym,"("))
{
Getsymbol(sym);
condition();
if(!strcmp(sym,")"))
{
Getsymbol(sym);
centence();
if(!strcmp(sym,";"))
{
Getsymbol(sym);
if(!strcmp(sym,"else"))
{
Getsymbol(sym);
centence();
}
}
else cout<<" ; "<<endl;
}
else cout<<" ) "<<endl;
}
else cout<<" ( "<<endl;
}
}
void condition()//条件
{
biaodashi();
guanxi();
biaodashi();
}
int guanxi()
{
if(sym==">")
return 1;
else if(sym=="<")
return 1;
else if(sym==">=")
return 1;
else if(sym=="<=")
return 1;
else if(sym=="==")
return 1;
else if(sym=="!=")
return 1;
else
return 0;
}
void while_circle()//当循环语句
{
if(sym=="while")
{
Getsymbol(sym);
if(sym=="(")
{
Getsymbol(sym);
condition();
if(sym==")")
{
Getsymbol(sym);
centence();
}
else cout<<" ) "<<endl;
}
else cout<<" ( "<<endl;
}
}
void function_diaoyong()//函数调用语句
{
Biaozhifu();
if(sym=="(")
{
Getsymbol(sym);
shican();
if(sym==")")
Getsymbol(sym);
else cout<<" ) "<<endl;
}
else cout<<" ( "<<endl;
}
void shican() //实参
{
biaodashi();
}
void fuhecentence() //复合语句,没有最外边的else表示可选
{
if(sym=="{")
{
Getsymbol(sym);
centence();
if(sym=="}")
Getsymbol(sym);
else cout<<" } "<<endl;
}
}
void empty()//空语句
{
}
void read_centence()//读语句
{
if(strcmp(sym,"scanf")==0)
{
Getsymbol(sym);
if(strcmp(sym,"(")==0)
{
Getsymbol(sym);
if(strcmp(sym,"\" ")==0)
{
Getsymbol(sym);
if(strcmp(sym,"%d")==0)
{
Getsymbol(sym);
if(strcmp(sym," \" ")==0)
{
Getsymbol(sym);
if(strcmp(sym,",")==0)
{
Getsymbol(sym);
if(strcmp(sym,"&")==0)
{
Getsymbol(sym);
Biaozhifu();
if(strcmp(sym,")")==0)
Getsymbol(sym);
else cout<<"缺少) "<<endl;
}
else cout<<"错误 "<<endl;
}
else cout<<"格式错误,缺少\",\" "<<endl;
}
else cout<<"格式错误 "<<endl;
}
else cout<<"缺少数据类型"<<endl;
}
else cout<<"格式错误 "<<endl;
}
else cout<<"缺少( "<<endl;
}
}
void write_centence()//写语句
{
if(strcmp(sym,"printf")==0)
{
Getsymbol(sym);
if(strcmp(sym,"(")==0)
{
Getsymbol(sym);
if(strcmp(sym,"\" ")==0)
{
Getsymbol(sym);
if(strcmp(sym,"%d")==0)
{
Getsymbol(sym);
if(strcmp(sym," \" ")==0)
{
Getsymbol(sym);
if(strcmp(sym,",")==0)
{
Getsymbol(sym);
biaodashi();
if(strcmp(sym,")")==0)
Getsymbol(sym);
else cout<<"缺少) "<<endl;
}
else cout<<"格式错误,缺少\",\" "<<endl;
}
else cout<<"格式错误 "<<endl;
}
else cout<<"缺少数据类型"<<endl;
}
else cout<<"格式错误 "<<endl;
}
else cout<<"缺少) "<<endl;
}
}