• 词法分析


    词法分析程序的功能:

    一个具体的词法分析程序,从输入的源程序中,识别出各个具有独立意义的单词,即基本关键字、标识符、数字、运算符、分隔符五大类。并依次输出各个单词的内部编码及单词符号自身值。

    符号与种别码对照表:

    用文法描述词法规则:

    〈标识符〉→l|l〈字母数字〉
    〈字母数字〉→l|d|l〈字母数字〉|d〈字母数字〉
    〈无符号整数〉→d|d〈无符号整数〉
    〈运算符〉→+|-|*|/|=|〈〈等号〉|〉〈等号〉……
    〈等号〉→=
    〈界符〉→,|;|(|)|……
    再比如:
    C→aCA  Ba→aB
    C→bCB  Bb→bB
    AD→aD  C→ε
    BD→bD  D→ε
    Aa→bD

    源代码:

    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    void Analyse(FILE *fp,char ch);
    main()
    {
    FILE *fp;
    char ch;
    printf("请输入源程序代码(以#键结束): ");
    if((fp=fopen("data.txt","w"))==NULL)
    {
    printf("Failure to open data.txt! ");
    exit(0);
    }
    ch=getchar();
    while(ch!='#')//将字符串存放到文件中
    {
    fputc(ch,fp);
    ch=getchar();
    }
    fclose(fp);
    if((fp=fopen("data.txt","rb"))==NULL)
    {
    printf("Failure to open data.txt! ");
    exit(0);
    }
    ch=fgetc(fp);
    Analyse(fp,ch);
    }
    void Analyse(FILE *fp,char ch) 
    {
    char keyword[40][40]={"include","auto","break","case","char","const","continue","default","do","double","else","enum","extern","float","for","goto","if","int","long","return","short","sizeof","static","struct","switch","typedef","union","void","while"};
    int i=0,j,flag;
    int n=0,m;
    char alp[20];
    char num[11];
    FILE *fp1;
    FILE *fp2;
    while(!feof(fp))
    {
    if(ch=='+'||ch=='-'||ch=='*'||ch=='/'||ch=='%'||ch=='='||ch=='>'||ch=='<')//判断判断运算符
    printf("%c 运算符 ",ch);
    else if(ch==';'||ch=='{'||ch=='}'||ch=='('||ch==')')//判断分隔符
    printf("%c 分隔符 ",ch);
    else if(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')//判断字符是不是字母
    {
    if((fp1=fopen("data1.txt","w"))==NULL)

    printf("Failure to open data1.txt! ");
    exit(0);
    }
    while(ch>='a'&&ch<='z'||ch>='A'&&ch<='Z')//将每个字母合并成一个字符串放到有个新的文件中
    {
    fputc(ch,fp1);
    i++;
    ch=fgetc(fp);
    }
    fclose(fp1);
    if((fp1=fopen("data1.txt","rb"))==NULL)

    printf("Failure to open data1.txt! ");
    exit(0);
    }
    fgets(alp,i+1,fp1);
    for(j=0;j<30;j++)
    {
    if(strcmp(alp,keyword[j])==0)//判断是否为关键字或标识符
    flag=1;
    }
    if(flag==1)
    printf("%s 关键字 ",alp);
    else
    printf("%s 标识符 ",alp);
    fclose(fp1);
    Analyse(fp,ch);
    }
    else if(ch==' '||ch==' ')//识别回车和空格
    printf(" ");
    else if(ch>='0'&&ch<='9')//识别数字
    {
    if((fp2=fopen("data2.txt","w"))==NULL)

    printf("Failure to open data2.txt! ");
    exit(0);
    }
    while(ch>='0'&&ch<='9')
    {
    fputc(ch,fp2);
    n++;
    ch=fgetc(fp);
    }
    fclose(fp2);
    if((fp2=fopen("data2.txt","rb"))==NULL)

    printf("Failure to open data2.txt! ");
    exit(0);
    }
    fgets(num,n+1,fp2);
    printf("%s 数字 ",num);
    fclose(fp2);
    Analyse(fp,ch);
    }


    ch=fgetc(fp);//放到所有if后面



    }


    }

    截图:

  • 相关阅读:
    转-- js(jQuery)获取时间的方法及常用时间类
    jquery 选择器(name,属性,元素)大全
    CSS中LI圆点样式li {list-style-type:符号名称}
    [华为oj]称砝码
    c++中的字符串与数字相互转换
    [华为]DP合唱队形
    [hihoCoder#1032]最长回文子串
    [STL系列]仿函数
    [字符串]寻找一个字符串中最大的公共子串
    [STL系列]STL容器性能比较列表
  • 原文地址:https://www.cnblogs.com/stcy520/p/5924957.html
Copyright © 2020-2023  润新知