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

📄 cifa.c

📁 编写关于编译原理知识的基本语法树和词法分析器
💻 C
字号:
/*  HELLO.C -- Hello, world */
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <ctype.h> 
#include <malloc.h> 
#include <ctype.h> 
#include <conio.h> 
#define NULL 0 
FILE *fp; 
char ch; 
char *keyword[8]={"BEGIN","DO","ELSE","END","IF","THEN","VAR","WHILE"};  /*t=1*/
char *border[6]={",",";",".",":=","(",")"};                              /*t=2*/
char *arithmetic[4]={"+","-","*","/"};                                   /*t=3*/
char *relation[6]={"<","<=","=",">",">=","<>"};                          /*t=4*/


int search(char searchstr[],int wordtype)
{ 
 int i; 
 switch (wordtype) 
 { 
 case 1:for(i=0;i<=7;i++) 
     { 
      if(strcmp(keyword[i],searchstr)==0) 
       return(1);
     }
 case 2: for(i=0;i<=5;i++)
   { 
    if(strcmp(border[i],searchstr)==0)
     return(2);
   }
 case 3:
 {
  for(i=0;i<=3;i++) 
  { 
   if(strcmp(arithmetic[i],searchstr)==0)
    return(3);
  } 
  break; 
  }
 case 4: for(i=0;i<=5;i++)
 {
    if(strcmp(relation[i],searchstr)==0)
     return(4);
  }

   default: return(0);
  }
}

int identfynum(char searchstr[],int wordtype)     /*获取内部码*/
{ 
 int i; 
 switch (wordtype) 
 { 
 case 1:for(i=0;i<=7;i++) 
     { 
      if(strcmp(keyword[i],searchstr)==0) 
       return(i);
     } 
 case 3:{
  for(i=0;i<=3;i++) 
  { 
   if(strcmp(arithmetic[i],searchstr)==0)
    return(i);
  } 
  break; 
     } 
 case 4: for(i=0;i<=5;i++)
   { 
    if(strcmp(relation[i],searchstr)==0)
     return(i);
   } 
 case 2: for(i=0;i<=5;i++)
   { 
    if(strcmp(border[i],searchstr)==0)
     return(i);
   }
  default: return(0);
 }
}

char letterprocess (char ch)/*字母处理函数 */
{ 
 int i=-1;
 int x;
 char letter[20]; 
 while (isalnum(ch)!=0) 
 { 
  letter[++i]=ch; 
  ch=fgetc(fp); 
 }; 
 letter[i+1]='\0'; 
 if (search(letter,1)==1)
 {
  x=identfynum(letter,1);
  printf("<keyword,%s,internal code:%d>\n",letter,x);
  /*strcat(letter,"\n");*/
  /*fputs('<' letter '>\n',outp);*/
 } 
 else 
 { 
  printf("<words,%s>\n",letter);
  /*strcat(letter,"\n");*/
  /*fputs(letter,outp);*/
 } 
 return(ch); 
} 

char numberprocess(char ch)   /*数字处理程序*/
{ 
 int i=-1;
 char num[20]; 
 while (isdigit(ch)!=0) 
 { 
  num[++i]=ch; 
  ch=fgetc(fp); 
 } 
 if(isalpha(ch)!=0) 
 { 
  while(isspace(ch)==0) 
  { 
   num[++i]=ch; 
   ch=fgetc(fp);
  } 
  num[i+1]='\0'; 
  printf("Error!Illegal word :%s\n",num);
  goto u; 
 } 
 num[i+1]='\0';
    printf("<num,%s>\n",num);
    /*strcat(num,"\n");*/
    /*fputs(num,outp);*/
u: return(ch); 
} 


char otherprocess(char ch) 
{ 
 int i=-1; 
 int x,y;
 char other[20]; 
 if (isspace(ch)!=0) 
 { 
  ch=fgetc(fp); 
  goto u; 
 } 
 while ((isspace(ch)==0)&&(isalnum(ch)==0)) 
 { 
  other[++i]=ch; 
 ch=fgetc(fp); 
 } 
 other[i+1]='\0'; 
 if (search(other,3)==3)
 {
  x=identfynum(other,3);
  if(x<2) y=10+x  ;
  else  y=18+x ;
  printf("<arithmetic,%s,internal code:%d>\n",other,y);
 }
 else 
  if (search(other,4)==4)
  {
   x=identfynum(other,4);
   printf("<relation,%s,internal code:%d>\n",other,x);
  }
  else 
   if (search(other,2)==2)
   {x=identfynum(other,4);
    printf("<border,%s,internal code:%d>\n",other,x);
   }
   else  
    printf("Error ! Illegal char! %s\n",other);
u: return (ch); 
} 


void main ()
{ 
 char str,c; 
 printf("*******************************Analysis Engine**********************************\n");
 /*outp=fopen("二元式表.txt","w");*/
     printf("wordtype 1: Keyword\n");
     printf("wordtype 2: Border\n");
     printf("wordtype 3: Arithmetic\n");
     printf("wordtype 4: Relation\n");
     printf("wordtype 5: Consts\n");
     printf("wordtype 6: Word\n");
 if ((fp=fopen("123.txt","r"))==NULL)
  printf("Can not open the source file!\n");
 else
 { 
  str =fgetc(fp); 
  while (str!=EOF) 
  { 
   if (isalpha(str)!=0) 
    str=letterprocess(str); 
   else 
   { 
    if (isdigit(str)!=0) 
     str=numberprocess(str); 
    else 
     str=otherprocess(str); 
   } 
     
  }; 
  printf("\n\nCompleted!! Over!!\n");
  printf("\n\nPush any key to exit!\n\n\n");
 } 
   getch();
} 


⌨️ 快捷键说明

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