• 词法分析


    词法分析:
         
        #include<stdio.h>
    #include<string.h>
    #define t 10
    #define num2 11
    void analyse(FILE *fp,char ch);
    int main()
    { 
    FILE *fp;
    char ch;
    if((fp=fopen("demo.txt","w"))==NULL)
    {
    printf("Fileure to open demo.txt!
    ");
    }
    printf("请输入源程序(以$结束):");
    ch=getchar();
    while(ch!='$')
    {
    fputc(ch,fp);
    ch=getchar();
    }
    fclose(fp);
    if((fp=fopen("demo.txt","r"))==NULL)
    {
    printf("Fileure to open demo.txt!
    ");
    } 
    ch=fgetc(fp);
    analyse(fp,ch);
    }
    void analyse(FILE *fp,char ch)
    { 
    int m,j,i=0,n,flag=0;
    FILE *fp1,*fp2;
    char s;
    char gjz[34][20]={"short","if","do","while","end","auto","double","int","struct","break"," "," ","else","long","switch","case","enum","register","typedef","char","extern","union","const","float","unsigned","continue","signed","for","default",
    "void","goto","sizeof","volatile","static"};
    char num[5];
    char bsf[10];
    char alp[15];	
    while(!feof(fp))
    { 
    m=0,j=0;
    i=0;
    if(ch=='+')
    printf("%c	%d
    ",ch,35);
    else if(ch=='-')
    printf("%c	%d
    ",ch,36);
    else if(ch=='*')
    printf("%c	%d
    ",ch,37);
    else if(ch=='/')
    printf("%c	%d
    ",ch,38);
    else if(ch=='=')
    printf("%c	%d
    ",ch,39);
    else if(ch=='#')
    printf("%c	%d
    ",ch,40);
    else if(ch=='>')
    {
    s=fgetc(fp);
    if(s=='=')
    printf("%c=	%d
    ",ch,43);
    else
    {
    printf("%c	%d
    ",ch,41);
    analyse(fp,s);
    
    }
    }
    else if(ch=='<')
    {
    s=fgetc(fp);
    if(s=='=')
    printf("%c=	%d
    ",ch,44);
    else
    {
    printf("%c	%d
    ",ch,42);
    analyse(fp,s);
    
    }
    }
    else if(ch=='.')
    printf("%c	%d
    ",ch,53);
    else if(ch==':')
    { s=fgetc(fp);
    if(s=='=')
    printf("%c=	%d
    ",ch,45);
    else
    {
    printf("%c	%d
    ",ch,46);
    analyse(fp,s);
    
    }
    }
    else if(ch=='(')
    printf("%c	%d
    ",ch,47);
    else if(ch==')')
    printf("%c	%d
    ",ch,48);
    else if(ch==';')
    printf("%c	%d
    ",ch,54);
    else if(ch==',')
    printf("%c	%d
    ",ch,49);
    else if(ch=='{')
    printf("%c	%d
    ",ch,50);
    else if(ch=='}')
    printf("%c	%d
    ",ch,51);
    else if(ch=='"')
    {
    printf("%c	%d
    ",ch,52);
    }
    else if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')
    {
    if((fp1=fopen("data.txt","w"))==NULL)
    {
    printf("Fileure to open data.txt!
    ");
    }
    do
    { 
    fputc(ch,fp1);
    i++;
    ch=fgetc(fp);
    }while(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z'||ch>='0'&&ch<='9');
    fclose(fp1);
    if((fp1=fopen("data.txt","r"))==NULL)
    {
    printf("Fileure to open data.txt!
    ");
    }
    fgets(alp,i+1,fp1);
    for(j=0;j<34;j++)
    {
    if(j==10)
    j=j+2;
    if(strcmp(alp,gjz[j])==0)
    {
    printf("%s	%d
    ",gjz[j],j);
    flag=1;
    break;
    }
    }
    if(flag==0)
    printf("%s	%d
    ",alp,t);
    fclose(fp1);
    analyse(fp,ch);
    }
    else if(ch>='0'&&ch<='9')
    { 
    if((fp2=fopen("data2.txt","w"))==NULL)
    {
    printf("Fileure to open data2.txt!
    ");
    }
    do
    {
    fputc(ch,fp2);
    m++;
    ch=fgetc(fp);
    } while(ch>='0'&&ch<='9');
    fclose(fp2);
    if((fp2=fopen("data2.txt","r"))==NULL)
    {
    printf("Fileure to open data2.txt!
    ");
    }
    fgets(num,m+1,fp2);
    printf("%s	%d
    ",num,num2);
    fclose(fp2);
    analyse(fp,ch);
    }
    else
    printf("
    ");
    ch=fgetc(fp);
    }
    }
    

      

  • 相关阅读:
    Python使用小功能
    2月5日学习记录
    关于js的词法分析
    JavaScript
    blog界面美化
    css属性
    css三大特性
    css之选择器4
    css之选择器3-系列选择器
    css之选择器2
  • 原文地址:https://www.cnblogs.com/dfq621/p/5984902.html
Copyright © 2020-2023  润新知