📄 语法分析.c
字号:
#include "lex.yy.c"
void begindo();
int fcx();//分程序
int blsm();//变量说明
int blsmb();//变量说明表
int yjb();//语句表
int initstal();//下个单词
int lx();//类型
int yj();//语句
int fzyj();//赋值语句
int tjyj(); //条件语句
int xhyj();//循环语句
int fhyj();//复合语句
int ssbds(); //算术表达式
int blb(); ///变量表
int xiang(); //项
int gxbds(); //关系表达式
int ys(); //因式
void finderror();
void showerror(char *);
char * error[50];
///构造一个堆栈
typedef struct {
int * base;
int * top;
int size;
}stack;
stack next; ///定义一个输入序列堆栈
int printstack(stack s){
int e;
while(s.top!=s.base)
{ e=* s.base++;
printf("%d ",e);}
return 1;
}
int pop(stack s){
int e;
if(next.top==next.base)
return 0;
e=*next.base++;
printf("这里读取了一个单词 %d \n",e);
return e;
}
///堆栈定义结束
void main(int args,char * * argv)
{
int c=0;
if(args>=2)
{
if((yyin=fopen(argv[1],"r"))==NULL)
{
printf("can't open file %s",argv[1]);
exit(0);
}
}
next.base=(int *)malloc(1000*sizeof(int));
next.top=next.base;
initstal();
printstack(next);
printf("\n");
begindo();
return;
}
void begindo(){ //主程序开始
if(pop(next)==PROGRAM) //1
if(pop(next)==NAME) //2
if(pop(next)==SEMI) //3
if(fcx())
printf("successful................\n");
else
showerror("分程序错误");
else
showerror("开始时少一个");
else
showerror("少程序名");
else
showerror("没有以PROGRAM开头");
}
int fcx(){ ///分程序开始
if(blsm())
{
printf("分析到变量说明正确\n");
if(pop(next)==1)
{
printf("分析到BEGIN正确\n");
if(yjb())
{
printf("分析到语句表正确\n");
if(pop(next)==END)
return 1;
showerror("缺少end标志");
}
else
showerror("语句表出错");
next.base--;
}
}
else
showerror("变量说明出错");
return 0;
}
int blsm(){ //变量说明
if(pop(next)==VAR)
{
if(blsmb())
{
if(pop(next)==SEMI)
return 1;
next.base--;
showerror("缺少定义结束标志");
}
else
showerror("变量说明表出错");
next.base--;
}
else
showerror("缺少VAR标志");
return 0;
}
int blsmb(){ //变量说明表
if(blb())
{
if(pop(next)==COLON)
{
printf("分析到COLON正确\n");
if(lx())
{
printf("分析到类型正确\n");
if(pop(next)==SEMI)
{
printf("分析到SEMI正确\n");
if(blsmb())
return 1;
next.base--;
return 1;
}
return 1;
}
next.base--;
}
}
else
showerror("变量表出错");
return 0;
}
int blb(){ ///变量表
if(pop(next)==NAME)
{
if(pop(next)==COMMA)
if(blb())
return 1;
else
return 0;
else
{
next.base--;
return 1;
}
}
else
showerror("缺少变量名");
next.base--;
return 0;
}
int lx(){ //类型
int c;
if((c=pop(next))==INTEGER)
return 1;
if(c==REAL)
return 1;
next.base--;
return 0;
}
int yjb(){ //语句表
if(yj())
{
printf("分析到语句222222判断正确\n");
if(pop(next)==SEMI)
{
if(yjb())
return 1;
else
return 0;
}
else
{
next.base--;
return 1;
}
return 1;
}
else
printf("分析到语句出错\n");
return 0;
}
int yj(){ //语句
if(fzyj())
{
printf("分析到语句判断正确\n");
return 1;
}
printf("分析到语句ok....判断正确\n");
if(tjyj())
return 1;
if(xhyj())
return 1;
if(fhyj())
return 1;
return 0;
}
int fzyj(){ //赋值语句
if(pop(next)==NAME)
{
printf("分析到赋值语句正确\n");
if(pop(next)==GIVE)
{
printf("分析到:=正确\n");
if(ssbds())
{
printf("分析到算术表达式正确\n");
return 1;
}
next.base--;
}
}
next.base--;
return 0;
}
int tjyj(){ //条件语句
if(pop(next)==IF)
if(gxbds())
{
if(pop(next)==THEN)
if(yj())
{
if(pop(next)==ELSE)
if(yj())
return 1;
next.base--;
}
next.base--;
}
next.base--;
return 0;
}
int xhyj(){///循环语句
if(pop(next)==WHILE)
if(gxbds())
{
if(pop(next)==DO)
if(yj())
return 1;
next.base--;
}
next.base--;
return 0;
}
int fhyj(){//复合语句
if(pop(next)==1)
if(yjb())
{
if(pop(next)==END)
return 1;
next.base--;
}
next.base--;
return 0;
}
int ssbds(){ //算术表达式
if(xiang())
return 1;
if(ssbds())
{
int k=pop(next);
if(k==PLUS||k==MINUS)
if(xiang())
return 1;
next.base--;
}
return 0;
}
int xiang(){ //项
if(ys())
return 1;
if(xiang())
{
int k=pop(next);
if((k==STAR)||(k==DIVOP))
if(ys())
return 1;
next.base--;
}
return 0;
}
int ys(){ //因式
if(pop(next)==NAME)
return 1;
next.base--;
if(pop(next)==ICON)
return 1;
next.base--;
if(ssbds())
return 1;
return 0;
}
int gxbds(){ //关系表达式
if(ssbds())
{
int k=pop(next);
if(k>=15&&k<=20)
if(ssbds())
return 1;
next.base--;
}
return 0;
}
int initstal(){
int c;
while(c=yylex()){
if(c<200)
{
*next.top++=c;
printf("%d ",c);
}
}
printf("\n");
return 1;
}
void showerror(char * error){
next.base--;
printf(" %s %d \n",error,pop(next));
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -