• 词法分析程序


    #include<stdio.h> 
    #include<string.h>
    #include<stdlib.h>
    typedef struct node{
        char *string;
        struct node *next;
    }NODE;
    int main()
    {
        int i;
        char *ch;
        char *delim=":;,+-*/=";
        char input[20];
        char Keywords[][10]={{"auto"},{"double"},{"int"},{"struct"},{"break"},{"else"},{"long"},{"switch"},{"case"},
        {"enum"},{"register"},{"typedef"},{"char"},{"extern"},{"return"},{"union"},{"const"},{"float"},{"short"},{"unsigned"},{"continue"},
        {"for"},{"signed"},{"void"},{"default"},{"goto"},{"sizeof"},{"volatile"},{"do"},{"if"},{"while"},{"static"},{"+"},{"-"},{"*"},{"/"},{":"},
        {":="},{"<"},{"<="},{"<>"},{">"},{">="},{"="},{":"},{"("},{")"},{"#"},{"{"},{"}"}};
        char number[]="0123456789";
        char biaowords[]="abcdefghijklmnopqrstuvwxyz";
        NODE *head=NULL;
        NODE *thekeywords=NULL;
        NODE *biaoshi=NULL;
        NODE *yunsuanfu=NULL;
        NODE *shuzi=NULL;
        NODE *jiefu=NULL;
        NODE *p=NULL;
        NODE *q=NULL;
        head=(NODE*)malloc(sizeof(NODE));
        head->next=NULL;
        thekeywords=(NODE*)malloc(sizeof(NODE));
        thekeywords->next=NULL;
        biaoshi=(NODE*)malloc(sizeof(NODE));
        biaoshi->next=NULL;
        yunsuanfu=(NODE*)malloc(sizeof(NODE));
        yunsuanfu->next=NULL;
        jiefu=(NODE*)malloc(sizeof(NODE));
        jiefu->next=NULL;
        shuzi=(NODE*)malloc(sizeof(NODE));
        shuzi->next=NULL;
        //scanf("%s",input);
        do
        {
            scanf("%s
    ",input);
            i=0;
            while(input[i] !='')
            {
                //printf("char=%c
    ",input[i]);
                if(input[i] == '+' || input[i] == '-' || input[i] == '*' || input[i] == '/' || input[i] == '=')
                {
                    //printf("
    hello
    ");
                    p=(NODE*)malloc(sizeof(NODE));
                    p->next=NULL;
                    p->string=(char*)malloc(sizeof(char)*2);
                    p->string[0]='';
                    strncpy(p->string,&input[i],1);
                    p->next=yunsuanfu->next;
                    yunsuanfu->next=p;
                //    printf("yunsuanfu is %c
    ",input[i]);
                }
                else if(ispunct(input[i]) && input[i] != '~')
                {
                    //ch=(char*)malloc(sizeof(NODE));
                    p=(NODE*)malloc(sizeof(NODE));
                    p->next=NULL;
                    p->string=(char*)malloc(sizeof(char)*2);
                    p->string[0]='';
                    strncpy(p->string,&input[i],1);
                    p->next=jiefu->next;
                    jiefu->next=p;
                }
                i++;
            }
            ch=strtok(input,delim);
            do
            {
                if(input[0] =='~')
                    break;
                q=head;
                p=(NODE*)malloc(sizeof(NODE));
                p->next=NULL;
                p->string=(char*)malloc(sizeof(input));
                p->string[0]='';
                strcat(p->string,ch);
                while(q->next !=NULL)
                    q=q->next;
                p->next=q->next;
                q->next=p;
            }while(ch=strtok(NULL,delim));        
        }while(input[0] !='~');
        i=0;
        p=head->next;
        printf("
    head:");
        while(p !=NULL)
        {
            printf("%s	",p->string);
            p=p->next;
        }
        p=head->next;
        while(p !=NULL)
        {
            q=(NODE*)malloc(sizeof(NODE));
            q->next=NULL;
            if(strspn(p->string,number))
            {
                q->string=p->string;
                q->next=shuzi->next;
                shuzi->next=q;            
            }
            else
            {
                for(i=0;i<=49;i++)
                {
                    if(strcmp(p->string,Keywords[i])==0)
                    {
                        q->string=Keywords[i];
                        if(i>=0 && i<=31)
                        {
                            q->next=thekeywords->next;
                            thekeywords->next=q;
                        }
                        else if(i>=32 && i<=35)
                        {
                            q->next=yunsuanfu->next;
                            yunsuanfu->next=q;
                        }
    
                        break;
                    }
                }
            }
            if(i>49)
            {
                if(strspn(p->string,biaowords))
                {
    
                        q->string=p->string;
                        q->next=biaoshi->next;
                        biaoshi->next=q;                    
                }
            }
            p=p->next;
        }
        printf("
    thekeywords:
    ");
        p=thekeywords->next;
        while(p != NULL)
        {
            printf("%s	",p->string);
            p=p->next;
        }
        printf("
    yunsuanfu:
    ");
        p=yunsuanfu->next;
        while(p != NULL)
        {
            printf("%s	",p->string);
            p=p->next;
        }
        printf("
    biaoshi:
    ");
        p=biaoshi->next;
        while(p != NULL)
        {
            printf("%s	",p->string);
            p=p->next;
        }
        printf("
    jiefu:
    ");
        p=jiefu->next;
        while(p != NULL)
        {
            printf("%s	",p->string);
            p=p->next;
        }
        printf("
    shuzi:
    ");
        p=shuzi->next;
        while(p != NULL)
        {
            printf("%s	",p->string);
            p=p->next;
        }
        return 0;
    }

    <字母>:S-〉A|AA

         A->{a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,w,y,

            A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,S,Y,Z}
    <数字>:S->A|AA|A0|A.

         A->{1,2,3,4,5,6,7,8,9}
    <整数常数> :S->A|AA|A0

              A->{1,2,3,4,5,6,7,8,9}


    <标识符>:S->A|A_|AB

          A->{a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,w,y,

            A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,S,Y,Z}

          B->{0,1,2,3,4,5,6,7,8,9}

    <关键字>:S->{int,float,double,long,unsige,static,include,define,for,if,whlie,do,for,...}

    <运算符>:S->{+,-,*,/,%}
    <界符>; S->{; , ( , ) , , }

  • 相关阅读:
    由js apply与call方法想到的js数据类型(原始类型和引用类型)
    ajax跨域
    悟透JavaScript(理解JS面向对象的好文章)
    Item 18: Understand the Difference between Function, Method, and Constructor Calls
    torch:hardsigmoid
    torch:CrossEntropy是个构造器,所以loss = torch.nn.CrossEntropyLoss()(output, target)这么写就对了
    检查运行文件名称与模块名称是否重复。如果重复,需要将文件名称重新命名。
    a = np.r_[1:4,0,4] 这语法有点神奇
    lstm官方demo,有好几种输出的shape
    np.c_ 这个玩意儿不是个方法,而是返回一个类然后直接和后面的ndarry相乘。。。相当于concatenate(【】,dim)
  • 原文地址:https://www.cnblogs.com/XiaoG-Liu/p/5924052.html
Copyright © 2020-2023  润新知