签到

05月06日
尚未签到

共有回帖数 0

    晚街听风

    等级:
    词法
    #include iostream
    #include fstream
    #include string
    #include ctype.h
    #define NULL 0

    using namespace std;
    char str;
    FILE *fp;
    char *keyword[49]={"and","array","begin","end","case","const","div","do","downto","else","end","file","for","function",
    "goto","if","in","label","mod","nil","of","packed","procedure","program","record","repeat","set","then","to","type",
    "until","var","while","with","exports","shr","string","asm","object","unit","constructor","implementation",
    "destructor","uses","inherited","inline","interface","library","shl"};
    char *logicOperator[4]={"not","and","or","xor"};//逻辑运算符
    char *comment[4]={"{","}","(*","*)"};//注释符
    char *relation[6]={"=","","","","=","="};//关系运算符
    char *separator[12]={"(",")","[","]",":=",",",";",".",":","..","'","^"};//界符

    bool search(char str[],int wordtype)
    {
        int i;
        switch (wordtype)
        {
        case 1:
            for(i=0;i49;i++)
            {if(strcmp(keyword,str)==0)
            printf("%sn",keyword);
            return true;}
            break;
        case 2:
            for(i=0;i4;i++)
            {if(strcmp(logicOperator,str)==0)return true;}
            break;
        case 3:
            for(i=0;i4;i++)
            {if(strcmp(comment,str)==0)return true;
            }
            break;
        case 4:
            for(i=0;i6;i++)
            {if(strcmp(relation,str)==0)return true;
            }
            break;
        case 5:
            for(i=0;i12;i++)
            {if(strcmp(separator,str)==0)return true;
            }
            break;
        }
        return (false);
    }
    int f(char c)
    {
        if((c='a'&&c='z')||(c='A')&&(c='Z'))
            return 1;
        else
            return 0;
    }
    int digit(char c)
    {
        if((c='0'&&c='9')||c=='-'||c=='+'||c=='.')
        return 1;
        else
            return 0;
    }
    char isLetter(char ch)
    {
        int i=-1;
        char str1[20];

        while(f(ch)||digit(ch))
        {
            str1[++i]=ch;
            ch=fgetc(fp);
        }
        str1[i+1]='';
        str=ch;
        if (search(str1,1))
        {printf("关键字,%sn",str1);
        }
        else if(search(str1,2))
        {printf("逻辑运算符,%sn",str1);
        }
        else printf("标识符,%sn",str1);    
         return (ch);
    }

    void isDigit(char ch)
    {
        int i=-1,flag=-2;
        char str1[20];

       
            while (digit(ch)&&flag0)
            {
                if(ch=='.'){
                    flag++;
                    str1[++i]='.';
                    ch=fgetc(fp);
                }
                else
                {
                str1[++i]=ch;
                ch=fgetc(fp);
                }
            }
            str1[++i]='';
            str=ch;
            printf("数字,%sn",str1);
    }

    char isOther(char ch)
    {
        int i=-1;
        char str1[5];
        while(!digit(ch)&&!f(ch)&&ch!=' '&&ch!='n'&&ch!='t'&&ch!=EOF&&i=1)
        {
           
            str1[++i]=ch;
                str1[i+1]='';
            if (search(str1,3))

       {printf("注释符,%sn",str1);
            i=-1;
             ch=fgetc(fp);
        }
        else if(search(str1,4))
        {printf("关系运算符,%sn",str1);i=-1;
         ch=fgetc(fp);
        }
        else if(search(str1,5))
        {printf("界符,%sn",str1);i=-1;
         ch=fgetc(fp);
        }
        else
            ch=fgetc(fp);
           
        }
        if(i==2)   printf("ERROR!n");
        str=ch;
        return ch;
    }

    int main()
    {

        if ((fp=fopen("sourcecode.txt","r"))==NULL)
        {
            cout"Open source file failed!"endl;
            return 0;
        }
        str=fgetc(fp);
        while(1)
        {
            if(str==EOF)break;
            else if(str=='t'||str=='n'||str==' '){     str=fgetc(fp);;}
            else if(isalpha(str)!=0)
            {
                str=isLetter(str);
            }

            else if(digit(str))
            {
                isDigit(str);
            }
            else
           
                str=isOther(str);
               
        }
        fclose(fp);
        return 0;
    }

    楼主 2015-12-24 11:13 回复

共有回帖数 0
  • 回 帖
  • 表情 图片 视频
  • 发表

登录直线网账号

Copyright © 2010~2015 直线网 版权所有,All Rights Reserved.沪ICP备10039589号 意见反馈 | 关于直线 | 版权声明 | 会员须知