⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scanner.c

📁 cg编译器
💻 C
📖 第 1 页 / 共 3 页
字号:
            } else if (ch >= '0' && ch <= '7') { // octal integer constants
                AlreadyComplained = 0;
                ival = 0;
                do {
                    if (ival <= 0x1fffffff) {
                        ii = ch - '0';
                        ival = (ival << 3) | ii;
                    } else {
                        if (!AlreadyComplained)
                            SemanticError(Cg->tokenLoc, ERROR___OCT_CONST_OVERFLOW);
                        AlreadyComplained = 1;
                    }
                    ch = Cg->currentInput->getch(Cg->currentInput);
                } while (ch >= '0' && ch <= '7');
                Cg->currentInput->ungetch(Cg->currentInput, ch);
                yylval.sc_int = ival;
                return INTCONST_SY;
            } else {
                Cg->currentInput->ungetch(Cg->currentInput, ch);
                ch = '0';
            }
            // Fall through...
        case '1': case '2': case '3': case '4':
        case '5': case '6': case '7': case '8': case '9':
            len = 0;
            do {
                if (len < MAX_SYMBOL_NAME_LEN) {
                    if (len > 0 || ch != '0') {
                        symbol_name[len] = ch;
                        len++;
                    }
                    ch = Cg->currentInput->getch(Cg->currentInput);
                }
            } while (ch >= '0' && ch <= '9');
            if (ch == '.' || ch == 'e' || ch == 'f' || ch == 'h' || ch == 'x') {
                return lFloatConst(symbol_name, len, ch);
            } else {
                Cg->currentInput->ungetch(Cg->currentInput, ch);
                ival = 0;
                AlreadyComplained = 0;
                for (ii = 0; ii < len; ii++) {
                    ch = symbol_name[ii] - '0';
                    if (ival > 214748364 || ival == 214748364 && ch >= 8) {
                        if (!AlreadyComplained)
                            SemanticError(Cg->tokenLoc, ERROR___INTEGER_CONST_OVERFLOW);
                        AlreadyComplained = 1;
                    }
                    ival = ival*10 + ch;
                }
                yylval.sc_int = ival;
                return INTCONST_SY;
            }
            break;
        case '-':
            ch = Cg->currentInput->getch(Cg->currentInput);
            if (ch == '-') {
                return MINUSMINUS_SY;
            } else if (ch == '=') {
                return ASSIGNMINUS_SY;
            } else {
                Cg->currentInput->ungetch(Cg->currentInput, ch);
                return '-';
            }
        case '+':
            ch = Cg->currentInput->getch(Cg->currentInput);
            if (ch == '+') {
                return PLUSPLUS_SY;
            } else if (ch == '=') {
                return ASSIGNPLUS_SY;
            } else {
                Cg->currentInput->ungetch(Cg->currentInput, ch);
                return '+';
            }
        case '*':
            ch = Cg->currentInput->getch(Cg->currentInput);
            if (ch == '=') {
                return ASSIGNSTAR_SY;
            } else {
                Cg->currentInput->ungetch(Cg->currentInput, ch);
                return '*';
            }
        case '%':
            ch = Cg->currentInput->getch(Cg->currentInput);
            if (ch == '=') {
                return ASSIGNMOD_SY;
            } else {
                Cg->currentInput->ungetch(Cg->currentInput, ch);
                return '%';
            }
        case ':':
            ch = Cg->currentInput->getch(Cg->currentInput);
            if (ch == ':') {
                return COLONCOLON_SY;
            } else {
                Cg->currentInput->ungetch(Cg->currentInput, ch);
                return ':';
            }
        case '=':
            ch = Cg->currentInput->getch(Cg->currentInput);
            if (ch == '=') {
                return EQ_SY;
            } else {
                Cg->currentInput->ungetch(Cg->currentInput, ch);
                return '=';
            }
        case '!':
            ch = Cg->currentInput->getch(Cg->currentInput);
            if (ch == '=') {
                return NE_SY;
            } else {
                Cg->currentInput->ungetch(Cg->currentInput, ch);
                return '!';
            }
        case '|':
            ch = Cg->currentInput->getch(Cg->currentInput);
            if (ch == '|') {
                return OR_SY;
            } else {
                Cg->currentInput->ungetch(Cg->currentInput, ch);
                return '|';
            }
        case '&':
            ch = Cg->currentInput->getch(Cg->currentInput);
            if (ch == '&') {
                return AND_SY;
            } else {
                Cg->currentInput->ungetch(Cg->currentInput, ch);
                return '&';
            }
        case '<':
            ch = Cg->currentInput->getch(Cg->currentInput);
            if (ch == '<') {
                return LL_SY;
            } else {
                if (ch == '=') {
                    return LE_SY;
                } else {
                    Cg->currentInput->ungetch(Cg->currentInput, ch);
                    return '<';
                }
            }
        case '>':
            ch = Cg->currentInput->getch(Cg->currentInput);
            if (ch == '>') {
                return GG_SY;
            } else {
                if (ch == '=') {
                    return GE_SY;
                } else {
                    Cg->currentInput->ungetch(Cg->currentInput, ch);
                    return '>';
                }
            }
        case '.':
            ch = Cg->currentInput->getch(Cg->currentInput);
            if (ch >= '0' && ch <= '9') {
                Cg->currentInput->ungetch(Cg->currentInput, ch);
                return lFloatConst(symbol_name, 0, '.');
            } else {
                if (ch == '.') {
                    return -1; // Special EOF hack
                } else {
                    Cg->currentInput->ungetch(Cg->currentInput, ch);
                    return '.';
                }
            }
        case '/':
            ch = Cg->currentInput->getch(Cg->currentInput);
            if (ch == '/') {
                do {
                    ch = Cg->currentInput->getch(Cg->currentInput);
                } while (ch != '\n' && ch != EOF);
                if (ch == EOF)
                    return -1;
                return '\n';
            } else if (ch == '*') {
                int nlcount = 0;
                ch = Cg->currentInput->getch(Cg->currentInput);
                do {
                    while (ch != '*') {
                        if (ch == '\n') nlcount++;
                        if (ch == EOF) {
                            SemanticError(Cg->tokenLoc, ERROR___EOF_IN_COMMENT);
                            return -1;
                        }
                        ch = Cg->currentInput->getch(Cg->currentInput);
                    }
                    ch = Cg->currentInput->getch(Cg->currentInput);
                    if (ch == EOF) {
                        SemanticError(Cg->tokenLoc, ERROR___EOF_IN_COMMENT);
                        return -1;
                    }
                } while (ch != '/');
                if (nlcount) {
                    return '\n';
                }
                // Go try it again...
            } else if (ch == '=') {
                return ASSIGNSLASH_SY;
            } else {
                Cg->currentInput->ungetch(Cg->currentInput, ch);
                return '/';
            }
            break;
        case '"':
            len = 0;
            ch = Cg->currentInput->getch(Cg->currentInput);
            while (ch != '"' && ch != '\n' && ch != EOF) {
                if (ch == '\\') {
                    ch = Cg->currentInput->getch(Cg->currentInput);
                    if (ch == '\n' || ch == EOF) {
                        break;
                    }
                }
                if (len < MAX_STRING_LEN) {
                    string_val[len] = ch;
                    len++;
                    ch = Cg->currentInput->getch(Cg->currentInput);
                }
            };
            string_val[len] = '\0';
            if (ch == '"') {
                yylval.sc_ident = LookUpAddString(atable, string_val);
                return STRCONST_SY;
            } else {
                SemanticError(Cg->tokenLoc, ERROR___CPP_EOL_IN_STRING);
                return ERROR_SY;
            }
        }
    }
} // byte_scan

int scan_include_name()
{
    char buf[MAX_STRING_LEN + 1];
    int len, ch;

    if (!Cg->currentInput->getch) return 0;
    len = 0;
    while ((ch = Cg->currentInput->getch(Cg->currentInput)) > 0 &&
           ch != '\n' && ch != '>'
    ) {
        if (len < MAX_STRING_LEN)
            buf[len++] = ch;
    }
    buf[len] = 0;
    if (ch == '\n') Cg->currentInput->ungetch(Cg->currentInput, ch);
    return LookUpAddString(atable, buf);
} // scan_include_name;

int yylex(void)
{
    static int last_token = '\n';
    int token;

    for(;;) {
        token = Cg->currentInput->scan(Cg->currentInput);

        if (token == '#' && last_token == '\n') {
            readCPPline();
            continue;
        }
        last_token = token;

        // expand macros
        if (token == IDENT_SY && MacroExpand(yylval.sc_ident))
            continue;

        // convert IDENTs to reserved words or TYPEIDENT as appropriate
        if (token == IDENT_SY) {
            Cg->mostRecentToken = yylval.sc_ident;
            if (yylval.sc_ident >= FIRST_USER_TOKEN_SY) {
                Symbol *pSymb = LookUpSymbol(NULL, yylval.sc_ident);
                if (pSymb && IsTypedef(pSymb))
                    token = TYPEIDENT_SY;
            } else {
                token = yylval.sc_ident;
            }
        } else {
            Cg->mostRecentToken = token;
        }

        if (Cg->options.TraceScanner) {
            if (token >= 127)
                printf("token = %s", GetAtomString(atable, token));
            else if (token >= 32)
                printf("token = <%c>", token);
            else if (token == '\n')
                printf("token = <\\n>");
            else if (token > 0)
                printf("token = <\\0%o>", token);
            else
                printf("token = <EOF>");
            switch (token) {
            case IDENT_SY:
            case TYPEIDENT_SY:
            case STRCONST_SY:
                printf(" = \"%s\"", GetAtomString(atable, yylval.sc_ident));
                break;
            case CFLOATCONST_SY:
            case FLOATCONST_SY:
            case FLOATHCONST_SY:
            case FLOATXCONST_SY:
                printf(" = %9.6g", yylval.sc_fval);
                break;
            case INTCONST_SY:
                printf(" = %d", yylval.sc_int);
                break;
            }
            printf("\n");
        }

        if (token == '\n') {
            continue;
        }

        return token;
    }
} // yylex

///////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////// End of scanner.c //////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -