📄 pl0语法分析器(递归子程序法).txt
字号:
}
if(token=='k')//条件语句
{
token=word();
T();
if(token!='o')
{
error(16);//条件语句缺少"then"报错不作处理
}
if(token=='e'||token=='k'||token=='q'||token=='g'||token=='n'||token=='r'||token=='f'||token!='.'||token!=';'||token!='j')
Y();//这样处理是因为可能刚刚已经取到了语句的开始符号
else
{
token=word();
Y();
}
}
if(token=='q')//当型循环
{
token=word();//putchar('y');
T();//putchar('y');
if(token!='i')
{
error(18);//putchar('y');//当型循环语句缺少"do"报错不作处理
//putchar(token);
if(token=='e'||token=='k'||token=='q'||token=='g'||token=='n'||token=='r'||token=='f'||token!='.'||token!=';'||token!='j')
{
//putchar(token);
Y();//这样处理是因为可能刚刚已经取到了语句的开始符号
}
else
{
//putchar(token);
token=word();//putchar('y');
Y();//putchar('y');
}
}
else
{
token=word();
Y();
}
}
if(token=='g')//过程调用语句
{
ytemprow=0;
ytemprow=row;
token=word();
if(token!='e')
{
error(14);//过程调用语句后面应该是标志符
while(ytemprow==row)
token=word();//除去同一行的多余字符
Y();//token=word();
}
else
{
compare(tempword,'m');
token=word();//putchar(token);putchar('y'); //在此还需要判断标志符是否具有过程属性,查表
}
}
if(token=='n')//读语句
{
ytemprow=0;
ytemprow=row;
token=word(); //putchar('x');
if(token!='(')
{
error(34);//读语句缺少左括号,判断是否是标志符
while(ytemprow==row)
token=word();
//token=word();
ytemprow=0;//putchar(token);
Y();//调用语句处理函数是因为这是读语句出错也就是在语句处理中,它的前面的函数S,F,B,G均不会出现在读语句之后
}
else
{
token=word();
if(token!='e')
{
error(36);//read语句括号中缺少标志符,//
while(ytemprow==row)
token=word();
Y();
ytemprow=0;//在此检查后跟符是否为括号,
}
else
{ //read语句括号中的标志符不是变量
compare(tempword,'p');//检查属性
token=word();
if(token!=')')//判断是否是同行
{
while(token!=')'&&ytemprow==row)//检查行是否结束
{
//contr++;//用作控制循环,改成判断是否是同行
if(token!=',')
{
error(5);//变量之间应该用逗号分隔
while(ytemprow==row)
token=word();
Y();
ytemprow=0;//putchar('x');
}
else
{
token=word();
if(token!='e')
{
error(36); //判断同行是否是结束,是就报错结束符是右括号
while(ytemprow==row)
token=word();
Y();
ytemprow=0;//putchar('x');
}
else
{
compare(tempword,'p');
token=word();
}
//if(contr>20)//缺少右括号//改成判断是否是同行
//{
// error(1);
// break;
} //}
}//while
if(token!=')')
{
error(22);
token=word();
}
else
{
token=word();
}
}
else
token=word();
}//else
}
}
if(token=='r')//写语句//处理方式与读语句一致
{
ytemprow=0;
ytemprow=row;
token=word();
if(token!='(')
{
error(39);
while(ytemprow==row)
token=word();
//token=word();
ytemprow=0;
Y();
}
else
{
token=word();//int contr=0;
if(token!='e'&&token!='d'&&token!='(')
{
error(38);
while(ytemprow==row)
token=word();
//token=word();
ytemprow=0;Y();
}
else
{
//compare(tempword,'p');
A();
//token=word();
if(token!=')')
{
while(token!=')'&&ytemprow==row)
{
if(token!=',')
{
error(5);
while(ytemprow==row)
token=word();
ytemprow=0;Y();
}
else
{
token=word();
if(token!='e'&&token!='d'&&token!='(')
{
error(38);
while(ytemprow==row)
token=word();
//putchar(token);
ytemprow=0;Y();
}
else
{
//compare(tempword,'p');
A();
//token=word();
}
}
} //while
if(token!=')')
{
error(22);//缺少右括号
token=word();
}
else
{
token=word();
}
}
else
{
token=word();//结束后再取下一个字符
}
}
}
}
if(token=='f')//复合语句
{
token=word();//putchar('y');
Y();
if(token!='j'&&token!=';')//判断是正确还是错误
{
error(17);//putchar('x');
}
else
{
while(token!='j')
{
if(token!=';')
{
//putchar('x');
error(10);//putchar('y');putchar(token);putchar('x');//缺少分号
if(token=='e'||token=='k'||token=='q'||token=='g'||token=='n'||token=='r'||token=='f'||token!='.'||token!=';'||token!='j')
Y();
else
{
token=word();
if(token=='e'||token=='k'||token=='q'||token=='g'||token=='n'||token=='r'||token=='f'||token!='.'||token!=';'||token!='j')
Y();
}
}
token=word();
Y();
}//while
if(token!='j')
{
error(17);//putchar('x');//缺少end
}
else
{
token=word();
}
}
}
if(token=='.')
{
if(word()!=NULL)
{
error(46);//程序意外结束,无法继续编译
exit(0);
}
}
}
}
void C()//过程首部
{
int gtemprow=0;
gtemprow=row;
if(token!='m')
{
error(1);//过程说明符应该为procedure
while(gtemprow==row)
token=word();
F();
}
else
{
token=word();//putchar('y');
if(token!='e')
{
error(4);//应该接标志符
while(gtemprow==row)
token=word();
F();
}
else
{
if(find(tempword))
{
error(43);//标志符已定义
//putchar('x');
}
else
{
filltable(tempword,'m');//putchar('c');
}
token=word();
if(token!=';')
{
error(17);//结束符应该为分号
}
else
{
token=word();
}
}
}
}
void A()//表达式
{
int atemprow=0;
atemprow=row;
if(token=='+'||token=='-')
{
token=word();//如果开始字符是‘+’或‘-’则取下一个字符
}
//putchar(token);
N();//putchar(token);
while(atemprow==row&&(token=='+'||token=='-'))//并且是同行
{
token=word();
N();
}
}
void N()//项
{
int ntemprow=0;
ntemprow=row;
if(token!='e'&&token!='d'&&token!='(')
{
error(7);
while(ntemprow==row&&token!='+'&&token!='-'&&token!=','&&token!='i'&&token!='o')
token=word();//将整个项去掉
}
else
{
I();
while(token=='*'||token=='/')
{
token=word();
I();
}
}
}
void I()//因子
{
int itemprow=0;
itemprow=row;
if(token!='e'&&token!='d'&&token!='(')
{
error(7);//开始符出错
while(itemprow==row&&token!='+'&&token!='-'&&token!=','&&token!='i'&&token!='o'&&token!='*'&&token!='/')
token=word();
}
else
{
if(token=='e')
{
compare(tempword,'p');
token=word();
}
if(token=='d')
token=word();
if(token=='(')
{
token=word();
A();
if(token!=')')
{
error(22);//缺少右括号
}
else
{
token=word();
}
}
}
}
void X()//关系运算符
{
if(token!='#'&&token!='<'&&token!='>'&&token!='b'&&token!='c')
{
error(40);//关系运算符出错
if(token!='+'&&token!='-'&&token!='e'&&token!='d'&&token!='(')//防止已经取得了下一个标志符的开始符
token=word();
}
else
{
token=word();
}
}
void T()//条件
{
int ttemprow=0;
ttemprow=row;
if(token!='+'&&token!='-'&&token!='e'&&token!='d'&&token!='('&&token!='l')
{
error(41);//条件开始子集错误
while(token!='i'&&token!='o')
{
token=word();
if(ttemprow!=row)
break;
}
}
else
{
if(token=='l')
{
A();
}
else
{//putchar('y');putchar(token);
A();//putchar('y');
X();//putchar('y');putchar(token);
A();
}
}
}
void main()
{
op=fopen("b.c","r");
token=word();
S();
fclose(op);
getchar();
exit(0);
}
/*程序中有许多不足之处,请各位指教*/
/*程序在VC6.0下编译通过*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -