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

📄 smscan.c

📁 基于单片机的 snmp协议解析的一些原代码 给有用的 同行
💻 C
📖 第 1 页 / 共 3 页
字号:
        usStrSpaceTokIndx = usStrSpaceIndx;

        /* scan until trailing apostrophe */
        for(;;) {
            rLexChar = yylexin();
            switch (ucCT[rLexChar+1]) {
            case CTeof:
                /* EOF in string */
                yyerror("EOF inside a string");
                goto bad_str;

            case CTaps:
                /* end of string */
                goto good_str;

            case CTline:
                /* a newline - don't add it to string */
                yyerror("End of line inside string");
                goto bad_str;

            default:
                /* anything else - add to string */
                yylexout(rLexChar);
                break;
            }
        }

        /* get next char and check for B or H */
good_str:
        rLexChar = yylexin();
        yylexout(0);
        if ((rLexChar == 'B') || (rLexChar == 'b')) {
#ifdef OLD
            if (rLexChar == 'b') {
                yywarning("Binary string should use uppercase \"B\"");
            }
#endif
            /* check that all chars are zero or one */
            for (i = 0; pszTokVal[i] != 0; i++) {
                if ((pszTokVal[i] != '1') && (pszTokVal[i] != '0')) {
                    yyerror("bad binary string");
                    goto bad_str;
                }
            }
            sTokType = tokBSTR;
        } else if ((rLexChar == 'H') || (rLexChar == 'h')) {
#ifdef OLD
            if (rLexChar == 'h') {
                yywarning("Hex string should use uppercase \"H\"");
            }
#endif
            /* check that all chars are hex digits */
            for (i = 0; pszTokVal[i] != 0; i++) {
                if (!isxdigit(pszTokVal[i])) {
                    yyerror("bad hex string");
                    goto bad_str;
                }
            }
            sTokType = tokHSTR;
        } else {
            yyerror("string needs to be followed by 'B' or 'H'");
            goto bad_str;
        }
#ifdef OLD
        if (pszTokVal[0] == 0) {
            yyerror("Zero length string");
            goto bad_str;
        }
#endif
        rLexChar = yylexin();

        /* put in string table */
        pStrTab = StrTabInsert(pszTokVal);
        if (pStrTab->pszVal != pszTokVal) {
            /* string already in string table */
            /* so don't need another copy */
            usStrSpaceIndx = usStrSpaceTokIndx;
            pszTokVal = pStrTab->pszVal;
        }
        yylval.strval.pStr = pStrTab;
        break;

bad_str:
        sTokType = tokBADSTR;
        usStrSpaceIndx = usStrSpaceTokIndx; /* remove from string table */
        szTok[0] = 0;
        pszTokVal = szTok;
        break;

    case CTat:
        /* An include file */
        /* check if already in an include file */
        if (usInclLevel >= MXLV) {
            yyerror("Include file nested too deeply");

            /* skip over file name */
            rLexChar = yylexin();
            while((rLexChar != EOF) && !(isspace(rLexChar))) {
                rLexChar = yylexin();
            }
            continue;           /* continue scanning */   
        }

        /* get filename */

        pszTokVal = (PSZ)&(pbStrSpace[usStrSpaceIndx]);
        usStrSpaceTokIndx = usStrSpaceIndx;

        for (;;) {
            rLexChar = yylexin();
            if ((rLexChar == EOF) || (isspace(rLexChar)))
                break;
            yylexout(rLexChar);
        }
        yylexout(0);

        apszInFile[usInclLevel] = pszInFile; /* save current file name */
        afhIn[usInclLevel] = fhIn;           /* save current file handle */

        /* try to open include file */
        fhIn = fopen(pszTokVal, "rb");
        if (fhIn == NULL) {
            yyerror("Error opening Include file \"%s\"", pszTokVal);
            fhIn = afhIn[usInclLevel]; /* restore file handle */
            usStrSpaceIndx = usStrSpaceTokIndx; /* free up string space */

            continue;           /* continue scanning */
        }
        pszInFile = pszTokVal;
        yylval.strval.pszFn = pszInFile;

        /* save state of base file */
        ausLineNo[usInclLevel] = usLineNo;
        ausColNo[usInclLevel] = usColNo;
        aLexChar[usInclLevel] = rLexChar;

        /* set new state */
        usLineNo = 1;
        usColNo = 0;
        usTokLineNo = 0;
        usTokColNo = 0;
        rLexChar = ' ';
        usInclLevel++;

        if (fPrintIname)
            fprintf(fhMsg, "In file: %s\n", pszInFile);

        /* start at top */
        continue;

    case CTpound:
        /* Check for scanner/compiler directives */

        /* get directive name */
        pszTokVal = (PSZ)&(pbStrSpace[usStrSpaceIndx]);
        usStrSpaceTokIndx = usStrSpaceIndx;
        for (;;) {
            /* get directive name */
            rLexChar = yylexin();
#if 0
				printf("rLexChar = %c\n", rLexChar);
#endif
            if ((rLexChar == EOF) || (isspace(rLexChar)))
                break;
            yylexout(rLexChar);
        }
        yylexout(0);

        /* free up string space */
        usStrSpaceIndx = usStrSpaceTokIndx;

        /* check if "include" directive */
        if (strcmp(pszTokVal, "include") == 0) {
            /* include a file */
            sTokType = dirINCLUDE;
            pszTokVal = "#include";
            break;

        } else if (strcmp(pszTokVal, "aliasModule") == 0) {
            /* alias a module */
            sTokType = dirALIASMODULE;
            pszTokVal = "#aliasModule";
            break;

        } else if (strcmp(pszTokVal, "aliasSymbol") == 0) {
            /* alias a symbol */
            sTokType = dirALIASSYMBOL;
            pszTokVal = "#aliasSymbol";
            break;

        } else if (strcmp(pszTokVal, "popOpt") == 0) {
            /* alias a symbol */
            sTokType = dirPOPOPT;
            pszTokVal = "#popOpt";
            break;

        } else if (strcmp(pszTokVal, "pushOpt") == 0) {
            /* alias a symbol */
            sTokType = dirPUSHOPT;
            pszTokVal = "#pushOpt";
            break;

        } else if (strcmp(pszTokVal, "addOpt") == 0) {
            /* alias a symbol */
            sTokType = dirADDOPT;
            pszTokVal = "#addOpt";
            break;

        } else if (strcmp(pszTokVal, "removeOpt") == 0) {
            /* alias a symbol */
            sTokType = dirREMOVEOPT;
            pszTokVal = "#removeOpt";
            break;

        } else if (strcmp(pszTokVal, "printOpt") == 0) {
            /* alias a symbol */
            sTokType = dirPRINTOPT;
            pszTokVal = "#printOpt";
            break;

        } else if (strcmp(pszTokVal, "help") == 0) {
            /* print help for directives */
            sTokType = dirHELP;
            pszTokVal = "#help";
            break;

        } else {
            /* unknown directive */
            yyerror("unknown directive \"%s\"", pszTokVal);

            /* scan to end of line */
            rLexChar = yylexin();
            while((rLexChar != EOF) && (rLexChar != '\n')) {
                rLexChar = yylexin();
            }
        }

        /* start at top */
        continue;

    case CTminus:
        /* check for start of a comment or a minus */
        rLexChar = yylexin();

        if (rLexChar == '-') {
            /* ASN.1 style comment */
            /* scan until end of line */
            for (;;) {
                rLexChar = yylexin();
                if (rLexChar == EOF) {
                    yyerror("EOF inside a comment");
                    break;
                } else if (rLexChar == '\n') {
                    /* end of comment */
                    rLexChar = yylexin();
                    break;
                }
#ifdef OLD
                else if (rLexChar == '-') {
                    /* possible end of comment */
                    rLexChar = yylexin();
                    if (rLexChar == '-') {
                        /* end of comment */
                        rLexChar == yylexin();
                        break;
                    }
                }
#endif
            }
            continue;           /* start over again */
        }
        /* just a minus */
        szTok[0] = '-';
        sTokType = chMINUS;
        pszTokVal = szTok;
        break;

    case CTlcb:     /* the { char */
        sTokType = chLCB;
common_char:
        szTok[0] = (CHAR)rLexChar;
        pszTokVal = szTok;
        rLexChar = yylexin();
        break;

    case CTrcb:     /* the } char */
        sTokType = chRCB;
        goto common_char;

    case CTlpr:     /* the ( char */
        sTokType = chLPR;
        goto common_char;

    case CTrpr:     /* the ) char */
        sTokType = chRPR;
        goto common_char;
    
    case CTsemi:    /* the ; char */
        sTokType = chSEMI;
        goto common_char;

    case CTcomma:   /* the , char */
        sTokType = chCOMMA;
        goto common_char;

    default:        /* anything else */
        sTokType = chOTHER;
        goto common_char;

    case CTdot:     /* check for ".." */
        rLexChar = yylexin();   /* get next input char */
        if (rLexChar == '.') {
            /* double dot token */
            pszTokVal = "..";
            sTokType = tokDOTDOT;
            rLexChar = yylexin();
            while (rLexChar == '.') {
                /* bad ".." */
                sTokType = tokBADDOTDOT;
                rLexChar = yylexin();
            }
        } else {
            /* just a dot which is its own type */
            sTokType = chDOT;
            szTok[0] = '.';
            pszTokVal = szTok;
        }
        break;

    case CTeq:      /* the = char, assume bad tokIS */
        sTokType = tokBADIS;
        pszTokVal = "::=";      /* set to bad assignment */
        rLexChar = yylexin();
        while (rLexChar == ':') {
            /* a colon - try next char */
            rLexChar = yylexin();
        }
        break;

    case CTcolon:   /* check for "::=" */
        rLexChar = yylexin();   /* get next input char */
        sTokType = tokIS;       /* assume type OK */
        pszTokVal = "::=";
        if (rLexChar == ' ') {
            /* a space - try next char */
            rLexChar = yylexin();
            sTokType = tokBADIS; /* set to bad assignment */
        }
        if (rLexChar == ':') {
            /* looking for "::=" */
            rLexChar = yylexin();
            if (rLexChar == ' ') {
                /* a space - try next char */
                rLexChar = yylexin();
                sTokType = tokBADIS; /* set to bad assignment */
            }
            if (rLexChar != '=') {
                /* found bad version */
                sTokType = tokBADIS;
            } else {
                /* found complete version */
                rLexChar = yylexin();
            }
        } else if (rLexChar == '=') {
            /* found bad version */
            rLexChar = yylexin();
            sTokType = tokBADIS;
        }
        break;

    case CTalnum:
        /* token is either a number, name, or keyword */

        /* scan to end of token */
        pszTokVal = (PSZ)&(pbStrSpace[usStrSpaceIndx]);
        usStrSpaceTokIndx = usStrSpaceIndx;
        while(((rClass = ucCT[rLexChar+1]) == CTalnum) ||
             (rClass == CTminus) ||
             (rClass == CTubar)) {
            yylexout(rLexChar);
            rLexChar = yylexin();
        }
        yylexout(0);

        /* check if token is a number */
        sTokType = tokNAME;     /* assume a name */
        if (isdigit(*pszTokVal)) {
            {
                INT c1;
                ULONG ulVal;

                /* check for all digits */
                for (ulVal = 0L, i = 0, c1 = *pszTokVal;
                        (c1 != 0) && isdigit(c1);) {
                    ulVal = ulVal * 10L + (c1 - '0');
                    /* make sure length OK */
                    if (i < (MXDFINT-1))
                        szNum[i] = (CHAR)c1;
                    c1 = pszTokVal[++i];
                }
                if (c1 == 0) {
                    /* end of string, so a number, maybe */
                    /* check for too big values */
                    if ((i >= MXDFINT) || ((i == (MXDFINT-1)) &&
                            (strcmp(pszTokVal, "4294967295") > 0))) {
                        /* value too big */
                        yyerror("number too big, using value of one instead");
                        szNum[0] = '1';
                        szNum[1] = 0;
                        yylval.numval.ul = 1L;
                    } else {
                        /* it is a number */
                        szNum[i] = 0;
                        yylval.numval.ul = ulVal;
                    }
                    sTokType = tokNUMBER;
                    usStrSpaceIndx = usStrSpaceTokIndx;
                    pszTokVal = szNum;
                }
            }
        }

        /* if token is not a number, check if a keyword */
        if (sTokType != tokNUMBER) {
            /* check if token a keyword */
            if ((i = getKWindex(pszTokVal, TRUE)) == -1) {
                /* token is a name */
                /* lookup name in string table and add if not present */
                pStrTab = StrTabInsert(pszTokVal);
                if (pStrTab->pszVal != pszTokVal) {
                    /* name already in string table */
                    /* so don't need another copy */
                    usStrSpaceIndx = usStrSpaceTokIndx;
                    pszTokVal = pStrTab->pszVal;
                }
                yylval.strval.pStr = pStrTab;
            } else {
                /* token is a KeyWord */
                sTokType = keyWordName[i].sType;
                usStrSpaceIndx = usStrSpaceTokIndx;
                pszTokVal = keyWordName[i].pszName;
            }
        }
        break;

#ifdef OLD
    default:
        /* BUG in scanner implementation */
        yyerror("Internal scanner error: bad case");
        exit(1);
#endif
    }

    LexChar = rLexChar;
    return(sTokType);

    } /* for(;;) */

} /* yylex */

/* end of file: SMSCAN.C */

⌨️ 快捷键说明

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