• 1014 词法分析器(完美版)


    #include<stdio.h>
    #include <string.h>
    #include<iostream.h>
    main(){
        char a[10000];
        char ch,bh;
        int i,s,t,j,q;
        s=0;
        t=0;
        printf("请输入字符(输入#结束):");
        do
        {
             scanf("%c",&ch);
             a[s++]=ch;
        }while(ch!='#');
        for(i=0;i<s;i++)
        {
            bh=a[i];
            if(bh>='a'&&ch<='z')
            {
                if(a[i]=='b'&&a[i+1]=='e'&&a[i+2]=='g'&&a[i+3]=='i'&&a[i+4]=='n')
                {
                    printf("<1,begin >
    ");
                i=i+4;
                continue;
                }
    
                else if(a[i]=='i'&&a[i-1]=='f')
                {
                    printf("<2,if >
    ");
                i=i+1;
                continue;
                }
    
                else if(a[i]=='t'&&a[i+1]=='h'&&a[i+2]=='e'&&a[i+3]=='n')
                {
                    printf("<3,then >
    ");
                i=i+3;
                continue;
                }
                else if(a[i]=='w'&&a[i+1]=='h'&&a[i+2]=='i'&&a[i+3]=='l'&&a[i+4]=='e')
                {
                    printf("<4,while >
    ");
                i=i+4;
                continue;
                }
                else if(a[i]=='d'&&a[i+1]=='o')
                {
                    printf("<5,do >
    ");
                i=i+1;
                continue;
                }
                else if(a[i]=='e'&&a[i+1]=='n'&&a[i+2]=='d')
                {
                    printf("<6,end >
    ");
                i=i+2;
                continue;
                }
                else if(a[i]=='l'&&a[i+1]=='('&&a[i+2]=='l'&&a[i+3]=='|'&&a[i+4]=='d'&&a[i+5]==')'&&a[i+6]=='*')
                {
                    printf("<10,l(l|d)* >
    ");
                i=i+6;
                continue;
                }
                else if(a[i]=='d'&&a[i+1]=='d'&&a[i+2]=='*')
                {
                    printf("<11,dd* >
    ");
                i=i+2;
                continue;
                }
                else
                {
                   if(a[i+1]>='0'&&a[i+1]<='9')
                   {
                    printf("<10,%c",a[i]);
                    while(a[i+1]>='0'&&a[i+1]<='9')
                    {
                            printf("%c",a[i+1]);
                            i=i+1;
                    }
                    printf(" >
    ");
                   }
                   else
                   printf("<10,%c >
    ",a[i]);
                }
            }
                else if(a[i]>='0'&&a[i]<='9')
                {
                  t=i;
                  j=i;
                while(a[i]>='0'&&a[i]<='9')
                {
                if(a[t]>='0'&&a[t]<='9')
                {
                 t=t+1;
                }
                else
                {
                q=t-j;
                printf("<11,");
                for(j;j<t;j++)
                {
                printf("%c",a[j]);
                }
                printf(" >
    ");
                i=i+q-1;
                break;
                }
                }
                }
    
            if(bh==':')
                if(a[i+1]=='='){
                    printf("<18,:= >
    ");
                    i=i+1;
                    continue;}
             if(bh=='<')
                if(a[i+1]='='){
                    printf("<21,<= >
    ");
                    i=i+1;
                    continue;}
             if(bh=='<')
                if(a[i+1]=='>'){
                    printf("<22,<> >
    ");
                    i=i+1;
                    continue;}
             if(bh=='>')
                if(a[i+1]=='='){
                    printf("<24,>= >
    ");
                    i=i+1;
                    continue;}
        
         switch(bh)
            {
            case '+':
                printf("<13,%c >
    ",bh);
                break;
             case '-':
                printf("<14,%c >
    ",bh);
                break;
             case '*':
                printf("<15,%c >
    ",bh);
                break;
             case '/':
                printf("<16,%c >
    ",bh);
                break;
             case ':':
                printf("<17,%c >
    ",bh);
                break;
             case '(':
                printf("<27,%c >
    ",bh);
                break;
             case ')':
                printf("<28,%c >
    ",bh);
                break;
             case '#':
                printf("<0,%c >
    ",bh);
                break;
             case '>':
                printf("<23,%c >
    ",bh);
                break;
             case '<':
                printf("<20,%c >
    ",bh);
                break;
             case ';':
                printf("<26,%c >
    ",bh);
                break;
             case '=':
                 printf("<25,%c >
    ",bh);
            }
        }
    
    }
  • 相关阅读:
    告别alert,拥抱console
    LeetCode之Max Points on a Line Total
    LeetCode之Maximum Product Subarray
    LeetCode之Reverse Words in a String
    LeetCode之Min Stack
    MySQL之系系统信息函数
    MySQL之日期时间函数
    MysqL之数值函数
    XML文件解析之JDOM解析
    XML文件解析之DOM4J解析
  • 原文地址:https://www.cnblogs.com/wangjunjie123/p/4878032.html
Copyright © 2020-2023  润新知