溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

C++用來檢測數(shù)據(jù)類型的聲明工具源碼

發(fā)布時間:2020-06-25 14:26:36 來源:網(wǎng)絡(luò) 閱讀:418 作者:weweishen 欄目:移動開發(fā)

代碼期間,把代碼過程中較好的代碼片段做個珍藏,如下的代碼內(nèi)容是關(guān)于C++用來檢測數(shù)據(jù)類型的聲明工具的代碼,應(yīng)該是對碼農(nóng)有較大用。

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#define MAXTOKENS 100
#define MAXTOKENLEN 64

struct token{
char type;
char string[MAXTOKENLEN];
};

int top = -1;
struct token stack[MAXTOKENS];
struct token this;

#define pop stack[top--]
#define push(s) stack[++top] = s

#define STRCMP(a,R,b) (strcmp(a,b) R 0)

enum type_tag classify_string(void)
{
if (STRCMP(s,==,"const"))
{
strcpy(s,"read-only");
return QUALIFIER;
}
if (STRCMP(s,==,"volatile")) return QUALIFIER;
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,==,"double")) return TYPE;
if (STRCMP(s,==,"float")) return TYPE;
if (STRCMP(s,==,"struct")) return TYPE;
if (STRCMP(s,==,"union")) return TYPE;
if (STRCMP(s,==,"enum")) return TYPE;
return IDENTIFIER;
}

void gettoken(void)
{

{
    this.type = (char)classify_string();
    return;
}

{
    strcpy(this.string,"pointer to");
    return;
}

this.string[1] = '';
return;

}

int read_to_first_identifier(void)
{
{
push(this);
gettoken();
}

printf("%s is ",this.string);
return 0;

}

int deal_with_arrays(void)
{
while(this.type == '[')
{
printf("array ");
if (isdigit(this.string[0]))
{
printf("0..%d ",atoi(this.string)-1);
}

    printf("of ");
}

return 0;

}

int deal_with_function_args(void)
{
{
gettoken();
}

printf("function returning ");
return 0;

}

int deal_with_pointers(void)
{
{
printf("%s ",pop.string);
}
return 0;
}

int deal_with_declarator(void)
{
switch(this.type)
{
case '[' : deal_with_arrays();break;
case '(' : deal_with_function_args();break;
default : break;
}

while(top >= 0)
{
    if (stack[top].type == '(')
    {
        pop;
    }
    else
    {
        printf("%s ",pop.string);
    }
}

return 0;

}

int main(void)
{
read_to_first_identifier();
deal_with_declarator();
printf("n");

return 0;

}

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI