标题:请问结构体的this怎么用??
只看楼主
stophin
Rank: 7Rank: 7Rank: 7
等 级:黑侠
威 望:3
帖 子:227
专家分:618
注 册:2010-3-26
得分:0 
谢谢楼上各位的回复,今天我把所有this去掉了s变成thi后编译成功并且运行成功。一个类似于自动机的程序,可以成为编译器的一部分,附上源代码,,额额额~~~另外还请各位高人看看这个this(也就是thi)究竟是做什么的?
程序代码:
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <string>
using namespace std;
#define MAXTOKENS 100
#define MAXTOKENLEN 64
enum type_tag { IDENTIFIER, QUALIFIER, TYPE };
struct token
{
    char type;
    char string[MAXTOKENLEN];
};
int top = -1;
struct token stack[MAXTOKENS];
struct token thi;
#define pop stack[top--]
#define push(s) stack[++top] = s
enum type_tag classify_string (void)
/*推断标识符类型*/
{
    char *s = thi.string;
    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, "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 IDENTIFIER;
}
void gettoken (void)
{
    char *p = thi.string;
    /*略过空白字符*/
    while ((*p = getchar()) == ' ');

    if (isalnum(*p))
    {
          /*读入的标识符以A-Z、0-9开头*/
          while (isalnum(*++p = getchar()));
          ungetc (*p, stdin);
          *p = '\0';
          thi.type = classify_string ();
          return;
    }
    if (*p == '*')
    {
        strcpy (thi.string, "pointer to ");
        thi.type = '*';
        return;
    }
    thi.string[1] = '\0';
    thi.type = *p;
    return;
}
/*理解所有分析过程的代码段*/
void read_to_first_identifer ()
{
    gettoken();
    while (thi.type != IDENTIFIER)
    {
        push(thi);
        gettoken();
    }
    printf("%s is ", thi.string);
    gettoken();
}
void deal_with_arrays ()
{
    while (thi.type == '[')
    {
        printf("array ");
        gettoken();/*数字或']'*/
        if (isdigit (thi.string[0]))
        {
            printf("0..%d ", atoi(thi.string)-1);
            gettoken();/*读取']'*/
        }
        gettoken();/*读取']'之后的再一个标记*/
        printf("of ");
    }
}
void deal_with_function_args ()
{
    while (thi.type != ')')
    {
        gettoken();
    }
    gettoken();
    printf("function returning ");
}
void deal_with_pointers ()
{
    while (stack[top].type == '*')
    {
        printf("%s ",pop.string);
    }
}
void deal_with_declarator()
{
    /*处理标识符之后可能存在的数组或函数*/
    switch(thi.type)
    {
        case '[':
            deal_with_arrays();
            break;
        case '(':
            deal_with_function_args();
            break;
    }
    deal_with_pointers();
    /*处理在读入到标识符之前压入到堆栈中的符号*/
    while(top >= 0)
    {
        if (stack[top].type == '(')
        {
            pop;
            gettoken();/*读取')'之后的符号*/
            deal_with_declarator();
        }
        else
        {
            printf("%s ",pop.string);
        }
    }
}
int main()
{
    /*将标记压入到堆栈中,直到遇见标识符*/
    read_to_first_identifer();
    deal_with_declarator();
    printf("\n");
    getch();
    return 0;
}

 

[ 本帖最后由 stophin 于 2011-9-17 23:17 编辑 ]
2011-09-17 23:12



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-350115-1-1.html




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

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