📄 scanner.l
字号:
fcncal: if (fcndef == YES || ppdefine == YES || rules == YES) { token = FCNCALL; goto fcn; /* NOTREACHED */ } if (template == NO) { templateparens = parens; template = YES; } goto ident; /* NOTREACHED */ }{identifier}/[* \t\n]+[a-zA-Z0-9_] { /* typedef name use */ goto ident; /* NOTREACHED */ }{identifier} { char *s; if (global == YES && ppdefine == NO && yytext[0] != '#' && external == NO && initializer == NO && arraydimension == NO && structfield == NO && template == NO && fcndef == NO) { if (esudef == YES) { /* if enum/struct/union */ token = MEMBERDEF; } else { token = GLOBALDEF; } } else { ident: token = IDENT; } fcn: /* if a long line */ if (yyleng > STMTMAX) { int c; /* skip to the end of the line */ warning("line too long"); while ((c = skipcomment_input()) != LEXEOF) { if (c == '\n') { unput(c); break; } } } /* truncate a long symbol */ if (yyleng - first > PATLEN) { warning("symbol too long"); yyleng = first + PATLEN; yytext[yyleng] = '\0'; } /* if a keyword */ yymore(); if ((s = lookup(yytext + first)) != NULL) { first = yyleng; /* if the start of a typedef */ if (s == typedeftext) { typedefbraces = braces; oldtype = YES; } /* if an enum/struct/union */ /* (needed for "typedef struct tag name;" so tag isn't marked as the typedef name) */ else if (s == enumtext || s == structtext || s == uniontext) { } /* if an external definition */ else if (s == externtext) { externalbraces = braces; external = YES; } /* keyword doesn't start a function template */ else if (templateparens == parens && template == YES) { templateparens = -1; template = NO; } else { /* next identifier after typedef was a keyword */ oldtype = NO; } } else { /* identifier */ last = yyleng; /* if a class/enum/struct/union tag definition */ if (tagdef && strnotequal(yytext + first, "class")) { token = tagdef; tagdef = '\0'; if (braces == 0) { esudef = YES; } } /* if a typedef name */ else if (braces == typedefbraces && oldtype == NO && arraydimension == NO) { token = TYPEDEF; } else { oldtype = NO; } return(token); /* NOTREACHED */ } }\[ { /* array dimension (don't worry or about subscripts) */ arraydimension = YES; goto more; /* NOTREACHED */ }\] { arraydimension = NO; goto more; /* NOTREACHED */ }\\\n { /* preprocessor statement is continued on next line */ goto eol; /* NOTREACHED */ }\n { /* end of the line */ if (ppdefine == YES) { /* end of a #define */ ppdefine = NO; yyless(yyleng - 1); /* rescan \n */ last = first; yymore(); return(DEFINEEND); } /* skip the first 8 columns of a breakpoint listing line */ /* and skip the file path in the page header */ if (bplisting == YES) { int c, i; switch (skipcomment_input()) { /* tab and EOF just fall through */ case ' ': /* breakpoint number line */ case '[': for (i = 1; i < 8 && skipcomment_input() != LEXEOF; ++i) ; break; case '.': /* header line */ case '/': /* skip to the end of the line */ while ((c = skipcomment_input()) != LEXEOF) { if (c == '\n') { unput(c); break; } } break; case '\n': /* empty line */ unput('\n'); break; } } eol: ++myylineno; first = 0; last = 0; if (symbols > 0) { return(NEWLINE); } lineno = myylineno; }\' { /* character constant */ if (sdl == NO) { multicharconstant('\''); } goto more; /* NOTREACHED */ }\" { /* string constant */ multicharconstant('"'); goto more; /* NOTREACHED */ }^[ \t\f]+ { /* don't save leading white space */ }\#[ \t]*include[ \t]*["<][^"> \t\n]+ { /* #include file */ char *s; s = strpbrk(yytext, "\"<"); incfile(s + 1, s); /* HBB: avoid pointer mismatch if yytext is * unsigned, or a pointer */ first = s - (char *)&(yytext[0]); last = yyleng; if (compress == YES) { yytext[0] = '\2'; /* compress the keyword */ } yymore(); return(INCLUDE); /* NOTREACHED */ }\#[ \t]*{identifier} | /* preprocessor keyword */{number} | /* number */. { /* punctuation and operators */ more: first = yyleng; yymore(); }%%voidinitscanner(char *srcfile){ char *s; if (maxifbraces == NULL) { maxifbraces = mymalloc(miflevel * sizeof(int)); preifbraces = mymalloc(miflevel * sizeof(int)); } first = 0; /* buffer index for first char of symbol */ last = 0; /* buffer index for last char of symbol */ lineno = 1; /* symbol line number */ myylineno = 1; /* input line number */ arraydimension = NO; /* inside array dimension declaration */ bplisting = NO; /* breakpoint listing */ braces = 0; /* unmatched left brace count */ classdef = NO; /* c++ class definition */ elseelif = NO; /* #else or #elif found */ esudef = NO; /* enum/struct/union global definition */ external = NO; /* external definition */ externalbraces = -1; /* external definition outer brace count */ fcndef = NO; /* function definition */ global = YES; /* file global scope (outside functions) */ iflevel = 0; /* #if nesting level */ initializer = NO; /* data initializer */ initializerbraces = -1; /* data initializer outer brace count */ lex = NO; /* lex file */ parens = 0; /* unmatched left parenthesis count */ ppdefine = NO; /* preprocessor define statement */ pseudoelif = NO; /* pseudo-#elif */ oldtype = NO; /* next identifier is an old type */ rules = NO; /* lex/yacc rules */ sdl = NO; /* sdl file */ structfield = NO; /* structure field declaration */ tagdef = '\0'; /* class/enum/struct/union tag definition */ template = NO; /* function template */ templateparens = -1; /* function template outer parentheses count */ typedefbraces = -1; /* initial typedef braces count */ BEGIN 0; /* if this is not a C file */ if ((s = strrchr(srcfile, '.')) != NULL) { switch (*++s) { /* this switch saves time on C files */ case 'b': if (strcmp(s, "bp") == 0) { /* breakpoint listing */ bplisting = YES; } break; case 'l': if (strcmp(s, "l") == 0) { /* lex */ lex = YES; global = NO; } break; case 's': if (strcmp(s, "sd") == 0) { /* sdl */ sdl = YES; BEGIN SDL; } break; case 'y': if (strcmp(s, "y") == 0) { /* yacc */ global = NO; } break; } }}intskipcomment_input(void){ int c; if ((c = getc (yyin)) == '/') { return comment (); } else { return c; } }intcomment_input(void){ int c; c = getc (yyin); return c;}intcomment(void){ int c, lastc; do { if ((c = getc(yyin)) == '*') { /* C comment */ lastc = '\0'; while ((c = getc(yyin)) != EOF && (c != '/' || lastc != '*')) { /* fewer '/'s */ if (c == '\n') { ++myylineno; } lastc = c; } /* return a blank for Reiser cpp token concatenation */ if ((c = getc(yyin)) == '_' || isalnum(c)) { (void) ungetc(c, yyin); c = ' '; break; } } else if (c == '/') { /* C++ comment */ while ((c = getc(yyin)) != EOF && c != '\n') { ; } break; } else { /* not a comment */ (void) ungetc(c, yyin); c = '/'; break; /* NOTREACHED */ } /* there may be an immediately following comment */ } while (c == '/'); return(c);}voidmulticharconstant(char terminator){ char c; /* scan until the terminator is found */ while ((c = yytext[yyleng++] = comment_input()) != terminator) { switch (c) { case '\\': /* escape character */ if ((yytext[yyleng++] = comment_input()) == '\n') { ++myylineno; } break; case '\t': /* tab character */ /* if not a lex program, continue */ if (lex == NO) { break; } /* fall through */ case '\n': /* illegal character */ /* assume the terminator is missing, so put this character back */ unput(c); yytext[--yyleng] = '\0'; /* fall through */ case LEXEOF: /* end of file */ return; default: /* change a control character to a blank */ if (!isprint((unsigned char)c)) { yytext[yyleng - 1] = ' '; } } /* if this token will overflow the line buffer */ /* note: '\\' may cause yyleng to be > STMTMAX */ if (yyleng >= STMTMAX) { /* truncate the token */ while ((c = comment_input()) != LEXEOF) { if (c == terminator) { unput(c); break; } else if (c == '\n') { ++myylineno; } } } } yytext[yyleng] = '\0';}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -