“编译原理”课程设计报告 CPR-1206010231
//扫描,对字符串进行逐一扫描 {
for(n=0;n<9;n++) //先将令牌清空
token[n]=NULL; ch=chter[p++]; while(ch==' ')
//当扫描到空格时,空格有分隔两个单词的作用
{
ch=chter[p]; p++; }
if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z')) //当扫描到单词首字符为字母时
{
m=0;
while((ch>='0'&&ch<='9')||(ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
//首字符后的字符为字母后数字
{
token[m++]=ch; ch=chter[p++]; }
token[m++]='\\0'; p--; syn=10;
for(n=0;n<6;n++)
//因为扫描到的ID有可能有关键字的情况,所以要进行关键字判定
{
if(strcmp(token,keyword[n])==0) {
syn=n+1; break; } } }
Page 5 of 24
“编译原理”课程设计报告 CPR-1206010231
else if((ch>='0'&&ch<='9')) //当扫描到单词首字符为数字时
{
sum = 0;
while((ch>='0'&&ch<='9'))
//首字符后的字符也为数字时
{
sum=sum*10+ch-'0'; ch=chter[p++]; } p--; syn=11; if(sum>32767)
//定义的整数不能超过int类型的范围,否则种别码为-1
syn=-1;
}
else switch(ch)
//当扫描到的单词首字符为以下列出的符号时
{
case'+':syn=13;
token[0]=ch; break; case'-':syn=14;
token[0]=ch; break; case'*':syn=15;
token[0]=ch; break; case'/':syn=16;
token[0]=ch; break; case':':m=0;
token[m++]=ch; ch=chter[p++]; if(ch=='=')
Page 6 of 24
“编译原理”课程设计报告 CPR-1206010231
//首字符为:第二个字符为=时
{
syn=18; token[m++]=ch; } else {
syn=17; p--; } break; case'<':m=0;
token[m++]=ch; ch=chter[p++]; if(ch=='>')
//首字符为<第二个字符为>时
{
syn=21; token[m++]=ch; }
else if(ch=='=')
//首字符为<第二个字符为=时
{
syn=22; token[m++]=ch; } else {
syn=20; p--; } break; case'>':m=0;
token[m++]=ch; ch=chter[p++];
Page 7 of 24
“编译原理”课程设计报告 CPR-1206010231 if(ch=='=')
//首字符为>第二个字符为=时
{
syn=24; token[m++]=ch; } else {
syn=23; p--; } break; case'=':syn=25;
token[0]=ch; break; case';':syn=26;
token[0]=ch; break; case'(':syn=27;
token[0]=ch; break; case')':syn=28;
token[0]=ch; break; case'#':syn=0;
token[0]=ch; break; default: syn=-1;
break;
} }
【实验1.4】调试与测试数据
输入字符不在分析范围中的错误界面:
Page 8 of 24
相关推荐: