| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1282 人关注过本帖
标题:小小码屋
取消只看楼主 加入收藏
朱大哥
Rank: 3Rank: 3
等 级:论坛游侠
威 望:2
帖 子:72
专家分:123
注 册:2015-1-16
结帖率:22.22%
收藏
已结贴  问题点数:10 回复次数:11 
小小码屋
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define KEYSIZE 17
#define SYMBILESIZE 6
#define KEYWORLD 1
#define DEFINED  2
#define SYMBOL   3
#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)=='_'))
//typedef struct{
//    char definedname[10];
//    int  dataspecies;
//    int  pointspecies;
   
//}
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[]={'[',']','(',')',',',' '};
int issymbol(char c)
{
    int i=0,flag=0;
    while(i<SYMBILESIZE){
        if(c==symbol[i]){
            flag=i;
            break;
        }
        i++;
    }
            return flag;
}
int isdefined(char *str)
{
    int i=1;
    if( ISCHARACTER ){
        while(*(str+i)){
            if(!ISCHAR_NUM_UNDERLINE){
                printf("%s:标识符不合法\n",str);
                return 0;
            }   
            i++;
        }
        for(i=0;i<KEYSIZE;i++)
            if(!strcmp(key[i].keyname,str)){
                printf("%s:这是个关键字:%s\n",key[i].keyname,key[i].keyexp);
                break;
            }
        if(i==KEYSIZE)           
          printf("%s:这是个标识符\n",str);   
    }
    else{
        printf("%s:标识符不合法\n",str);
        return 0;
    }   
}
int readword(char *str,int i)
{
        int n,j=0;
        if(*(str+i)=='\0')
            return 0;
   
        if((n=issymbol(*(str+i)))){
            i++;
        }
        else{
            for(;*(str+i)!='\0';i++){
                if(!issymbol(*(str+i))){
                    dealword[j]=*(str+i);
                    j++;
                }
                else{
                  
                     break;
                }
            }   
            isdefined(dealword);
            memset(dealword,0,100);
        }
      
        readword(str,i);
   
}
int main(int argc,char **argv)
{
    char *str="long fast bbc ok 999,union,oo34*@&#^!ok,bcnbads9";
    readword(str,0);        
}
 



[ 本帖最后由 朱大哥 于 2015-1-28 16:59 编辑 ]
搜索更多相关主题的帖子: include 
2015-01-27 22:09
朱大哥
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 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{
//    char definedname[10];
//    int  dataspecies;
//    int  pointspecies;
   
//}
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;
int issymbol(char c)
{
    int i=0,flag=0;
    while(i<SYMBOLSIZE){
        if(c==symbol[i]){
            flag=i;
            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++;
        }   
    }
    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 main(int argc,char **argv)
{
    int i,j=0;
    char *str="long 10a,bbc7,$adqw";
    readword(str,0);   
    for(i=0;i<exp_sequence_flag;i++)
        if(exp_sequence[i]==SYMBOL)
            printf("符号 ");
        else if(exp_sequence[i]==DEFINED)
            printf("标识符 ");
        else if(exp_sequence[i]==ERROR){
            printf("[%s]:表达式错误 ",exp_error_sequence[j]);
            j++;
        }
        else if(exp_sequence[i]==NUM)
            printf("数字 ");
        else
            printf("关键字 ");
    printf("\n");
}


[ 本帖最后由 朱大哥 于 2015-1-28 03:38 编辑 ]
2015-01-28 03:21
朱大哥
Rank: 3Rank: 3
等 级:论坛游侠
威 望:2
帖 子:72
专家分:123
注 册:2015-1-16
收藏
得分:0 
回复 2楼 诸葛欧阳
你拿过去编译一下不就晓得是干嘛了嘛 这么懒
2015-01-28 03:45
朱大哥
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 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{
//    char definedname[10];
//    int  dataspecies;
//    int  pointspecies;
   
//}
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;
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++;
        }   
    }
    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 main(int argc,char **argv)
{
    int i,j=0;
    char *str="[(])";
    readword(str,0);   
    for(i=0;i<exp_sequence_flag;i++)
        if(exp_sequence[i]==SYMBOL)
            printf("符号 ");
        else if(exp_sequence[i]==DEFINED)
            printf("标识符 ");
        else if(exp_sequence[i]==ERROR){
            printf("[%s]:表达式错误 ",exp_error_sequence[j]);
            j++;
        }
        else if(exp_sequence[i]==NUM)
            printf("数字 ");
        else
            printf("关键字 ");
    if(!symbolmatching())
        printf("括号不匹配");
      
    printf("\n");
   
}


[ 本帖最后由 朱大哥 于 2015-1-28 16:29 编辑 ]
2015-01-28 16:11
朱大哥
Rank: 3Rank: 3
等 级:论坛游侠
威 望:2
帖 子:72
专家分:123
注 册:2015-1-16
收藏
得分:0 
回复 5楼 砖家的谎言
因为我的操作系统是双系统 linux+win7
不知道为什么 我打代码喜欢在linux下面  打游戏看黄片又喜欢在win7里面
所以问题就来了 :在linux下我可以看代码 写代码
               但是在win7里面就不行了,因为vs6.0装不上,VS太大 其他的又用不来
               咋办呢 ,我想在win7下看到代码,并且思索代码,打草稿
               我的办法是:坛子里放个帖子,存起来。
               这样的好处有如下:
               操作系统重新装了,东西不怕掉
               翻阅查找方便
               
               至于访客 看与不看 其实没多大关系
               
            
2015-01-28 16:39
朱大哥
Rank: 3Rank: 3
等 级:论坛游侠
威 望:2
帖 子:72
专家分:123
注 册:2015-1-16
收藏
得分:0 
回复 9楼 zklhp
我用vim啊,写点编译相关的
帖子可以回帖
我一点一点的堆代码
堆点存点
真的堆榻了,这不原始资料都在
2015-01-28 16:55
朱大哥
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: 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
快速回复:小小码屋
数据加载中...
 
   



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

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