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

📄 cifa.c

📁 c语言的词法分析器
💻 C
字号:
#include   <stdio.h>
#include   <stdlib.h>
#include   <string.h>
#include   <ctype.h>
#define   LEN   sizeof(struct   Node)
#define   NULL   0

  struct   Node
  {char   data;
    struct   Node   *next;
  };


  void   output(struct   Node*);
  void   scaner();
  void   getbc();
  void   getch();
  void   concat();
  int   letter(char   ch);
  int   degit(char   ch);
  int   reserve();
  void   retract();
  void   back(int   a,char   *b);

  struct   Node   *head,*p;
  char   ch;
  char   *key[]={"main","int","char","if","else","for","while"};
  char   token[20];

  int   main(void)
  {
      head=(struct   Node   *)malloc(LEN);
	    if(!head)   {printf("error");exit(1);}
      head->next=NULL;
      head->data=' ';
      p=head;
      printf("When   input   a   \'$\'   at   the   beigining   of   an   line,this   programe   will   be   over.\n");
      printf("And   the   programe   will   output   the   codes   you   inputed   just   now.\n");
      printf("Please   input   your   codes:\n");

while(1)
{
	int i=0;
    char   temp[256];
    gets(temp);
    if(temp[0]=='$')
		break;

    p->next=(struct   Node   *)malloc(LEN);
    if(!(head->next))
	{
	 printf("error");exit(1);
	}
    p=p->next;
    while(temp[i]!='\0'   &&   i<256)
	{
     p->data=temp[i];
     p->next=(struct Node *)malloc(LEN);
     if(!(p->next))
	 {
		 printf("error");exit(1);
	 }
	 p=p->next;
	 i++;
	}
  p->data='\n';
  p->next=NULL;
  }
  output(head);
  p=head->next;
  while(p->next!=NULL)
  scaner();
  system("pause");
  return   0;
  }

  void   output(struct   Node   *head)
  {
	  if(!head)
	  {
		  printf("error");
		  exit(1);
	  }
      p=head->next;
   while(p->next!=NULL)
    {
       printf("%c",p->data);
       p=p->next;
    }
      printf("\n");
  }

  void   getbc()
  {
  while(ch==' ')
  getch();
  }

  void   getch()
  {
  ch=p->data;
  p=p->next;
  }

  void   concat()
  {
  unsigned   int   i;
  i=strlen(token);
  token[i]=ch;
  token[i+1]='\0';
  }

  int   letter(char   ch)
  {
  return   isalpha((int)ch);
  }

  int   digit(char   ch)
  {
  return   isdigit((int)ch);
  }

  int   reserve()
  {
  int   k;
  for(k=0;k<6;k++)
  {
    if(strcmp(key[k],token)==0)   return   (k+1);
  }
  return   10;
    }

  void   retract()
  {
  struct   Node   *Q;
  Q=head->next;
  while(Q->next!=p)
  Q=Q->next;
  p=Q;
  }

  void   back(int   a,char   *b)
  {
  printf("(%d,%s)",a,b);
  }


  void   scaner()
  {
  int   c;
  token[0]=NULL;
  getch();
  getbc();
  if(letter(ch))
  {
  while(letter(ch)||digit(ch))
  {
  concat();
  getch();
  }
  retract();
  c=reserve();
  if(c!=10)   back(c,token);
  else   back(10,token);
  }
  else if(digit(ch))
  {
  while(digit(ch))
  {
  concat();
  getch();
  }
  retract();
  printf("(20,%d)",atoi(token));
  }
  else
  switch(ch)
  {
  case'+':   back(22,"+");break;
  case'-':   back(23,"-");break;
  case'*':   back(24,"*");break;
  case'/':   back(25,"/");break;
  case'<':   getch();
		    if(ch=='=')   back(38,"<=");
    retract();
	    back(36,"<");
		    break;
  case'>':   getch();
		    if(ch=='=')   back(37,">=");
    retract();
    back(35,">");
    break;
  case';':   back(34,";");break;
  case'{':   back(30,"{");break;
  case'}':   back(31,"}");break;
  case'(':   back(26,"(");break;
  case')':   back(27,")");break;
  case'=':   back(21,"=");break;
  case'\n':   break;
  default:   printf("error");break;
  }
  getchar();
  }

⌨️ 快捷键说明

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