0

いくつか質問があるので、質問された行が見つけやすいようにコメントに数字を入れます。

[1] 実際には存在しないトークン変数に char *p を割り当てるにはどうすればよいですか?

[2] ここに '\0' を入れないのはなぜですか? 他のすべての if 条件では何が行われますか?

[3] () をトークン文字列のみにコピーするのはなぜですか? [] と英数字の場合はそうしませんか?

[4] これらの return コマンドは奇妙な IMO です -> 最初に: なぜこの return PARENS のように見えないのか、2 つ目: tokentype = '(' を返すとき、それは char であるのに、関数 gettoken が整数を返すと宣言されているのはなぜですか?

[5] SUPPOSING part: let input be ( abc ) then: ( function to return tokentype '(' abc enter if condition (isalpha(C)) and the last ) exit that condition due to unetch. メインの else 条件に沿っていますか?私のウォークスルーは正しいですか?

#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define MAXTOKEN 100
enum { NAME, PARENS, BRACKETS };
void dcl(void);
void dirdcl(void);
int gettoken(void);
int tokentype; /*type of last token  ALSO [4] !!! */
char token[MAXTOKEN]; /*last token string */
char name[MAXTOKEN]; /*identifier name */
char datatype[MAXTOKEN]; /*data type = char, int, etc. */
char out[1000];

main() /* convert declaration to words */
{
    while (gettoken() != EOF) {    /* 1st token on line */
        strcpy(datatype, token);   /* is the datatype */
        out[0] = '\0';
        dcl();            /* parse rest of line */
        if (tokentype != '\n')
            printf("syntax error\n");
        printf("%s: %s %s\n", name, out, datatype);
    }
    return 0;
}

int gettoken(void) /* return next token */
{
    int c, getch(void);
    void ungetch(int);
    char *p = token;        /* [1] */
    while ((c = getch()) == ' ' || c == '\t')
        ;
    if (c == '(') {
        if ((c = getch()) == ')') {
            strcpy(token, "()");            /* [2][3] */
            return tokentype = PARENS;           /* [4] */
        } else {
            ungetch(c);
            return tokentype = '(';
        }
    } else if (c == '[') {
        for (*p++ = c; (*p++ = getch()) != ']'; )
            ;
        *p = '\0';
        return tokentype = BRACKETS;
    } else if (isalpha(c)) {
        for (*p++ = c; isalnum(c = getch()); ) /* SUPPOSING [5] */
            *p++ = c;
        *p = '\0';
        ungetch(c);
        return tokentype = NAME;
    } else
        return tokentype = c;
}

/* dcl: parse a declarator */
void dcl(void)
{
    int ns;
    for (ns = 0; gettoken() == '*'; ) /* count *'s */
        ns++;
    dirdcl();
    while (ns-- > 0)
        strcat(out, " pointer to");
}

/* dirdcl: parse a direct declarator */
void dirdcl(void)
{
    int type;
    if (tokentype == '(') {
        dcl();
        if (tokentype != ')')
            printf("error: missing )\n");
    } else if (tokentype == NAME) /* variable name */
        strcpy(name, token);
    else
        printf("error: expected name or (dcl)\n");
    while ((type=gettoken()) == PARENS || type == BRACKETS)
        if (type == PARENS)
            strcat(out, " function returning");
        else {
            strcat(out, " array");
            strcat(out, token);
            strcat(out, " of");
        }
}

前もって感謝します!

4

1 に答える 1

2

1)token存在しますが、これは次のように定義されたグローバル変数です。char token[MAXTOKEN];

2)strcpy()終了する0バイトをソースからコピーするため、手動で行う必要はありません

3)これはリテラル文字列の特殊なケース処理のように見えます()-いくつかの括弧は、私たちが持っているケースを処理するのとは対照的に、間に何もありません( some stuff )

4)(3)によれば、PARENSは空の括弧のセットのトークンタイプのように見えますが、それらの間に何かがある場合は特定のsが当てはまるため、(個別)に返されます。tokentype

5)あなたが求めていることに従うかどうかはわかりませんが、閉じ括弧の特別なケースはないように見えるので、最後のelse分岐を取り、tokentype = c

于 2012-08-16T19:41:07.863 に答える