现在的位置: 首页 > 综合 > 正文

C语言声明解析

2013年10月06日 ⁄ 综合 ⁄ 共 4260字 ⁄ 字号 评论关闭

/*  type.h  */

 

#ifndef TTYPE_Z_W
#define TTYPE_Z_W

#define LEN (16)

enum type_tag {IDEN,QUAL,TYPE};     //分别是:标识符、限定符、类型

typedef struct node
{
    char *string;
    struct node *next;
    char type;
} token;

typedef struct
{
    token *next;
} poin;

token *creat(void);
int   push(poin *,poin *);
int   pop(poin *);
enum type_tag classify_string(poin *);
int gettoken(poin *p);
int read_to_first_identifer(poin *n,poin *p);
int deal_with_arrays(poin *p);
int deal_with_func(poin *p);
int deal_with_pointers(poin *n);
int deal_with_declarator(poin *n,poin *p);

#endif

 

/*  type.c  */

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "type.h"

token *creat(void)
{
    token *s;
    if((s=(token *)malloc(sizeof(token)))==NULL)
    {
        fprintf(stderr,"%s/n","结构体内存分配失败");
        return NULL;
    }
    if((s->string=(char *)malloc(LEN*sizeof(char)))==NULL)
        fprintf(stderr,"%s/n","字符数组内存分配失败");
    s->type=3;
    s->next=NULL;
    return s;
}
int push(poin *n,poin *p)
{
    p->next->next=n->next;
    n->next=p->next;
    return 1;
}
int pop(poin *n)
{
    token *temp;
    temp=n->next;
    n->next=temp->next;
    free(temp->string);
    free(temp);
    return 1;
}
enum type_tag classify_string(poin *p)
{
    char *s=p->next->string;
    if(!strcmp(s,"const"))//if(s==const)
    {
        strcpy(s,"read-only");
        return QUAL;     
    }
    if(!strcmp(s,"volatile")) return  QUAL;
    if(!strcmp(s,"void")) return  TYPE;
    if(!strcmp(s,"char")) return  TYPE;
    if(!strcmp(s,"signed")) return  TYPE;
    if(!strcmp(s,"unsigned")) return  TYPE;
    if(!strcmp(s,"short")) return  TYPE;
    if(!strcmp(s,"int")) return  TYPE;
    if(!strcmp(s,"long")) return  TYPE;
    if(!strcmp(s,"float")) return  TYPE;
    if(!strcmp(s,"double")) return  TYPE;
    if(!strcmp(s,"struct")) return  TYPE;
    if(!strcmp(s,"union")) return  TYPE;
    if(!strcmp(s,"enum")) return  TYPE;
    return IDEN;
}
int gettoken(poin *p)
{
    int n=0,m=LEN;
    char *str=p->next->string;
    while((*str=getchar())==' ');    
    if(isalnum(*str)||*str=='_')     
    {
        while(n++,(*++str=getchar())=='_'||isalnum(*str))
        {
            if(n>=m-2)
            {
                m+=LEN;
                p->next->string=(char *)realloc(p->next->string,m*sizeof(char));
                if(p->next->string==NULL)
                {
                    fprintf(stderr,"%s/n","内存扩展失败");
                    return -1;
                }
                str=p->next->string+n;
            }
        }
        ungetc(*str,stdin);
        *str='/0';
        if(n<m-2)
            p->next->string=(char *)realloc(p->next->string,(n+1)*sizeof(char));
        p->next->type=classify_string(p);
        return 1;
    }
    if(*str=='*')
        strcpy(p->next->string,"pointer to");
    else
        p->next->string[1]='/0';
    p->next->type=*str;
    p->next->string=(char *)realloc(p->next->string,strlen(p->next->string)*sizeof(char));
    return 1;
}
int read_to_first_identifer(poin *n,poin *p)
{
    gettoken(p);
    while(p->next->type!=IDEN) 

    {
        push(n,p);
        p->next=creat();
        gettoken(p);
    }
    printf("%s is ",p->next->string);
    gettoken(p);
    return 1;
}
int deal_with_arrays(poin *p)
{
    while(p->next->type=='[')
    {
        printf("array ");
        gettoken(p);
        if(isdigit(p->next->string[0])) //
        {
            printf("0...%d ",atoi(p->next->string)-1);
            gettoken(p);             //  

       }
        gettoken(p);                 //
        printf("of ");
    }
    return 1;
}
int deal_with_func(poin *p) //
{
    while(p->next->type!=')')
    {
        gettoken(p);
    }
    gettoken(p);
    printf("function returning ");//
    return 1;
}
int deal_with_pointers(poin *n)//
{
    while(n->next->type=='*')
    {
        printf("%s",n->next->string);     //
        pop(n);
    }

    return 1;
}
int deal_with_declarator(poin *n,poin *p)//

{
    switch(p->next->type)
    {
    case '[':
        deal_with_arrays(p);
        break;
    case '(':
        deal_with_func(p);
    }
    deal_with_pointers(n);
    while(n->next!=NULL)
    {
        if(n->next->type=='(')
        {
            pop(n);
            gettoken(p);
            deal_with_declarator(n,p);
        }
        else
        {
            printf("%s ",n->next->string);
            pop(n);
        }
    }
    return 1;
}

/* main.c */

 

#include <stdio.h>
#include <stdlib.h>
#include "type.h"

int main(void)
{
    poin n= {NULL},p;
    p.next=creat();
    read_to_first_identifer(&n,&p);
    deal_with_declarator(&n,&p);
    while(n.next!=NULL)
        pop(&n);
    if(p.next!=NULL)
    {
        free(p.next->string);
        free(p.next);
    }
    printf("/n");
    return 0;
}

抱歉!评论已关闭.