zoukankan      html  css  js  c++  java
  • 编程实验一 词法分析程序

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    typedef struct node{
        char zifuji[20];
        struct node *next;
    }NODE;
    
    typedef struct stack{
        char num[20];
        int size;
    }STACK;
    
    void cmp(char *ch);
    int shizi(char ch[]);
    //字转词
    void zizhuanci(NODE *node){
        
        NODE *p;
        p=node->next;
    
        while(p!=NULL){
            
            cmp(p->zifuji);        
    
            p=p->next;
        }
    
    }
    //判断词义
    void cmp(char *ch){ int key; if(strcmp(ch,"begin")==0){ printf("<%s 1 > ",ch); } else if(strcmp(ch,"if")==0){ printf("<%s 2 > ",ch); } else if(strcmp(ch,"then")==0){ printf("<%s 3 > ",ch); } else if(strcmp(ch,"while")==0){ printf("<%s 4 > ",ch); } else if(strcmp(ch,"do")==0){ printf("<%s 5 > ",ch); } else if(strcmp(ch,"end")==0){ printf("<%s 6 > ",ch); } else if(strcmp(ch,"+")==0){ printf("<%s 13> ",ch); } else if(strcmp(ch,"-")==0){ printf("<%s 14> ",ch); } else if(strcmp(ch,"*")==0){ printf("<%s 15> ",ch); } else if(strcmp(ch,"/")==0){ printf("<%s 16> ",ch); } else if(strcmp(ch,":")==0){ printf("<%s 17> ",ch); } else if(strcmp(ch,":=")==0){ printf("<%s 18> ",ch); } else if(strcmp(ch,"<")==0){ printf("<%s 20> ",ch); } else if(strcmp(ch,"<=")==0){ printf("<%s 21> ",ch); } else if(strcmp(ch,"<>")==0){ printf("<%s 22> ",ch); } else if(strcmp(ch,">")==0){ printf("<%s 23> ",ch); } else if(strcmp(ch,">=")==0){ printf("<%s 24> ",ch); } else if(strcmp(ch,"=")==0){ printf("<%s 25> ",ch); } else if(strcmp(ch,";")==0){ printf("<%s 26> ",ch); } else if(strcmp(ch,"(")==0){ printf("<%s 27> ",ch); } else if(strcmp(ch,")")==0){ printf("<%s 28> ",ch); } else if(strcmp(ch,"#")==0){ printf("<%s 0> ",ch); } else{ key=shizi(ch); if(key==1) printf("<%s 12> ",ch,key); else if(key==2) printf("<%s 11> ",ch,key); else printf("<%s 错误> ",ch,key); } } //判断数字词义 int shizi(char ch[]){ int i; int key=0; for(i=0;i<=sizeof(ch);i++) if((ch[i]>='0'&&ch[i]<='9')||(ch[i]>='a'&&ch[i]<='z')) key=1; else if(ch[i]>='0'&&ch[i]<='9') key=2; return key; } //字分成词 int word_splitter(char* str,NODE *node){ NODE *p,*q; p=node; int length = strlen(str); char aword[20]; int number = 0; int start = 0; memset(aword, 0, 20); for(int i = start; i < length; ++i){ if(str[i] == ' '){ memcpy(aword, str + start, i - start); ++number; //printf("%d %s ", number, aword); q=(NODE *)malloc(sizeof(NODE)); q->next=NULL; strcpy(q->zifuji,aword); p->next=q; p=q; memset(aword, 0, 20); start = i + 1; } } if(start < length) { memcpy(aword, str + start, length - start); ++number; //printf("%d %s ", number, aword); q=(NODE *)malloc(sizeof(NODE)); q->next=NULL; strcpy(q->zifuji,aword); p->next=q; p=q; } return number; } int main(){ NODE *node,*p,*q; int i; STACK *stack; int size=-1; node = p =(NODE *)malloc(sizeof(NODE)); stack=(STACK *)malloc(sizeof(STACK)); printf("请输入: "); for(i=0;i<1;i++){ q=(NODE *)malloc(sizeof(NODE)); gets(node->zifuji); q=p->next; puts(node->zifuji); //printf("%d",sizeof(node->zifuji)); } i=word_splitter(node->zifuji,node); /* p=node->next; while(p){ puts(p->zifuji); printf(" "); p=p->next; } */ zizhuanci(node); return 0; }
  • 相关阅读:
    time 模块学习
    day 14 自定义模块,常用模块 time .datetime ,time 模块
    day 13 课后作业
    day 12 课后作业
    day 11课后作业
    树状数组最值
    hdu 1059 Dividing bitset 多重背包
    XVII Open Cup named after E.V. Pankratiev. XXI Ural Championship
    最长公共子序列板/滚动 N^2
    Uva 10635
  • 原文地址:https://www.cnblogs.com/caishun/p/4826934.html
Copyright © 2011-2022 走看看