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

📄 cffx.cpp

📁 编译原理课程设计
💻 CPP
字号:
  
/*词法分析 cifa fenxi chengxu*/
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define NULL 0
#define max 8

FILE *fp;
char cbuffer;
char *key[max]={"if","else","for","while","do","return","break","main"};
char *border[8]={",",";","{","}","(",")","[","]"};
char *arithmetic[4]={"+","-","*","/"};
char *relation[6]={"<","<=","=",">",">=","<>"};
char *consts[20];
char *label[20];


int constnum=0,labelnum=0;

int search(char searchchar[],int wordtype)
{
     int i=0;
     switch (wordtype) {
       case 1:for (i=0;i<=max-1;i++)                       //遍历key[],查看是否是系统预留关键字
		  {
		   if (strcmp(key[i],searchchar)==0)
		     return(i+1);
		  }
       case 2:{for (i=0;i<=7;i++)                         //遍历border[],看是否是连接符
		  {
		   if (strcmp(border[i],searchchar)==0)
		      return(i+1);
		  }	       return(0);
	      }

       case 3:{for (i=0;i<=3;i++)                         //查看是否是算术运算符
		  {
		   if (strcmp(arithmetic[i],searchchar)==0)
		      {
		       return(i+1);
		      }
		  }
	      return(0);
	      }

       case 4:{for (i=0;i<=5;i++)                         //查看是否是关系运算符
		  {
		   if (strcmp(relation[i],searchchar)==0)
		      {
		       return(i+1);
		      }
		  }
	       return(0);
	      }

       case 5:                                            //查看是否是数字
		  
		  for (i=0;i<constnum;i++)		  
		  if (strcmp(consts[i],searchchar)==0)	return(i+1);	     
	      //consts[i]=(char *)malloc(sizeof(searchchar));
		  consts[i]=new char [sizeof(searchchar)];
		  strcpy(consts[i],searchchar);
	      constnum++;
	      return(i);
	      

       case 6:                                           //查看是否用户自定义变量或者是字符串
		   for (i=0;i<labelnum;i++)		 
		   if (strcmp(label[i],searchchar)==0)	return(i+1);
          
		   label[i]=new char [sizeof(searchchar)];
		  strcpy(label[i],searchchar);
	      labelnum++;
	      return(i);
	     

      }
	  return(0);

}


char alphaprocess(char buffer)//是否系统关键字分析子程序
{
      int atype;
      int i=-1;
      char alphatp[20];//临时变量,赞存当前要判断的字符串
      while ((isalpha(buffer))||(isdigit(buffer)))//将从当前字符开始的整个满足条件的字符串读取完整进行判断
	    {
	    alphatp[++i]=buffer;
	    buffer=fgetc(fp);
	    }
      alphatp[i+1]='\0';//设置数组结束标志
      if (atype=search(alphatp,1))//运行判断程序,检查是否在关键字表中存在此字符串
	  printf("(%s , 1)\n",alphatp); 
	  else
	 {
	 atype=search(alphatp,6);//不是关键字则判断是否是用户自定义变量
	 printf("(%s , 6)\n",alphatp);
	 }
      return(buffer);
}

char digitprocess(char buffer)//判断是否是数字
{
      int i=-1,j=0;
      char digittp[20];
      int dtype;
      while ((isdigit(buffer))||".")
	    {
		    if(buffer=='.') j++;//变量J用来记小数点个数
			digittp[++i]=buffer;
	        buffer=fgetc(fp);
	    }
      digittp[i+1]='\0';
	  if(j>2)printf("%s,error,not a word \n",digittp);
      dtype=search(digittp,5);
      printf("(%s  , 5)\n",digittp);
      return(buffer);
}

char otherprocess(char buffer)//不是数字和关键字的其他字符串分析子程序
{

      int i=-1;
      char othertp[20];
      int otype,otypetp;
      othertp[0]=buffer;
      othertp[1]='\0';
      if (otype=search(othertp,3))//判断是不是算术运算符
	 {
	 printf("(%s , 3)\n",othertp);
	 buffer=fgetc(fp);
	 goto out;
	 }

      if (otype=search(othertp,4))//判断是不是关系运算符
	      {
	      buffer=fgetc(fp);
	      othertp[1]=buffer;
	      othertp[2]='\0';
	      if (otypetp=search(othertp,4))
		 {
		 printf("(%s , 4)\n",othertp);
		 goto out;
		 }
	      else
		 othertp[1]='\0';
		 printf("(%s  , 4)\n",othertp);
		 goto out;
	      }

      if (buffer==':')
	      {
	      buffer=fgetc(fp);
	      if (buffer=='=')
		 printf("(:= , 2)\n");
		 buffer=fgetc(fp);
		 goto out;
	      }
	   else
	      {
	      if (otype=search(othertp,2))
		 {
		 printf("(%s , 2)\n",othertp);
		 buffer=fgetc(fp);
		 goto out;
		 }
	      }

	  if ((buffer!='\n')&&(buffer!=' '))
		  printf("%c error,not a word\n",buffer);
	  buffer=fgetc(fp);
out:      return(buffer);
}



void main()
{
     int i;
      for (i=0;i<=20;i++)//清空数组
	  {
	   label[i]=NULL;
	   consts[i]=NULL;
	  };
      if ((fp=fopen("Cpp1.cpp","r"))==NULL)//打开文件名为cpp1.cpp的文件,打开类型为r(只读)
	 printf("error");
      else
	{
	cbuffer = fgetc(fp);//fgetc()函数,读取当前第一个字符
	while (cbuffer!=EOF)//EOF为结束符
	 {
	  if (isalpha(cbuffer))//判断是否是字母,是的话则运行关键字判断程序
	     cbuffer=alphaprocess(cbuffer);
	  else if (isdigit(cbuffer))//判断是否是数字,是的话运行数字判断程序
	     cbuffer=digitprocess(cbuffer);
	  else cbuffer=otherprocess(cbuffer);//都不是的话运行其他类型变量的判断程序
	  }
	 printf("over\n");
	 getchar();
	 }
}

⌨️ 快捷键说明

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