| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1282 人关注过本帖
标题:小小码屋
只看楼主 加入收藏
朱大哥
Rank: 3Rank: 3
等 级:论坛游侠
威 望:2
帖 子:72
专家分:123
注 册:2015-1-16
收藏
得分:0 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define KEYSIZE 17
#define SYMBOLSIZE 6
#define KEYWORD    1
#define DEFINED    2
#define SYMBOL     3
#define NUM        4
#define ERROR      5
#define POINT_TYPE 1
#define FUNC_TYPE  2
#define ARR_TYPE   3
#define VAR_TYPE   4
#define ISCHARACTER ((97<=*str&&*str<=122)||(65<=*str&&*str<=90))
#define ISCHAR_NUM_UNDERLINE ((97<=*(str+i)&&*(str+i)<=122)||(65<=*(str+i)&&*(str+i)<=90)||(48<=*(str+i)&&*(str+i)<=57)||(*(str+i)=='_'))
#define IS_NUM(A)    (48<=A&&A<=57)
typedef struct defined{
    char definedname[10];
    int  definedtype;
    int  position;
    struct defined *next;
      
}defined;
typedef struct{
    char keyname[10];
    char keyexp[100];
}keyword;
char dealword[100];
keyword key[KEYSIZE]={
        "char","字符型",
        "int","整型",
        "short","短整型",
        "long","长整型",
          "float","浮点型",
        "double","双精度浮点型",
        "signed","有符号的",
        "unsigned","无符号的",
        "auto","自动的",
        "extern","全局的",
        "static","静态的",
        "typedef","别名",
        "union","共用体",
        "struct","结构体",
        "enum","枚举体",
        "void","无返回值或者无参或者无类型",
        "*","指针",
        };
char symbol[]={'(','[',')',']',',',' '};
char exp_sequence[40];
char exp_symbol_sequence[20];
char exp_keyword_sequence[20];
char exp_error_sequence[20][20];
int  exp_num_sequence[20];
int  exp_sequence_flag=0;
int  exp_symbol_seq_flag=0;
int  exp_keyword_seq_flag=0;
int  exp_num_seq_flag=0;
int  exp_error_seq_flag=0;
struct defined def;
int issymbol(char c)
{
    int i=0,flag=0;
    while(i<SYMBOLSIZE){
        if(c==symbol[i]){
            flag=i+1;
            break;
        }
        i++;
    }
            return flag;
}
int isdefined(char *str)
{
    int i=1,num;
    if( ISCHARACTER ){
        while(*(str+i)){
            if(!ISCHAR_NUM_UNDERLINE){
                exp_sequence[exp_sequence_flag]=ERROR;
                exp_sequence_flag++;
                return 0;
            }   
            i++;
        }
        for(i=0;i<KEYSIZE;i++)
            if(!strcmp(key[i].keyname,str)){
                exp_sequence[exp_sequence_flag]=KEYWORD;
                exp_sequence_flag++;
                exp_keyword_sequence[exp_keyword_seq_flag]=i;
                exp_keyword_seq_flag++;
                break;
            }
        if(i==KEYSIZE){           
            exp_sequence[exp_sequence_flag]=DEFINED;
            exp_sequence_flag++;
            strcpy(def.definedname,str);      //确定标识符名字,当表达式存在多个标识符时,无法说出是哪几个 以后需更改 strcpy需改写
        }   
    }
    else if(IS_NUM(*str)){
        num=*str-48;
        while(*(str+i)){
            if(!IS_NUM(*(str+i))){
               
                exp_sequence[exp_sequence_flag]=ERROR;
                exp_sequence_flag++;
                return 0;
            }
            num*=10+(*(str+i)-48);   
            i++;
           
        }
        exp_sequence[exp_sequence_flag]=NUM;
        exp_sequence_flag++;
        exp_num_sequence[exp_num_seq_flag]=num;
        exp_num_seq_flag++;
      
    }
    else{
      
        exp_sequence[exp_sequence_flag]=ERROR;
        exp_sequence_flag++;
        return 0;
    }   
    return 1;
}
int readword(char *str,int i)
{
        int n,j=0;
        if(*(str+i)=='\0')
            return 0;
        if((n=issymbol(*(str+i)))){
           
            if(*(str+i)!=' '){
                exp_sequence[exp_sequence_flag]=SYMBOL;
                exp_sequence_flag++;
                exp_symbol_sequence[exp_symbol_seq_flag]=n;
                exp_symbol_seq_flag++;
               
            }      
            i++;
        }
        else{
            for(;*(str+i)!='\0';i++){
                if(!issymbol(*(str+i))){
                    dealword[j]=*(str+i);
                    j++;
                }
                else{
                    dealword[j]='\0';
                         break;
                }
            }   
            if(!isdefined(dealword)){
                strcpy(exp_error_sequence[exp_error_seq_flag],dealword);
                exp_error_seq_flag++;
            }
            memset(dealword,0,100);
        }
      
        readword(str,i);
   
}
int symbolmatching()
{
    int flag1=0,flag2=0,i=0,error_flag=0;
    for(;i<exp_symbol_seq_flag;i++){
        if(exp_symbol_sequence[i]==1)
            flag1++;
        else if(exp_symbol_sequence[i]==3)
            flag1--;
        else if(exp_symbol_sequence[i]==2)
            flag2++;
        else    if(exp_symbol_sequence[i]==4)
            flag2--;
        else
            ;
        if(flag1<0||flag2<0 \
           ||(exp_symbol_sequence[i]==1&&exp_symbol_sequence[i+1]==4) \
           ||(exp_symbol_sequence[i]==2&&exp_symbol_sequence[i+1]==3)){
            printf("符号不匹配");
            return 0;
        }
    }
    if(flag1>0||flag2>0){
            printf("符号号不匹配");
            return 0;
        }
   
   
    return 1;
   
}
int definedmatching()
{
    signed int i,flag=-1,position;
    for(i=0;i<exp_sequence_flag;i++)
        if(exp_sequence[i]==DEFINED){
            flag++;
            position=i;
        }
    if(!(flag==0)){
        printf("存在零个或者两个以上标识符,表达式不合法");
        return 0;
    }   
    def.position=position;
    return 1;
}
int have_no_error()
{
     int i,j=0;
     for(i=0;i<exp_sequence_flag;i++)
           if(exp_sequence[i]==ERROR){
                    printf("[%s]:表达式错误 ",exp_error_sequence[j]);
                 j++;
        }
    if(j==0)
        return 1;
    else
        return 0;
      
}
int main(int argc,char **argv)
{
    int i,j=0;
    char *str="int 1a[10]";
    readword(str,0);   
    if(have_no_error()&&symbolmatching()&&definedmatching())
        printf("可以进行语义分析了\n");
   
   
}


[ 本帖最后由 朱大哥 于 2015-1-28 18:04 编辑 ]
2015-01-28 17:41
八画小子
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:37
帖 子:709
专家分:2063
注 册:2010-11-11
收藏
得分:0 
以下是引用朱大哥在2015-1-28 16:55:49的发言:

我用vim啊,写点编译相关的
帖子可以回帖
我一点一点的堆代码
堆点存点
真的堆榻了,这不原始资料都在

终于找到一个也用vim的大哥了,要不交流一下心得?
2015-01-28 19:14
神马都要问
Rank: 2
等 级:论坛游民
帖 子:6
专家分:11
注 册:2015-1-29
收藏
得分:0 
始终不会装双系统
2015-01-29 10:24
wube
Rank: 12Rank: 12Rank: 12
等 级:贵宾
威 望:23
帖 子:1820
专家分:3681
注 册:2011-3-24
收藏
得分:0 
我是用三系统,看片打游戏都用MAC,只有编程VS2008和用iTools才用Win7
但是公司用XP,所以VC6和VB6在家里都开不起来

不要選我當版主
2015-01-29 11:51
朱大哥
Rank: 3Rank: 3
等 级:论坛游侠
威 望:2
帖 子:72
专家分:123
注 册:2015-1-16
收藏
得分:0 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define KEYSIZE       11
#define SYMBOLSIZE    6
#define MODIFIERSIZE  8
#define KEYWORD       1
#define DEFINED       2
#define SYMBOL        3
#define NUM           4
#define MODIFIER      5
#define ERROR         6
#define POINT_TYPE    1
#define FUNC_TYPE     2
#define ARR_TYPE      3
#define VAR_TYPE      4
#define LONG_KEY_POSITION   3
#define SHORT_KEY_POSITION  2
#define LONG_MODI_POSITION  1
#define SHORT_MODI_POSITION 0
#define ISCHARACTER ((97<=*str&&*str<=122)||(65<=*str&&*str<=90))
#define ISCHAR_NUM_UNDERLINE ((97<=*(str+i)&&*(str+i)<=122)||(65<=*(str+i)&&*(str+i)<=90)||(48<=*(str+i)&&*(str+i)<=57)||(*(str+i)=='_'))
#define IS_NUM(A)    (48<=A&&A<=57)
typedef struct defined{
    char definedname[10];
    int  definedtype;
    int  position;
    struct defined *next;
      
}defined;
typedef struct{
    char keyname[10];
    char keyexp[100];
}keyword;
char dealword[100];
keyword key[KEYSIZE]={
        "char","字符型",
        "int","整型",
        "short","短整型",
        "long","长整型",
          "float","浮点型",
        "double","双精度浮点型",
        "union","共用体",
        "struct","结构体",
        "enum","枚举体",
        "void","无返回值或者无参或者无类型",
        "*","指针",
        };
keyword modifierkey[MODIFIERSIZE]={
        "short","短整型的",
        "long","长整型的",
        "signed","有符号的",
        "unsigned","无符号的",
        "auto","自动的",
        "extern","全局的",
        "static","静态的",
        "typedef","别名",
        };
char symbol[]={'(','[',')',']',',',' '};
char exp_sequence[40];
char exp_symbol_sequence[20];
char exp_keyword_sequence[20];
char exp_error_sequence[20][20];
char exp_modifier_sequence[20];
int  exp_num_sequence[20];
int  exp_sequence_flag=0;
int  exp_symbol_seq_flag=0;
int  exp_keyword_seq_flag=0;
int  exp_num_seq_flag=0;
int  exp_error_seq_flag=0;
int  exp_modifier_seq_flag=0;
struct defined def;
int issymbol(char c)
{
    int i=0,flag=0;
    while(i<SYMBOLSIZE){
        if(c==symbol[i]){
            flag=i+1;
            break;
        }
        i++;
    }
            return flag;
}
int isdefined(char *str)
{
    int i=1,num,flag;
    flag=KEYSIZE>MODIFIERSIZE ? KEYSIZE:MODIFIERSIZE;
    if( ISCHARACTER ){
        while(*(str+i)){
            if(!ISCHAR_NUM_UNDERLINE){
                exp_sequence[exp_sequence_flag]=ERROR;
                exp_sequence_flag++;
                return 0;
            }   
            i++;
        }
        for(i=0;i<flag;i++){
            if(i<KEYSIZE){
                    if(!strcmp(key[i].keyname,str)){
                    exp_sequence[exp_sequence_flag]=KEYWORD;
                    exp_sequence_flag++;
                    exp_keyword_sequence[exp_keyword_seq_flag]=i;
                    exp_keyword_seq_flag++;
                    break;
                    }
            }
            if(i<MODIFIERSIZE){
                if(!strcmp(modifierkey[i].keyname,str)){
                    exp_sequence[exp_sequence_flag]=MODIFIER;
                    exp_sequence_flag++;
                    exp_modifier_sequence[exp_keyword_seq_flag]=i;
                    exp_modifier_seq_flag++;
                    break;
                    }
                  
            }
           
        }
        if(i==flag){           
            exp_sequence[exp_sequence_flag]=DEFINED;
            exp_sequence_flag++;
            strcpy(def.definedname,str);      //确定标识符名字,当表达式存在多个标识符时,无法说出是哪几个 以后需更改 strcpy需改写
        }   
    }
    else if(IS_NUM(*str)){
        num=*str-48;
        while(*(str+i)){
            if(!IS_NUM(*(str+i))){
               
                exp_sequence[exp_sequence_flag]=ERROR;
                exp_sequence_flag++;
                return 0;
            }
            num*=10+(*(str+i)-48);   
            i++;
           
        }
        exp_sequence[exp_sequence_flag]=NUM;
        exp_sequence_flag++;
        exp_num_sequence[exp_num_seq_flag]=num;
        exp_num_seq_flag++;
      
    }
    else{
      
        exp_sequence[exp_sequence_flag]=ERROR;
        exp_sequence_flag++;
        return 0;
    }   
    return 1;
}
int readword(char *str,int i)
{
        int n,j=0;
        if(*(str+i)=='\0')
            return 0;
        if((n=issymbol(*(str+i)))){
           
            if(*(str+i)!=' '){
                exp_sequence[exp_sequence_flag]=SYMBOL;
                exp_sequence_flag++;
                exp_symbol_sequence[exp_symbol_seq_flag]=n;
                exp_symbol_seq_flag++;
               
            }      
            i++;
        }
        else{
            for(;*(str+i)!='\0';i++){
                if(!issymbol(*(str+i))){
                    dealword[j]=*(str+i);
                    j++;
                }
                else{
                    dealword[j]='\0';
                         break;
                }
            }   
            if(!isdefined(dealword)){
                strcpy(exp_error_sequence[exp_error_seq_flag],dealword);
                exp_error_seq_flag++;
            }
            memset(dealword,0,100);
        }
      
        readword(str,i);
   
}
int symbolmatching()
{
    int flag1=0,flag2=0,i=0,error_flag=0;
    for(;i<exp_symbol_seq_flag;i++){
        if(exp_symbol_sequence[i]==1)
            flag1++;
        else if(exp_symbol_sequence[i]==3)
            flag1--;
        else if(exp_symbol_sequence[i]==2)
            flag2++;
        else    if(exp_symbol_sequence[i]==4)
            flag2--;
        else
            ;
        if(flag1<0||flag2<0 \
           ||(exp_symbol_sequence[i]==1&&exp_symbol_sequence[i+1]==4) \
           ||(exp_symbol_sequence[i]==2&&exp_symbol_sequence[i+1]==3)){
            printf("符号不匹配");
            return 0;
        }
    }
    if(flag1>0||flag2>0){
            printf("符号号不匹配");
            return 0;
        }
   
   
    return 1;
   
}
//int definedmatching()
//{
//    signed int i,flag=-1,position;
//    for(i=0;i<exp_sequence_flag;i++)
//        if(exp_sequence[i]==DEFINED){
//            flag++;
//            position=i;
//        }
//    if(!(flag==0)){
//        printf("存在零个或者两个以上标识符,表达式不合法");
//        return 0;
//    }   
//    def.position=position;
//    return 1;
//}
int have_no_error()
{
     int i,j=0;
     for(i=0;i<exp_sequence_flag;i++)
           if(exp_sequence[i]==ERROR){
                    printf("[%s]:表达式错误 ",exp_error_sequence[j]);
                 j++;
        }
    if(j==0)
        return 1;
    else
        return 0;
      
}
int main(int argc,char **argv)
{
    int i,j=0;
    char *str="unsigned int a[10]";
    readword(str,0);   
    if(have_no_error()&&symbolmatching())
        printf("可以进行语义分析了\n");
   
   
}
  


[ 本帖最后由 朱大哥 于 2015-1-29 15:16 编辑 ]
2015-01-29 15:02
朱大哥
Rank: 3Rank: 3
等 级:论坛游侠
威 望:2
帖 子:72
专家分:123
注 册:2015-1-16
收藏
得分:0 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define KEYSIZE       11
#define SYMBOLSIZE    6
#define MODIFIERSIZE  8
#define KEYWORD       1
#define DEFINED       2
#define SYMBOL        3
#define NUM           4
#define MODIFIER      5
#define ERROR         6
#define POINT_TYPE    7
#define FUNC_TYPE     8
#define ARR_TYPE      9
#define VAR_TYPE      10
#define LONG_KEY_POSITION   3
#define SHORT_KEY_POSITION  2
#define LONG_MODI_POSITION  1
#define SHORT_MODI_POSITION 0
#define ISCHARACTER ((97<=*str&&*str<=122)||(65<=*str&&*str<=90))
#define ISCHAR_NUM_UNDERLINE ((97<=*(str+i)&&*(str+i)<=122)||(65<=*(str+i)&&*(str+i)<=90)||(48<=*(str+i)&&*(str+i)<=57)||(*(str+i)=='_'))
#define IS_NUM(A)    (48<=A&&A<=57)
typedef struct defined{
    char definedname[10];
    int  definedtype;
    int  position;
    struct defined *next;
      
}defined;
typedef struct{
    char keyname[10];
    char keyexp[100];
}keyword;
char dealword[100];
keyword key[KEYSIZE]={
        "char","字符型",
        "int","整型",
        "short","短整型",
        "long","长整型",
          "float","浮点型",
        "double","双精度浮点型",
        "union","共用体",
        "struct","结构体",
        "enum","枚举体",
        "void","无返回值或者无参或者无类型",
        "*","指针",
        };
keyword modifierkey[MODIFIERSIZE]={
        "short","短整型的",
        "long","长整型的",
        "signed","有符号的",
        "unsigned","无符号的",
        "auto","自动的",
        "extern","全局的",
        "static","静态的",
        "typedef","别名",
        };
char symbol[]={'(','[',')',']',',',' '};
char exp_sequence[40];
char exp_store_sequence[40];
char exp_symbol_sequence[20];
char exp_keyword_sequence[20];
char exp_error_sequence[20][20];
char exp_defined_sequence[20][20];
char exp_modifier_sequence[20];
int  exp_num_sequence[20];
int  exp_sequence_flag=0;
int  exp_symbol_seq_flag=0;
int  exp_keyword_seq_flag=0;
int  exp_num_seq_flag=0;
int  exp_error_seq_flag=0;
int  exp_modifier_seq_flag=0;
int  exp_defined_seq_flag=0;
int  exp_store_seq_flag=0;
struct defined def;
int issymbol(char c)
{
    int i=0,flag=0;
    while(i<SYMBOLSIZE){
        if(c==symbol[i]){
            flag=i+1;
            break;
        }
        i++;
    }
            return flag;
}
int isdefined(char *str)
{
    int i=1,num,flag;
    flag=KEYSIZE>MODIFIERSIZE ? KEYSIZE:MODIFIERSIZE;
    if( ISCHARACTER ){
        while(*(str+i)){
            if(!ISCHAR_NUM_UNDERLINE){
                exp_sequence[exp_sequence_flag]=ERROR;
                exp_sequence_flag++;
                exp_sequence[exp_sequence_flag]=0;
                exp_sequence_flag++;
                return 0;
            }   
            i++;
        }
        for(i=0;i<flag;i++){
            if(i<KEYSIZE){
                    if(!strcmp(key[i].keyname,str)){
                    exp_sequence[exp_sequence_flag]=KEYWORD;
                    exp_sequence_flag++;
                    //exp_keyword_sequence[exp_keyword_seq_flag]=i;
                    //exp_keyword_seq_flag++;
                    exp_sequence[exp_sequence_flag]=i;
                    exp_sequence_flag++;
                    break;
                    }
            }
            if(i<MODIFIERSIZE){
                if(!strcmp(modifierkey[i].keyname,str)){
                    exp_sequence[exp_sequence_flag]=MODIFIER;
                    exp_sequence_flag++;
                    //exp_modifier_sequence[exp_modifier_seq_flag]=i;
                    //exp_modifier_seq_flag++;
                    exp_sequence[exp_sequence_flag]=i;
                    exp_sequence_flag++;
                  
                    break;
                    }
                  
            }
           
        }
        if(i==flag){           
            exp_sequence[exp_sequence_flag]=DEFINED;
            exp_sequence_flag++;
            strcpy(exp_defined_sequence[exp_defined_seq_flag],dealword);
            exp_sequence[exp_sequence_flag]=exp_defined_seq_flag;
            exp_sequence_flag++;
            exp_defined_seq_flag++;
        }   
    }
    else if(IS_NUM(*str)){
        num=*str-48;
        while(*(str+i)){
            if(!IS_NUM(*(str+i))){
               
                exp_sequence[exp_sequence_flag]=ERROR;
                exp_sequence_flag++;
                exp_sequence[exp_sequence_flag]=0;
                exp_sequence_flag++;
                return 0;
            }
            num*=10+(*(str+i)-48);   
            i++;
           
        }
        exp_sequence[exp_sequence_flag]=NUM;
        exp_sequence_flag++;
        exp_num_sequence[exp_num_seq_flag]=num;
        exp_sequence[exp_sequence_flag]=exp_num_seq_flag;
        exp_num_seq_flag++;
        exp_sequence_flag++;
      
    }
    else{
      
        exp_sequence[exp_sequence_flag]=ERROR;
        exp_sequence_flag++;
        exp_sequence[exp_sequence_flag]=0;
        exp_sequence_flag++;
        return 0;
    }   
    return 1;
}
int readword(char *str,int i)
{
        int n,j=0;
        if(*(str+i)=='\0')
            return 0;
        if((n=issymbol(*(str+i)))){
           
            if(*(str+i)!=' '){
                exp_sequence[exp_sequence_flag]=SYMBOL;
                exp_sequence_flag++;
                exp_symbol_sequence[exp_symbol_seq_flag]=n;
                exp_sequence[exp_sequence_flag]=n;
                exp_sequence_flag++;
                exp_symbol_seq_flag++;
               
            }      
            i++;
        }
        else{
            for(;*(str+i)!='\0';i++){
                if(!issymbol(*(str+i))){
                    dealword[j]=*(str+i);
                    j++;
                }
                else{
                    dealword[j]='\0';
                         break;
                }
            }   
            if(!isdefined(dealword)){
                strcpy(exp_error_sequence[exp_error_seq_flag],dealword);
                exp_error_seq_flag++;
            }
            memset(dealword,0,100);
        }
      
        readword(str,i);
   
}
int symbolmatching()
{
    int flag1=0,flag2=0,i=0,error_flag=0;
    for(;i<exp_symbol_seq_flag;i++){
        if(exp_symbol_sequence[i]==1)
            flag1++;
        else if(exp_symbol_sequence[i]==3)
            flag1--;
        else if(exp_symbol_sequence[i]==2)
            flag2++;
        else    if(exp_symbol_sequence[i]==4)
            flag2--;
        else
            ;
        if(flag1<0||flag2<0 \
           ||(exp_symbol_sequence[i]==1&&exp_symbol_sequence[i+1]==4) \
           ||(exp_symbol_sequence[i]==2&&exp_symbol_sequence[i+1]==3)){
            printf("符号不匹配");
            return 0;
        }
    }
    if(flag1>0||flag2>0){
            printf("符号号不匹配");
            return 0;
        }
   
   
    return 1;
   
}
int have_no_error()
{
     int i,j=0,flag=0;
     for(i=0;i<exp_sequence_flag;i+=2){
           if(exp_sequence[i]==ERROR){
                    printf("[%s]:表达式错误 ",exp_error_sequence[j]);
                 j++;
            }   
        if(exp_sequence[i]==DEFINED)   
             flag=1;
    }
    if(flag==0)
        printf("表达式中没有标识符\n");
    if(j==0&&flag==1)
        return 1;
    else
        return 0;
      
}
int syntax_simple(char *exp,int size)
{
    int i=0,j=0,pointnum=0;
    char expsegment[40];
    for(;i<size;i+=2){
       if(exp[i]!=DEFINED){
            if(exp[i]==SYMBOL&&exp[i+1]==1){
                    i+=2;
                for(;i<size;i+=2){   
                    if(exp[i]==SYMBOL&&exp[i+1]==3){
                        break;
                    }
                    expsegment[j]=exp[i];
                    j++;
                    expsegment[j]=exp[i+1];
                    j++;
                }
               
                     syntax_simple(expsegment,j);
               
            }
           
            else{
                exp_store_sequence[exp_store_seq_flag]=exp[i];
                  exp_store_seq_flag++;
                exp_store_sequence[exp_store_seq_flag]=exp[i+1];
                exp_store_seq_flag++;      
            }
        }
      else{
            exp_store_sequence[exp_store_seq_flag]=exp[i];
            exp_store_seq_flag++;
            exp_store_sequence[exp_store_seq_flag]=exp[i+1];
            exp_store_seq_flag++;
            if(exp[i+2]==SYMBOL&&exp[i+3]==2){
               
                if(exp[i+4]==NUM&&(exp[i+6]==SYMBOL&&exp[i+7]==4)){
                    printf("数组\n");
                    exp_store_sequence[exp_store_seq_flag]=ARR_TYPE;
                    exp_store_seq_flag++;
                    //printf("cccc%d\n ",exp_sequence[i+5]);
                    exp_store_sequence[exp_store_seq_flag]=exp_num_sequence[exp[i+5]];
                    exp_store_seq_flag++;
                    i+=8;      
                }
                else{
                    if(exp[i+4]==SYMBOL&&exp[i+5]==4)
                        printf("%s[]未指定数组大小\n",exp_defined_sequence[exp[i+1]]);
                    else
                        printf("%s[后面未定义\n",exp_defined_sequence[exp[i+1]]);
                    exp_store_sequence[exp_store_seq_flag]=ERROR;
                    exp_store_seq_flag++;
                    exp_store_sequence[exp_store_seq_flag]=0;
                    exp_store_seq_flag++;   
                    return 0;
                }
               
            }
            else if(exp[i+2]==SYMBOL&&exp[i+3]==1){
                    printf("函数\n");
                    exp_store_sequence[exp_store_seq_flag]=FUNC_TYPE;
                    exp_store_seq_flag++;
                    exp_store_sequence[exp_store_seq_flag]=0;
                    exp_store_seq_flag++;
            }
            else {
                if(i+2!=size){
                    printf("%s识别出是个单变量,但是后面有多余的\n",exp_defined_sequence[exp[i+1]]);
                    exp_store_sequence[exp_store_seq_flag]=ERROR;
                    exp_store_seq_flag++;
                    exp_store_sequence[exp_store_seq_flag]=0;
                    exp_store_seq_flag++;
                    return 0;   
                }
                else{
                    printf("变量\n");
                    exp_store_sequence[exp_store_seq_flag]=VAR_TYPE;
                    exp_store_seq_flag++;
                    exp_store_sequence[exp_store_seq_flag]=0;
                    exp_store_seq_flag++;
                }
            }
           
           
           
        }
        }
               
      
   
}
int main(int argc,char **argv)
{
    int i,j=0;
    char *str="unsigned int (a[jsdhfsf]) ";
    readword(str,0);   
    if(have_no_error()&&symbolmatching()){
        syntax_simple(exp_sequence,exp_sequence_flag);   
    }
    for(i=0;i<exp_store_seq_flag;i+=1)
        printf("%d \n",exp_store_sequence[i]);
      
    //printf("%d \n",exp_num_sequence[0]);
   
   
   
}
2015-01-30 05:55
朱大哥
Rank: 3Rank: 3
等 级:论坛游侠
威 望:2
帖 子:72
专家分:123
注 册:2015-1-16
收藏
得分:0 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define KEYSIZE       11
#define SYMBOLSIZE    7
#define MODIFIERSIZE  8
#define KEYWORD       1
#define DEFINED       2
#define SYMBOL        3
#define NUM           4
#define MODIFIER      5
#define ERROR         6
#define POINT_TYPE    7
#define FUNC_TYPE     8
#define ARR_TYPE      9
#define VAR_TYPE      10
#define LONG_KEY_POSITION   3
#define SHORT_KEY_POSITION  2
#define LONG_MODI_POSITION  1
#define SHORT_MODI_POSITION 0
#define ISCHARACTER ((97<=*str&&*str<=122)||(65<=*str&&*str<=90))
#define ISCHAR_NUM_UNDERLINE ((97<=*(str+i)&&*(str+i)<=122)||(65<=*(str+i)&&*(str+i)<=90)||(48<=*(str+i)&&*(str+i)<=57)||(*(str+i)=='_'))
#define IS_NUM(A)    (48<=A&&A<=57)
#define IS_LEFT_BRACKET(i)         exp[i]==SYMBOL&&exp[i+1]==1
#define IS_RIGHT_BRACKET      exp[i]==SYMBOL&&exp[i+1]==3
#define IS_LEFT_SQUARE_BRACKET  exp[i]==SYMBOL&&exp[i+1]==2
#define IS_RIGHT_SQUARE_BRACKET exp[i]==SYMBOL&&exp[i+1]==4
#define IS_ASTERISK_IN_STORE(A) exp_store_sequence[A]==SYMBOL&&exp_store_sequence[A+1]==7
#define IS_ARRARY             exp[i+2]==SYMBOL&&exp[i+3]==2&&exp[i+4]==NUM&&exp[i+6]==SYMBOL&&exp[i+7]==4
#define IS_VARIABLE(i)           i==size||(IS_LEFT_BRACKET(i))
#define IS_COMMA(exp,i)               exp[i]==SYMBOL&&exp[i+1]==5
#define PUSH(A,B)               exp_store_sequence[exp_store_seq_flag++]=A;\
                           exp_store_sequence[exp_store_seq_flag++]=B
           
typedef struct defined{
    char definedname[10];
    int  definedtype;
    int  position;
    struct defined *next;
      
}defined;
typedef struct{
    char keyname[10];
    char keyexp[100];
}keyword;
char dealword[100];
keyword key[KEYSIZE]={
        "char","字符型",
        "int","整型",
        "short","短整型",
        "long","长整型",
          "float","浮点型",
        "double","双精度浮点型",
        "union","共用体",
        "struct","结构体",
        "enum","枚举体",
        "void","无返回值或者无参或者无类型",
        };
keyword modifierkey[MODIFIERSIZE]={
        "short","短整型的",
        "long","长整型的",
        "signed","有符号的",
        "unsigned","无符号的",
        "auto","自动的",
        "extern","全局的",
        "static","静态的",
        "typedef","别名",
        };
char symbol[]={'(','[',')',']',',',' ','*'};
char exp_sequence[40];
char exp_store_sequence[40];
char exp_symbol_sequence[20];
char exp_keyword_sequence[20];
char exp_error_sequence[20][20];
char exp_defined_sequence[20][20];
char exp_modifier_sequence[20];
int  exp_num_sequence[20];
int  exp_sequence_flag=0;
int  exp_symbol_seq_flag=0;
int  exp_keyword_seq_flag=0;
int  exp_num_seq_flag=0;
int  exp_error_seq_flag=0;
int  exp_modifier_seq_flag=0;
int  exp_defined_seq_flag=0;
int  exp_store_seq_flag=0;
struct defined def;
int issymbol(char c)
{
    int i=0,flag=0;
    while(i<SYMBOLSIZE){
        if(c==symbol[i]){
            flag=i+1;
            break;
        }
        i++;
    }
            return flag;
}
int isdefined(char *str)
{
    int i=1,num,flag;
    flag=KEYSIZE>MODIFIERSIZE ? KEYSIZE:MODIFIERSIZE;
    if( ISCHARACTER ){
        while(*(str+i)){
            if(!ISCHAR_NUM_UNDERLINE){
                exp_sequence[exp_sequence_flag]=ERROR;
                exp_sequence_flag++;
                exp_sequence[exp_sequence_flag]=0;
                exp_sequence_flag++;
                return 0;
            }   
            i++;
        }
        for(i=0;i<flag;i++){
            if(i<KEYSIZE){
                    if(!strcmp(key[i].keyname,str)){
                    exp_sequence[exp_sequence_flag]=KEYWORD;
                    exp_sequence_flag++;
                    //exp_keyword_sequence[exp_keyword_seq_flag]=i;
                    //exp_keyword_seq_flag++;
                    exp_sequence[exp_sequence_flag]=i;
                    exp_sequence_flag++;
                    break;
                    }
            }
            if(i<MODIFIERSIZE){
                if(!strcmp(modifierkey[i].keyname,str)){
                    exp_sequence[exp_sequence_flag]=MODIFIER;
                    exp_sequence_flag++;
                    //exp_modifier_sequence[exp_modifier_seq_flag]=i;
                    //exp_modifier_seq_flag++;
                    exp_sequence[exp_sequence_flag]=i;
                    exp_sequence_flag++;
                  
                    break;
                    }
                  
            }
           
        }
        if(i==flag){           
            exp_sequence[exp_sequence_flag]=DEFINED;
            exp_sequence_flag++;
            strcpy(exp_defined_sequence[exp_defined_seq_flag],dealword);
            exp_sequence[exp_sequence_flag]=exp_defined_seq_flag;
            exp_sequence_flag++;
            exp_defined_seq_flag++;
        }   
    }
    else if(IS_NUM(*str)){
        num=*str-48;
        while(*(str+i)){
            if(!IS_NUM(*(str+i))){
               
                exp_sequence[exp_sequence_flag]=ERROR;
                exp_sequence_flag++;
                exp_sequence[exp_sequence_flag]=0;
                exp_sequence_flag++;
                return 0;
            }
            num*=10+(*(str+i)-48);   
            i++;
           
        }
        exp_sequence[exp_sequence_flag]=NUM;
        exp_sequence_flag++;
        exp_num_sequence[exp_num_seq_flag]=num;
        exp_sequence[exp_sequence_flag]=exp_num_seq_flag;
        exp_num_seq_flag++;
        exp_sequence_flag++;
      
    }
    else{
      
        exp_sequence[exp_sequence_flag]=ERROR;
        exp_sequence_flag++;
        exp_sequence[exp_sequence_flag]=0;
        exp_sequence_flag++;
        return 0;
    }   
    return 1;
}
int readword(char *str,int i)
{
        int n,j=0;
        if(*(str+i)=='\0')
            return 0;
        if((n=issymbol(*(str+i)))){
           
            if(*(str+i)!=' '){
                exp_sequence[exp_sequence_flag]=SYMBOL;
                exp_sequence_flag++;
                exp_symbol_sequence[exp_symbol_seq_flag]=n;
                exp_sequence[exp_sequence_flag]=n;
                exp_sequence_flag++;
                exp_symbol_seq_flag++;
               
            }      
            i++;
        }
        else{
            for(;*(str+i)!='\0';i++){
                if(!issymbol(*(str+i))){
                    dealword[j]=*(str+i);
                    j++;
                }
                else{
                    dealword[j]='\0';
                         break;
                }
            }   
            if(!isdefined(dealword)){
                strcpy(exp_error_sequence[exp_error_seq_flag],dealword);
                exp_error_seq_flag++;
            }
            memset(dealword,0,100);
        }
      
        readword(str,i);
   
}
int symbolmatching()
{
    int flag1=0,flag2=0,i=0,error_flag=0;
    for(;i<exp_symbol_seq_flag;i++){
        if(exp_symbol_sequence[i]==1)
            flag1++;
        else if(exp_symbol_sequence[i]==3)
            flag1--;
        else if(exp_symbol_sequence[i]==2)
            flag2++;
        else    if(exp_symbol_sequence[i]==4)
            flag2--;
        else
            ;
        if(flag1<0||flag2<0 \
           ||(exp_symbol_sequence[i]==1&&exp_symbol_sequence[i+1]==4) \
           ||(exp_symbol_sequence[i]==2&&exp_symbol_sequence[i+1]==3)){
            printf("符号不匹配");
            return 0;
        }
    }
    if(flag1>0||flag2>0){
            printf("符号号不匹配");
            return 0;
        }
   
   
    return 1;
   
}
int have_no_error()
{
     int i,j=0,flag=0;
     for(i=0;i<exp_sequence_flag;i+=2){
           if(exp_sequence[i]==ERROR){
                    printf("[%s]:表达式错误 ",exp_error_sequence[j]);
                 j++;
            }   
        if(exp_sequence[i]==DEFINED)   
             flag=1;
    }
    if(flag==0)
        printf("表达式中没有标识符\n");
    if(j==0&&flag==1)
        return 1;
    else
        return 0;
      
}
/*int exp_segment(char *exp,int size,char *segment)
{
    int i,j=0,bracket_flag=1;
    for(i=0;i<size;i+=2){
        if(IS_LEFT_BRACKET)
            bracket_flag++;
        if(IS_RIGHT_BRACKET)
            bracket_flag--;
        if(bracket_flag==0)
            break;
        segment[j++]=exp[i];
        segment[j++]=exp[i+1];
    }
   
   
}*/
int syntax_simple(char *exp,int size)
{
    int i=0,j=0,asterisk_num=0,n=0,bracket_flag,n1,n2,z;
    char expsegment[40],func_list[40];
    for(;i<size;i+=2){
        if(IS_LEFT_BRACKET(i)){
                bracket_flag=1;j=0;
                for(i+=2;i<size;i+=2){
                    if(IS_LEFT_BRACKET(i))
                        bracket_flag++;
                    if(IS_RIGHT_BRACKET)
                        bracket_flag--;
                    if(bracket_flag==0)
                        break;
               
                    expsegment[j++]=exp[i];
                    expsegment[j++]=exp[i+1];
                }
                if(   exp_store_seq_flag>=2&&
                       (exp_store_sequence[exp_store_seq_flag-2]==VAR_TYPE||
                        exp_store_sequence[exp_store_seq_flag-2]==POINT_TYPE||
                          exp_store_sequence[exp_store_seq_flag-2]==ARR_TYPE
                            )
                      ){
                    if(j==0&&(i+4)<size){
                            printf("解析出是个函数,但是函数列表后存有不名符号\n");
                            PUSH(ERROR,0);
                            return  0;
                        }
                    n2=exp_store_seq_flag;
                    PUSH(FUNC_TYPE,0);
                    while(n<j){
                        n1=0;
                        for(;n<j;n+=2){
                            if(IS_COMMA(expsegment,n))
                                break;
                            func_list[n1++]=expsegment[n];
                            func_list[n1++]=expsegment[n+1];
                        }   
                        syntax_simple(func_list,n1);
                        if(n!=j){
                            //printf("xad%d %d\n",n,j);
                            PUSH(SYMBOL,5);
                        }      
                        exp_store_sequence[n2+1]++;
                        n+=2;
                       
                    }
                  
                           
                  }
                else{
                    if(j==0){
                        printf("表达式错误:存在空括号\n");
                        PUSH(ERROR,0);
                        return 0;
                    }
                    syntax_simple(expsegment,j);
                    memset(expsegment,0,40);
                }
            }
        else{
            if(exp[i]==DEFINED){
                if(IS_ARRARY){
                    if(IS_ASTERISK_IN_STORE(exp_store_seq_flag-2)){
                        asterisk_num++;
                        for(exp_store_seq_flag-=2;exp_store_seq_flag>=0;exp_store_seq_flag-=2)
                                if(IS_ASTERISK_IN_STORE(exp_store_seq_flag-2))
                                    asterisk_num++;
                                else
                                    break;
                        PUSH(exp[i],exp[i+1]);
                        PUSH(POINT_TYPE,asterisk_num);
                        PUSH(ARR_TYPE,exp_num_sequence[exp[i+5]]);   
                    }
                    else{
                        PUSH(exp[i],exp[i+1]);
                        PUSH(ARR_TYPE,exp_num_sequence[exp[i+5]]);
                    }
                    i+=6;
                }
                else if(IS_VARIABLE(i+2)){
                    if(IS_ASTERISK_IN_STORE(exp_store_seq_flag-2)){
                        asterisk_num++;
                        for(exp_store_seq_flag-=2;exp_store_seq_flag>=0;exp_store_seq_flag-=2)
                                if(IS_ASTERISK_IN_STORE(exp_store_seq_flag-2))
                                    asterisk_num++;
                                else
                                    break;
                        PUSH(exp[i],exp[i+1]);
                        PUSH(POINT_TYPE,asterisk_num);
                        PUSH(VAR_TYPE,0);   
                    }
                    else{
                        PUSH(exp[i],exp[i+1]);
                        PUSH(VAR_TYPE,0);
                    }
                       
                }
                else{
                    PUSH(ERROR,0);
                    printf("错误\n");
                    return 0;
                }
                  
            }
            else{
                PUSH(exp[i],exp[i+1]);
            }
      
           
        }
      
    }
}
int main(int argc,char **argv)
{
    int i,j=0;
    char *str="unsigned int  A[100]";
    readword(str,0);   
    if(have_no_error()&&symbolmatching()){
        syntax_simple(exp_sequence,exp_sequence_flag);   
    }
    for(i=0;i<exp_store_seq_flag;i+=1)
        printf("%d \n",exp_store_sequence[i]);
      
   
   
   
}


[ 本帖最后由 朱大哥 于 2015-1-31 02:21 编辑 ]
2015-01-30 22:15
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:0 
现在有云的

授人以渔,不授人以鱼。
2015-01-30 22:53
朱大哥
Rank: 3Rank: 3
等 级:论坛游侠
威 望:2
帖 子:72
专家分:123
注 册:2015-1-16
收藏
得分:0 
不会用
2015-01-31 02:21
朱大哥
Rank: 3Rank: 3
等 级:论坛游侠
威 望:2
帖 子:72
专家分:123
注 册:2015-1-16
收藏
得分:0 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define KEYSIZE       11
#define SYMBOLSIZE    7
#define MODIFIERSIZE  8
#define KEYWORD       1
#define DEFINED       2
#define SYMBOL        3
#define NUM           4
#define MODIFIER      5
#define ERROR         6
#define POINT_TYPE    7
#define FUNC_TYPE     8
#define ARR_TYPE      9
#define VAR_TYPE      10
#define LONG_KEY_POSITION   3
#define SHORT_KEY_POSITION  2
#define LONG_MODI_POSITION  1
#define SHORT_MODI_POSITION 0
#define ISCHARACTER ((97<=*str&&*str<=122)||(65<=*str&&*str<=90))
#define ISCHAR_NUM_UNDERLINE ((97<=*(str+i)&&*(str+i)<=122)||(65<=*(str+i)&&*(str+i)<=90)||(48<=*(str+i)&&*(str+i)<=57)||(*(str+i)=='_'))
#define IS_NUM(A)    (48<=A&&A<=57)
#define IS_LEFT_BRACKET(i)         exp[i]==SYMBOL&&exp[i+1]==1
#define IS_RIGHT_BRACKET      exp[i]==SYMBOL&&exp[i+1]==3
#define IS_LEFT_SQUARE_BRACKET  exp[i]==SYMBOL&&exp[i+1]==2
#define IS_RIGHT_SQUARE_BRACKET exp[i]==SYMBOL&&exp[i+1]==4
#define IS_ASTERISK_IN_STORE(A) exp_store_sequence[A]==SYMBOL&&exp_store_sequence[A+1]==7
#define IS_ARRARY             exp[i+2]==SYMBOL&&exp[i+3]==2&&exp[i+4]==NUM&&exp[i+6]==SYMBOL&&exp[i+7]==4
#define IS_VARIABLE(i)           i==size||(IS_LEFT_BRACKET(i))
#define IS_COMMA(exp,i)               exp[i]==SYMBOL&&exp[i+1]==5
#define PUSH(A,B)               exp_store_sequence[exp_store_seq_flag++]=A;\
                           exp_store_sequence[exp_store_seq_flag++]=B
           
typedef struct defined{
    char definedname[10];
    int  definedtype;
    int  position;
    struct defined *next;
      
}defined;
typedef struct{
    char keyname[10];
    char keyexp[100];
}keyword;
char dealword[100];
keyword key[KEYSIZE]={
        "char","字符型",
        "int","整型",
        "short","短整型",
        "long","长整型",
          "float","浮点型",
        "double","双精度浮点型",
        "union","共用体",
        "struct","结构体",
        "enum","枚举体",
        "void","无返回值或者无参或者无类型",
        };
keyword modifierkey[MODIFIERSIZE]={
        "short","短整型的",
        "long","长整型的",
        "signed","有符号的",
        "unsigned","无符号的",
        "auto","自动的",
        "extern","全局的",
        "static","静态的",
        "typedef","别名",
        };
char symbol[]={'(','[',')',']',',',' ','*'};
char exp_sequence[40];
char exp_store_sequence[40];
char exp_symbol_sequence[20];
char exp_keyword_sequence[20];
char exp_error_sequence[20][20];
char exp_defined_sequence[20][20];
char exp_modifier_sequence[20];
int  exp_num_sequence[20];
int  exp_sequence_flag=0;
int  exp_symbol_seq_flag=0;
int  exp_keyword_seq_flag=0;
int  exp_num_seq_flag=0;
int  exp_error_seq_flag=0;
int  exp_modifier_seq_flag=0;
int  exp_defined_seq_flag=0;
int  exp_store_seq_flag=0;
struct defined def;
int issymbol(char c)
{
    int i=0,flag=0;
    while(i<SYMBOLSIZE){
        if(c==symbol[i]){
            flag=i+1;
            break;
        }
        i++;
    }
            return flag;
}
int isdefined(char *str)
{
    int i=1,num,flag;
    flag=KEYSIZE>MODIFIERSIZE ? KEYSIZE:MODIFIERSIZE;
    if( ISCHARACTER ){
        while(*(str+i)){
            if(!ISCHAR_NUM_UNDERLINE){
                exp_sequence[exp_sequence_flag]=ERROR;
                exp_sequence_flag++;
                exp_sequence[exp_sequence_flag]=0;
                exp_sequence_flag++;
                return 0;
            }   
            i++;
        }
        for(i=0;i<flag;i++){
            if(i<KEYSIZE){
                    if(!strcmp(key[i].keyname,str)){
                    exp_sequence[exp_sequence_flag]=KEYWORD;
                    exp_sequence_flag++;
                    //exp_keyword_sequence[exp_keyword_seq_flag]=i;
                    //exp_keyword_seq_flag++;
                    exp_sequence[exp_sequence_flag]=i;
                    exp_sequence_flag++;
                    break;
                    }
            }
            if(i<MODIFIERSIZE){
                if(!strcmp(modifierkey[i].keyname,str)){
                    exp_sequence[exp_sequence_flag]=MODIFIER;
                    exp_sequence_flag++;
                    //exp_modifier_sequence[exp_modifier_seq_flag]=i;
                    //exp_modifier_seq_flag++;
                    exp_sequence[exp_sequence_flag]=i;
                    exp_sequence_flag++;
                  
                    break;
                    }
                  
            }
           
        }
        if(i==flag){           
            exp_sequence[exp_sequence_flag]=DEFINED;
            exp_sequence_flag++;
            strcpy(exp_defined_sequence[exp_defined_seq_flag],dealword);
            exp_sequence[exp_sequence_flag]=exp_defined_seq_flag;
            exp_sequence_flag++;
            exp_defined_seq_flag++;
        }   
    }
    else if(IS_NUM(*str)){
        num=*str-48;
        while(*(str+i)){
            if(!IS_NUM(*(str+i))){
               
                exp_sequence[exp_sequence_flag]=ERROR;
                exp_sequence_flag++;
                exp_sequence[exp_sequence_flag]=0;
                exp_sequence_flag++;
                return 0;
            }
            num*=10+(*(str+i)-48);   
            i++;
           
        }
        exp_sequence[exp_sequence_flag]=NUM;
        exp_sequence_flag++;
        exp_num_sequence[exp_num_seq_flag]=num;
        exp_sequence[exp_sequence_flag]=exp_num_seq_flag;
        exp_num_seq_flag++;
        exp_sequence_flag++;
      
    }
    else{
      
        exp_sequence[exp_sequence_flag]=ERROR;
        exp_sequence_flag++;
        exp_sequence[exp_sequence_flag]=0;
        exp_sequence_flag++;
        return 0;
    }   
    return 1;
}
int readword(char *str,int i)
{
        int n,j=0;
        if(*(str+i)=='\0')
            return 0;
        if((n=issymbol(*(str+i)))){
           
            if(*(str+i)!=' '){
                exp_sequence[exp_sequence_flag]=SYMBOL;
                exp_sequence_flag++;
                exp_symbol_sequence[exp_symbol_seq_flag]=n;
                exp_sequence[exp_sequence_flag]=n;
                exp_sequence_flag++;
                exp_symbol_seq_flag++;
               
            }      
            i++;
        }
        else{
            for(;*(str+i)!='\0';i++){
                if(!issymbol(*(str+i))){
                    dealword[j]=*(str+i);
                    j++;
                }
                else{
                    dealword[j]='\0';
                         break;
                }
            }   
            if(!isdefined(dealword)){
                strcpy(exp_error_sequence[exp_error_seq_flag],dealword);
                exp_error_seq_flag++;
            }
            memset(dealword,0,100);
        }
      
        readword(str,i);
   
}
int symbolmatching()
{
    int flag1=0,flag2=0,i=0,error_flag=0;
    for(;i<exp_symbol_seq_flag;i++){
        if(exp_symbol_sequence[i]==1)
            flag1++;
        else if(exp_symbol_sequence[i]==3)
            flag1--;
        else if(exp_symbol_sequence[i]==2)
            flag2++;
        else    if(exp_symbol_sequence[i]==4)
            flag2--;
        else
            ;
        if(flag1<0||flag2<0 \
           ||(exp_symbol_sequence[i]==1&&exp_symbol_sequence[i+1]==4) \
           ||(exp_symbol_sequence[i]==2&&exp_symbol_sequence[i+1]==3)){
            printf("符号不匹配");
            return 0;
        }
    }
    if(flag1>0||flag2>0){
            printf("符号号不匹配");
            return 0;
        }
   
   
    return 1;
   
}
int have_no_error()
{
     int i,j=0,flag=0;
     for(i=0;i<exp_sequence_flag;i+=2){
           if(exp_sequence[i]==ERROR){
                    printf("[%s]:表达式错误 ",exp_error_sequence[j]);
                 j++;
            }   
        if(exp_sequence[i]==DEFINED)   
             flag=1;
    }
    if(flag==0)
        printf("表达式中没有标识符\n");
    if(j==0&&flag==1)
        return 1;
    else
        return 0;
      
}
/*int exp_segment(char *exp,int size,char *segment)
{
    int i,j=0,bracket_flag=1;
    for(i=0;i<size;i+=2){
        if(IS_LEFT_BRACKET)
            bracket_flag++;
        if(IS_RIGHT_BRACKET)
            bracket_flag--;
        if(bracket_flag==0)
            break;
        segment[j++]=exp[i];
        segment[j++]=exp[i+1];
    }
   
   
}*/
int syntax_simple(char *exp,int size)
{
    int i=0,j=0,asterisk_num=0,n=0,bracket_flag,n1,n2,z;
    char expsegment[40],func_list[40];
    for(;i<size;i+=2){
        if(IS_LEFT_BRACKET(i)){
                bracket_flag=1;j=0;
                for(i+=2;i<size;i+=2){
                    if(IS_LEFT_BRACKET(i))
                        bracket_flag++;
                    if(IS_RIGHT_BRACKET)
                        bracket_flag--;
                    if(bracket_flag==0)
                        break;
               
                    expsegment[j++]=exp[i];
                    expsegment[j++]=exp[i+1];
                }
                if(   exp_store_seq_flag>=2&&
                       (exp_store_sequence[exp_store_seq_flag-2]==VAR_TYPE||
                        exp_store_sequence[exp_store_seq_flag-2]==POINT_TYPE||
                          exp_store_sequence[exp_store_seq_flag-2]==ARR_TYPE
                            )
                      ){
                    if(j==0&&(i+4)<size){
                            printf("解析出是个函数,但是函数列表后存有不名符号\n");
                            PUSH(ERROR,0);
                            return  0;
                        }
                    n2=exp_store_seq_flag;
                    PUSH(FUNC_TYPE,0);
                    while(n<j){
                        n1=0;
                        for(;n<j;n+=2){
                            if(IS_COMMA(expsegment,n))
                                break;
                            func_list[n1++]=expsegment[n];
                            func_list[n1++]=expsegment[n+1];
                        }   
                        syntax_simple(func_list,n1);
                        if(n!=j){
                            //printf("xad%d %d\n",n,j);
                            PUSH(SYMBOL,5);
                        }      
                        exp_store_sequence[n2+1]++;
                        n+=2;
                       
                    }
                  
                           
                  }
                else{
                    if(j==0){
                        printf("表达式错误:存在空括号\n");
                        PUSH(ERROR,0);
                        return 0;
                    }
                    syntax_simple(expsegment,j);
                    memset(expsegment,0,40);
                }
            }
        else{
            if(exp[i]==DEFINED){
                if(IS_ARRARY){
                    if(IS_ASTERISK_IN_STORE(exp_store_seq_flag-2)){
                        asterisk_num++;
                        for(exp_store_seq_flag-=2;exp_store_seq_flag>=0;exp_store_seq_flag-=2)
                                if(IS_ASTERISK_IN_STORE(exp_store_seq_flag-2))
                                    asterisk_num++;
                                else
                                    break;
                        PUSH(exp[i],exp[i+1]);
                        PUSH(POINT_TYPE,asterisk_num);
                        PUSH(ARR_TYPE,exp[i+5]);   
                    }
                    else{
                        PUSH(exp[i],exp[i+1]);
                        PUSH(ARR_TYPE,exp[i+5]);
                    }
                    i+=6;
                }
                else if(IS_VARIABLE(i+2)){
                    if(IS_ASTERISK_IN_STORE(exp_store_seq_flag-2)){
                        asterisk_num++;
                        for(exp_store_seq_flag-=2;exp_store_seq_flag>=0;exp_store_seq_flag-=2)
                                if(IS_ASTERISK_IN_STORE(exp_store_seq_flag-2))
                                    asterisk_num++;
                                else
                                    break;
                        PUSH(exp[i],exp[i+1]);
                        PUSH(POINT_TYPE,asterisk_num);
                        PUSH(VAR_TYPE,0);   
                    }
                    else{
                        PUSH(exp[i],exp[i+1]);
                        PUSH(VAR_TYPE,0);
                    }
                       
                }
                else{
                    PUSH(ERROR,0);
                    printf("错误\n");
                    return 0;
                }
                  
            }
            else{
                PUSH(exp[i],exp[i+1]);
            }
      
           
        }
      
    }
}
/*int no_error_for_syntax()
{
    int i=0,flag;
    for(;i<exp_store_seq_flag;i+=2){
        if(exp_store_sequence[i]==ERROR)
            return 0;
    }
    for(i=2;i<exp_store_seq_flag;i+=2){
        //if(exp_store_sequence[i]==exp_store_sequence[i-2]){
        //    printf("存在两个以上相同类型的符号\n");
        //    return 0;
        //}
        if(exp_store_sequence[i]=[i]==DEFINED&&exp_store_sequence[i-2]!=KEYWORD){
            printf("标识符类型未知\n");
            return 0;
        }
      
    }
    return 1;
   

}*/
int main(int argc,char **argv)
{
    int i,j=0;
    char *str="unsigned int   A(char *a,char *b[20])";
    readword(str,0);   
    if(have_no_error()&&symbolmatching()){
        syntax_simple(exp_sequence,exp_sequence_flag);   
    }
    for(i=0;i<exp_store_seq_flag;i+=2){
         printf("%d.",i/2);
         if(exp_store_sequence[i]==KEYWORD)
            printf("关键字  :%s \n",key[exp_store_sequence[i+1]].keyname);
        else if(exp_store_sequence[i]==DEFINED)
            printf("标识符名:%s\n",exp_defined_sequence[exp_store_sequence[i+1]]);
        else if(exp_store_sequence[i]==ARR_TYPE)
            printf("数组:%d\n",exp_num_sequence[exp_store_sequence[i+1]]);
        else if(exp_store_sequence[i]==VAR_TYPE)
            printf("变量\n");
        else if(exp_store_sequence[i]==POINT_TYPE)
            printf("指针\n");
        else if(exp_store_sequence[i]==FUNC_TYPE)
            printf("函数\n函数列表如下:\n第一个参数为:\n");
        else if(exp_store_sequence[i]==ERROR)
            printf("这个地方有错误\n");
        else if(exp_store_sequence[i]==MODIFIER)
            printf("修饰词  :%s\n",modifierkey[exp_store_sequence[i+1]].keyname);
        else    if(exp_store_sequence[i]==SYMBOL&&exp_store_sequence[i+1]==5)
            printf("下一个参数是:\n");
        else
            printf("我也不知道是什么\n");
      
      
    }
   
   
}
  


[ 本帖最后由 朱大哥 于 2015-1-31 03:27 编辑 ]
2015-01-31 03:21
快速回复:小小码屋
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.045747 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved