⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cifa.c

📁 给大家一些有关编译原理的程序
💻 C
字号:
#include<stdio.h>
#include<string.h>
#define N 100
char ch,A[N];
char *keyword[7]={"begin","and","if","then","else","while","do"};

int i=0;
char token[30];
int reserve(char B[])
{  int t, j;
 for(j=0;j<7;j++)
    
  if(!strcmp(B,keyword[j]))
  {	
switch(j)	
{ case 0: t=1;
	           break;
 case 1: t=2;
	               break;
 case 2: t=3;
	               break;
 case 3: t=4;
	               break;
 case 4: t=5;
	               break;
 case 5: t=6;
	               break;
 case 6: t=7;
	               break;
 default: printf("\n该字符串不是关键字");
	}
	break;
}
	
  if(j==7) t=10;
	return t;
}
char getch()
{
 ch=A[i];
 i++;
 return ch;
}
void retract()
{i--;
}
void getbc()
{  int j=0;
	while(ch==' '||ch=='\n')
{ j++;
 getch();
}
 if(j!=0&&ch=='#')
	 retract(); 
}

void concat()
{ char c[2];
c[0]=ch; c[1]='\0';
strcat(token,c);

}
void scaner()
{  int c;
	token[0]='\0';
 
   getbc();
   
   if((ch<='z')&&(ch>='A'))
   { 
	   while(((ch>='A')&&(ch<='z'))||((ch<='9')&&(ch>='0')))
		  
	   { concat();
         getch();
	   }
   retract();
   c=reserve(token);
   if(c!=10)
   printf("\n关键字 %d\t%s",c,token);
   else
    printf("\n标识符 10\t%s",token);
   }
   else 
   if((ch<='9')&&(ch>='0'))
   {
	   while((ch<='9')&&(ch>='0'))
	   {  concat();
          getch();
	   }
   retract();
   printf("\n数字   11\t %s",token);
   }
   else switch(ch)
   { case'+':printf("\n运算符 13\t+");
             break;
 case'-':printf("\n运算符 14\t-");
             break;
 case'*':printf("\n运算符 15\t*");
             break;

 case'/':printf("\n运算符 16\t/");
             break;
 case'<':getch();
	     if(ch=='=')
			printf("\n运算符 17\t<=");
		 else if(ch=='>')
              printf("\n运算符 18\t<>");
		      else
			  { retract();
			    printf("\n运算符 19\t<");
			  }
			  break;
 case':':getch();
	     if(ch=='=')
			 printf("\n运算符 22\t:=");
		 else
		 {retract();
		   printf("\n运算符 21\t:");
		 }
		 break;
 case';':printf("\n运算符 23\t;\n");
	     break;
 case'#':printf("\n程序结束");break;
 default:printf("\nERROR");
   }
}
void main()
{ int j=0;
  printf("请输入源程序:\n");
  scanf("%c",&A[0]);
  while(A[j]!='#')
  {j++;
   scanf("%c",&A[j]);
  }
  getch();
  while(ch!='#')
  {    
	  scaner();
	  getch();
  }
  
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -