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

📄 main.c

📁 非常经典的C语言词法分析器
💻 C
字号:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define ID 38
#define INT 39
#define LT 40
#define LE 41
#define EQ  42
#define NE 43
#define GT 44
#define GE 45
#define GA 46
#define COMMA 47     /* ',' */
#define FH 48        /* ';'*/
#define CO 49        /* ':'*/
#define LS 50       /* '(' */
#define RS 51       /* ') '*/
#define LM 52      /* '[' */
#define RM 53       /* '] '*/
#define LL 54      /* '{' */
#define RL 55       /* '} '*/
#define PC 56       /* '% '*/
#define BA 57       /* '\' */
#define DM 58       /* ' " '*/
#define ADD 59      /* '+'*/
#define SUB 60     /* '-'*/
#define MUL 61    /* '*' */
#define DIV 62     /* '/' */
#define SA 63      /* '++' */
#define SS 64      /* '--' */
char TOKEN[20];
char PRE[20],PPRE[20];
int k1=0;
int k2=0;
struct
{
	char def1[20];
	char def2[20];
}DEF[50];



int Lookup(char *x);
void Out(int a,char *x);
void Report_error(void);
void Scanner_example(FILE *fp);
void Print(FILE *fp);
/* Look up the key table! */
int Lookup(char *x)   
{
    char str[20],ch;
    int c,i,j;
    FILE *fp1=NULL;
    i=0; j=0;
    if((fp1=fopen("d:\\tools\\tc\\cifa\\key.txt","r"))==NULL)
    {
        printf("Unable to open key.txt!\n"); 
        exit(0);
    }
    ch=fgetc(fp1);
    while(ch!=EOF)
    { 
        while(!(ch==' '||ch=='\n'))
        {
            str[j]=ch;
            j++; 
            ch=fgetc(fp1);
        }
        str[j]='\0';
        i++;
        c=strcmp(x,str);
        if(c==0) 
            return i;
        else
        {
            ch=fgetc(fp1);
            while(ch==' '||ch=='\n')
                ch=fgetc(fp1);
            j=0;
        }
    }
    fclose(fp1);
    return 0;

}
/* Output the result! */
void Out(int a,char *x)     
{
    FILE *fp2,*fp3,*fp4;
    char str[21];
    int num[21],i,data,n=20;
    strcpy(str,x);
    if(a!=INT)  /* identifier or key */
    {
        if(a == ID)     /* identifier */
        {
            if((fp2=fopen("d:\\tools\\tc\\cifa\\id.txt","at+"))==NULL)
            {
                printf("Unable to open id.txt!\n");
                exit(1);
            }
            fprintf(fp2,"%d    %s\n",a,str);
            fclose(fp2);
        }        
    }
    else  /* const */
    {
        str[n]='\0';
        for(i=0;i<21&&str[i]!='\0';i++)   /* change char to int! */
            num[i]=str[i]-48;
        data=num[0];   
        while(i>1)
        {
            data=data*10+num[i-1];
            i--;
        }
        str[n]='\0';
        if(data==0)
        {
            str[0]='0';
            str[1]='\0';
        }
        else
        {
            while(data!=0)     /* change decimal to binary! */
            {
                n--;
                str[n]=data%2+48;
                data=data/2;
            }
            i=0;
            while(str[n]!='\0')   /* Output binary string */
            {
                str[i]=str[n];
                i++;
                n++;
            }
            str[i]='\0';
        }
        if((fp3=fopen("d:\\tools\\tc\\cifa\\const.txt","at+"))==NULL)  /* const */
        {
            printf("Unable to open const.txt!\n");
            exit(1);
        }
        fprintf(fp3,"%d    %s\n",a,str);
        fclose(fp3);
    }
    if((fp4=fopen("d:\\tools\\tc\\cifa\\result.txt","at+"))==NULL)
    {
        printf("Unable to open result.txt!\n");
        exit(1);
    }
    fprintf(fp4,"%d    %s\n",a,str);
    fclose(fp4);       
}
/* deal with error*/
void Report_error(void)
{
    FILE *fp4;
    char str[50]="The char is unvalid! ";
    if((fp4=fopen("d:\\tools\\tc\\cifa\\result.txt","at+"))==NULL)
    {
        printf("Unable to open result.txt!\n");
        exit(1);
    }
    fprintf(fp4,"%s\n",str);
    fclose(fp4);
}
/* Scanner the source file */
void Scanner_example(FILE *fp)
{
    char ch;
    int i,j,c;
    ch=fgetc(fp);
    while(ch!=EOF)
    {
        while(ch==' '||ch=='\n')
            ch=fgetc(fp);
        if(ch=='/')       /* get rid of exegesis*/
        {
            ch=fgetc(fp);
            if(ch=='*')
              {
                ch=fgetc(fp);
                while(!(ch=='*'&&(ch=fgetc(fp))=='/')) ch=fgetc(fp);
                ch=fgetc(fp);
              }
            else
            {fseek(fp,-2,1);
             ch=fgetc(fp);
            }
        }
        while(ch==' '||ch=='\n')
            ch=fgetc(fp);
        if(isalpha(ch))
        {
            TOKEN[0]=ch;
            ch=fgetc(fp);
            i=1;
            while(isalnum(ch))
            {
                TOKEN[i]=ch;
                i++;
                ch=fgetc(fp);
            }
            TOKEN[i]='\0';
            fseek(fp,-1,1);



			
			
			
			
			
			for(j=0;j<=k1;j++)
			{
				if(!strcmp(DEF[j].def1,TOKEN))
				{
					strcpy(TOKEN,DEF[j].def2);
					break;
				}
			}
			
			j=0;
			while(TOKEN[j]!='\0')
			{
				if(isdigit(TOKEN[j]))
				{
					j++;
				}
				else break;
			}
			if(TOKEN[j]=='\0')
			{
				printf("%c\n",TOKEN[j]);
				Out(INT,TOKEN);
				continue;
				
			}




			
			
		
			if(!strcmp(PRE,"define")) 
			{				
				strcpy(DEF[k1].def1,TOKEN);
				printf("DEF[%d].def1=%s\n",k1,DEF[k1].def1);				
				k1++;				
			}
			if(!strcmp(PPRE,"define"))
			{
			    strcpy(DEF[k2].def2,TOKEN);
				printf("DEF[%d].def2=%s\n",k2,DEF[k2].def2);
				k2++;				
			}



			
			/*for(j=0;j<=k1;j++)
			{
				if(!strcmp(DEF[j].def1,TOKEN))
				{
					strcpy(TOKEN,DEF[j].def2);
					break;
				}
			}
			
			j=0;
			while(TOKEN[j]!='\0')
			{
				if(isdigit(TOKEN[j]))
				{
					j++;
				}
				else break;
			}
			if(TOKEN[j]=='\0')
			{
				printf("%c\n",TOKEN[j]);
				Out(INT,TOKEN);
				break;
			}*/







            c=Lookup(TOKEN);
            if(c==0) Out(ID,TOKEN);  /*identifier*/
            else Out(c,TOKEN);   /*key*/
        }
        else if(isdigit(ch))   /*const integer*/
        {
            TOKEN[0]=ch;
            ch=fgetc(fp);
            i=1;
            while(isdigit(ch))
            {
                TOKEN[i]=ch;
                i++;
                ch=fgetc(fp);
            }
            TOKEN[i]='\0';
            fseek(fp,-1,1);
            Out(INT,TOKEN);





		
			
			if(!strcmp(PRE,"define")) 
			{
				strcpy(DEF[k1].def1,TOKEN);
				printf("DEF[%d].def1=%s\n",k1,DEF[k1].def1);				
				k1++;
				
			}
			if(!strcmp(PPRE,"define"))
			{
				strcpy(DEF[k2].def2,TOKEN);
				printf("DEF[%d].def2=%s\n",k2,DEF[k2].def2);				
				k2++;				
			}














        }
        else
            switch(ch)
        {
        case '<':            
            ch=fgetc(fp);
            if(ch=='=')
                Out(LE," ");
            else
            {
                fseek(fp,-1,1);
                Out(LT," ");
            }
            break;
        case '=':
            ch=fgetc(fp);
            if(ch=='=')
                Out(EQ," ");
            else
            {
                fseek(fp,-1,1);
                Out(GA," ");
            }
            break;
        case '>':
            ch=fgetc(fp);
            if(ch=='=')
                Out(GE," ");
            else
            {
                fseek(fp,-1,1);
                Out(GT," ");
            }
            break;
        case '!':
            ch=fgetc(fp);
            if(ch=='=')
                Out(NE," ");
            else Report_error();
            break;
        case ',':
            Out(COMMA," ");
            break;
        case ';':
            Out(FH," ");
            break;
        case '(':
            Out(LS," ");
            break;
        case ')':
            Out(RS," ");
            break;
        case '[':
            Out(LM," ");
            break;
        case ']':
            Out(RM," ");
            break;
        case '{':
            Out(LL," ");
            break;
        case '}':
            Out(RL," ");
            break;
        case '%':
            Out(PC," ");
            break;
        case '\\':
            Out(BA," ");
            break;
        case ':':
            Out(CO," ");
            break;
        case '"':
             Out(DM," ");
             break;
        case '+':
            ch=fgetc(fp);
            if(ch=='+')
                Out(SA," ");
            else
            {
                fseek(fp,-1,1);
                Out(ADD," ");
            }
            break;
        case '-':
            ch=fgetc(fp);
            if(ch=='-')
                Out(SS," ");
            else
            {
                fseek(fp,-1,1);
                Out(SUB," ");
            }
            break;
        case '*':
            Out(MUL," ");
            break;
        case '/':
            Out(DIV," ");
            break;
        default:
            Report_error();
            break;
        }
        ch=fgetc(fp);

	    strcpy(PPRE,PRE);		
		strcpy(PRE,TOKEN);
    }
	getch();
    return;
}
/* Output file context*/
void Print(FILE *fp)
{
    char ch;
    ch=fgetc(fp);
    while(ch!=EOF)
    {
        putchar(ch);
        ch=fgetc(fp);
    }
    fclose(fp);
}
/* void main()*/
void main()
{
    FILE *fp,*fp1,*fp2,*fp3,*fp4;  /*fp:example.txt  fp1:key.txt fp2:id.txt  fp3:const.txt fp4:result.txt*/
    fp=fp1=fp2=fp3=fp4=NULL;
    if((fp2=fopen("d:\\tools\\tc\\cifa\\id.txt","w"))==NULL)  /* Create id.txt */
    {
        printf("Unable to open id.txt!\n");
        exit(1);
    }
    fclose(fp2);
    if((fp3=fopen("d:\\tools\\tc\\cifa\\const.txt","w"))==NULL)  /* Create const.txt*/
    {
        printf("Unable to open const.txt!\n");
        exit(1);
    }
    fclose(fp3);
    if((fp4=fopen("d:\\tools\\tc\\cifa\\result.txt","w"))==NULL)  /* Create result.txt*/
    {
        printf("Unable to open result.txt!\n");
        exit(1);
    }
    fclose(fp4);
    printf("The source file as follows:\n");
    if((fp=fopen("d:\\tools\\tc\\cifa\\example.txt","r"))==NULL)
    {
        printf("Unable to open the file!\n");
        exit(0);
    }
    Print(fp);
    printf("\n");
    getch();
    printf("The key word table as follows:\n");
    if((fp1=fopen("d:\\tools\\tc\\cifa\\key.txt","r"))==NULL)
    {
        printf("Unable to open the file!\n");
        exit(0);
    }
    Print(fp1);
    getch();    
    /*fgets(str,19,fp1);
    while(str[0]!=' ')
    {
        puts(str);
        fflush(stdin);
        fgets(str,19,fp1);
    } */
    if((fp=fopen("d:\\tools\\tc\\cifa\\example.txt","r"))==NULL)   /* Open example.txt again! */
    {
        printf("Unable to open the file!\n"); 
        exit(0);
    }
    Scanner_example(fp);
    fclose(fp);
    printf("The compiling result as follows:\n");
    if((fp4=fopen("d:\\tools\\tc\\cifa\\result.txt","r"))==NULL)
    {
        printf("Unable to open result.txt!\n");
        exit(1);
    }
    Print(fp4);
    getch();
    printf("The identifier as follows:\n");
    if((fp2=fopen("d:\\tools\\tc\\cifa\\id.txt","r"))==NULL)
    {
        printf("Unable to open id.txt!\n");
        exit(1);
    }
    Print(fp2);
    getch();
    printf("The const as follows:\n");
    if((fp3=fopen("d:\\tools\\tc\\cifa\\const.txt","r"))==NULL)
    {
        printf("Unable to open const.txt!\n");
        exit(1);
    }
    Print(fp3);
    getch();
}

⌨️ 快捷键说明

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