1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
|
D [0-9]
L [a-zA-Z_]
H [a-fA-F0-9]
E [Ee][+-]?{D}+
FS (f|F|l|L)
IS (u|U|l|L)*
ws [ \t\v\f]
nl [\n]
%{
/* $Id: scan.l,v 1.14 1995/01/25 08:19:24 lott Exp $
*
* csize, a program to measure the size of C source files.
* Copyright (C) 1994 Christopher Lott <lott@informatik.uni-kl.de>
* FB Informatik - Bau 57 / Universitaet KL / D--67653 Kaiserslautern / Germany
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation. See the file COPYING for more details.
*
* This file holds the source used by flex to generate the scanner for C.
* SEE THE FILE "INSTALL" FOR THE REQUIRED FLEX VERSION. Sorry to
* shout, but different versions of flex exhibit different behavior.
*
* Why flex? Because lex complains about this file as follows:
* "scan.l", line xxx: (Error) Too many right contexts
* If you simply remove the first token ("auto"), then lex dumps core
* on my machine. Neither behavior is acceptable and I refuse to code
* around bugs in lex when flex is available, reliable, and free.
*
*/
#include <stdio.h>
#include <scan.h>
#include <csize.h>
/* variables supplied by flex
*
*/
extern int yyleng;
extern char *yytext;
extern FILE *yyin;
/* global variables
* variables beginning with C_ are referenced in main and
* Echo is set in main; all appear in csize.h
*
*/
int Echo,
Lex_errors;
long C_newlines, /* incremented at the *end* of a line */
C_blank_lines,
C_lines_w_comments,
C_nb_nc_lines, /* incremented when first nbnc is seen */
C_semicolons,
C_pp_directives;
/*
* variables used only in this file
*/
static char *Filename;
static int Column;
static long prev_line_with_text;
static long prev_line_with_comment;
/*
* functions defined and used only in this file
*/
static int check_type _((void));
static void count_nb_nc_lines _((void));
static void count _((void));
static void semicolon _((void));
static void string_lit _((void));
static void count_line_w_comment _((void));
static void comment _((int));
static void pp_dir _((void));
static void count_ws _((void));
static void whitespace_line _((void));
static void newline _((void));
static void scan_error _((char *));
static void bad_char _((void));
%}
%%
"auto" { count(); return(AUTO); }
"break" { count(); return(BREAK); }
"case" { count(); return(CASE); }
"char" { count(); return(CHAR); }
"const" { count(); return(CONST); }
"continue" { count(); return(CONTINUE); }
"default" { count(); return(DEFAULT); }
"do" { count(); return(DO); }
"double" { count(); return(DOUBLE); }
"else" { count(); return(ELSE); }
"enum" { count(); return(ENUM); }
"extern" { count(); return(EXTERN); }
"float" { count(); return(FLOAT); }
"for" { count(); return(FOR); }
"goto" { count(); return(GOTO); }
"if" { count(); return(IF); }
"int" { count(); return(INT); }
"long" { count(); return(LONG); }
"register" { count(); return(REGISTER); }
"return" { count(); return(RETURN); }
"short" { count(); return(SHORT); }
"signed" { count(); return(SIGNED); }
"sizeof" { count(); return(SIZEOF); }
"static" { count(); return(STATIC); }
"struct" { count(); return(STRUCT); }
"switch" { count(); return(SWITCH); }
"typedef" { count(); return(TYPEDEF); }
"union" { count(); return(UNION); }
"unsigned" { count(); return(UNSIGNED); }
"void" { count(); return(VOID); }
"volatile" { count(); return(VOLATILE); }
"while" { count(); return(WHILE); }
{L}({L}|{D})* { count(); return(check_type()); }
0[xX]{H}+{IS}? { count(); return(CONSTANT); }
0{D}+{IS}? { count(); return(CONSTANT); }
{D}+{IS}? { count(); return(CONSTANT); }
'(\\.|[^\\'\n])+' { count(); return(CONSTANT); }
{D}+{E}{FS}? { count(); return(CONSTANT); }
{D}*"."{D}+({E})?{FS}? { count(); return(CONSTANT); }
{D}+"."{D}*({E})?{FS}? { count(); return(CONSTANT); }
">>=" { count(); return(RIGHT_ASSIGN); }
"<<=" { count(); return(LEFT_ASSIGN); }
"+=" { count(); return(ADD_ASSIGN); }
"-=" { count(); return(SUB_ASSIGN); }
"*=" { count(); return(MUL_ASSIGN); }
"/=" { count(); return(DIV_ASSIGN); }
"%=" { count(); return(MOD_ASSIGN); }
"&=" { count(); return(AND_ASSIGN); }
"^=" { count(); return(XOR_ASSIGN); }
"|=" { count(); return(OR_ASSIGN); }
">>" { count(); return(RIGHT_OP); }
"<<" { count(); return(LEFT_OP); }
"++" { count(); return(INC_OP); }
"--" { count(); return(DEC_OP); }
"->" { count(); return(PTR_OP); }
"&&" { count(); return(AND_OP); }
"||" { count(); return(OR_OP); }
"<=" { count(); return(LE_OP); }
">=" { count(); return(GE_OP); }
"==" { count(); return(EQ_OP); }
"!=" { count(); return(NE_OP); }
";" { count(); semicolon(); return(';'); }
"{" { count(); return('{'); }
"}" { count(); return('}'); }
"," { count(); return(','); }
":" { count(); return(':'); }
"=" { count(); return('='); }
"(" { count(); return('('); }
")" { count(); return(')'); }
"[" { count(); return('['); }
"]" { count(); return(']'); }
"." { count(); return('.'); }
"&" { count(); return('&'); }
"!" { count(); return('!'); }
"~" { count(); return('~'); }
"-" { count(); return('-'); }
"+" { count(); return('+'); }
"*" { count(); return('*'); }
"/" { count(); return('/'); }
"%" { count(); return('%'); }
"<" { count(); return('<'); }
">" { count(); return('>'); }
"^" { count(); return('^'); }
"|" { count(); return('|'); }
"?" { count(); return('?'); }
\" { count(); string_lit(); return(STRING_LITERAL); }
"#"{ws}*"define" { count(); pp_dir(); /* from K&R 2/e p. 239 */ }
"#"{ws}*"undef" { count(); pp_dir(); }
"#"{ws}*"include" { count(); pp_dir(); }
"#"{ws}*"line" { count(); pp_dir(); }
"#"{ws}*"error" { count(); pp_dir(); }
"#"{ws}*"pragma" { count(); pp_dir(); }
"#"{ws}*"endif" { count(); pp_dir(); }
"#"{ws}*"if" { count(); pp_dir(); }
"#"{ws}*"ifdef" { count(); pp_dir(); }
"#"{ws}*"ifndef" { count(); pp_dir(); }
"#"{ws}*"elif" { count(); pp_dir(); }
"#"{ws}*"else" { count(); pp_dir(); }
"#"{ws}*"include_next" { count(); pp_dir(); /* from GCC's cccp.c */ }
"#"{ws}*"import" { count(); pp_dir(); }
"#"{ws}*"warning" { count(); pp_dir(); }
"#"{ws}*"sccs" { count(); pp_dir(); }
"#"{ws}*"ident" { count(); pp_dir(); }
"#"{ws}*"assert" { count(); pp_dir(); }
"#"{ws}*"unassert" { count(); pp_dir(); }
"\\" { count(); /* backslash processing is weak */ }
"/*" { comment(1); }
{ws}+ { count_ws(); }
^{ws}+$ { whitespace_line(); }
{nl} { newline(); }
. { bad_char(); }
%%
/* If being compiled with a C++ compiler,
* then the flex input function is named
* "yyinput"; else it's named "input".
*
* Only tested with g++.
*
*/
#ifdef __cplusplus
#define input yyinput
#endif
/* init_scanner
* initialize necessary variables and set up the input file pointer
* before processing a file.
*
* Called from csize.c
*
*/
void
init_scanner(FILE *fp, char *filename)
{
C_newlines = 0;
C_blank_lines = 0;
C_lines_w_comments = 0;
C_nb_nc_lines = 0;
C_semicolons = 0;
C_pp_directives = 0;
Lex_errors = 0;
prev_line_with_comment = -1;
prev_line_with_text = -1;
Filename = filename;
/* although the flex documentation states that assigning
* to yyin like this:
* yyin = fp;
* should be equivalent to
* yyrestart(fp);
* the run-time behavior of scanners generated with flex 2.4.7
* is different; with only the assignment, the scanner complains
* of an illegal characater at line 1, character 1 of subsequent
* files. Anyhow, this works.
*/
yyrestart(fp);
}
/* count_nb_nc_lines
* count nonblank noncomment lines in the input, but
* ensure that we don't count the same line twice by
* checking and/or updating the appropriate variable
*
* called by functions; no dependence on yytext
*
*/
static void
count_nb_nc_lines(void)
{
if (C_newlines != prev_line_with_text) {
++C_nb_nc_lines;
prev_line_with_text = C_newlines;
}
}
/* count
* count columns for lines with *nonblank noncomment* text.
* Optionally echo the buffer yytext, and call count_nb_nc_lines
* to count the line.
*
* called by lexer with the *nonblank noncomment* token in yytext
*
*/
static void
count(void)
{
Column += yyleng;
if (Echo)
ECHO;
count_nb_nc_lines();
}
/* semicolon
* saw a semicolon in the input, just bump the appropriate counter
*
* count() is called before this function
*
* called by lexer but has no dependence on yytext
*
*/
static void
semicolon(void)
{
++C_semicolons;
}
/* pp_dir
* Read characters until the terminating newline ('\n') is found. Count the
* number of directives. Even though no text may appear after a directive
* on a line, count columns so that the function newline() can be used.
*
* Complications:
* Escaped backslash characters ('\\'). These do nothing.
* Escaped newline characters (continuation lines). These force the
* function to update newline and nonblank noncomment line counts.
* Note that a continuation line can consist of a single newline,
* or only whitespace and a newline; in both cases the continuation
* line terminates the preprocessor directive. The algorithm makes
* provisions to treat both cases identically. Those blank-ish
* continuation lines, although they look blank, are counted as
* nonblank noncomment lines.
* Comments that begin on the pp_dir's line. These must be processed
* until the closing * and / is found, after which processing of
* the pp resumes. See the part in K&Rrev2, p. 229, about
* logically successive preprocessor phases.
* The terminating newline must be given back via unput so that
* the lexer can correctly identify the beginning of lines
* for rules that use the caret ('^')
*
* Input errors:
* EOF condition before the terminating newline
*
* count() is called before this function, which marks the first line of
* the preprocessor directive as having nonblank noncomment text.
*
* Called by the lexer, but no dependence on yytext
*
*/
static void
pp_dir(void)
{
register int c;
register int in_pp_dir = 1;
register int saw_forwslash = 0;
register int apply_backslash = 0;
++C_pp_directives;
while (in_pp_dir && (c = input()) != EOF) {
if (c == '\n') { /* first take care of newline chars */
if (apply_backslash) {
apply_backslash = 0;
newline(); /* register the newline globally */
count_nb_nc_lines(); /* count the new nb nc line */
}
else {
if (Column == 0) /* if Column has value 0, newline() will */
++Column; /* count the line as blank; don't let it */
unput(c); /* give back the nl */
in_pp_dir = 0; /* done with this pp dir */
}
}
else {
if (Echo) /* only do echo processing for non-newlines */
putchar(c);
++Column;
switch (c) {
case '\\':
if (apply_backslash) {
apply_backslash = 0;
}
else {
apply_backslash = 1;
}
saw_forwslash = 0;
break;
case '/':
saw_forwslash = 1;
apply_backslash = 0;
break;
case '*':
if (saw_forwslash) {
comment(0); /* 0 means don't echo the start token again */
}
saw_forwslash = 0;
apply_backslash = 0;
break;
default:
apply_backslash = 0; /* any char after f/b-slash turns off the flags */
saw_forwslash = 0;
break;
}
}
} /* while */
if (c == EOF)
scan_error("EOF in preprocessor directive");
}
/* string_lit
* Read characters until the closing quote ('"') is found. Do not count
* string literals. Count columns because a string literal may be followed
* by other text on a line.
*
* Complications:
* Escaped backslash characters ('\\'). These do nothing.
* Escaped quote characters ('\"'). These do not terminate
* the string literal.
* Escaped newline characters (continuation lines). These force the
* function to update newline and nonblank noncomment line counts.
* Note that a continuation line can never be entirely blank -
* either it has another escaped newline, or it has the terminating
* quote. This prevents the anomaly of counting a blank line as
* something else, as is the case for preprocessor directives.
*
* Input errors:
* unescaped newline before the terminating quote
* EOF condition before the terminating quote
*
* count() is called before this function, which marks the first line of
* the string literal line as having nonblank noncomment text.
*
* Called by the lexer, but no dependence on yytext
*
*/
static void
string_lit(void)
{
register int c;
register int in_string_lit = 1;
register int apply_backslash = 0; /* apply backslash to the next char seen */
while (in_string_lit && (c = input()) != EOF) {
if (c == '\n') { /* first take care of newline chars */
if (apply_backslash) {
apply_backslash = 0;
newline(); /* register the newline globally */
count_nb_nc_lines(); /* count the new nb nc line */
}
else {
scan_error("newline in string literal");
unput(c); /* give back the newline */
in_string_lit = 0; /* give up on this string literal */
}
}
else { /* prev char was not a newline */
switch (c) {
case '\\':
if (apply_backslash)
apply_backslash = 0;
else
apply_backslash = 1;
break;
case '"':
if (apply_backslash)
apply_backslash = 0;
else
in_string_lit = 0; /* finished with this string literal */
break;
default:
if (apply_backslash) /* any char after bslash turns off the flag */
apply_backslash = 0;
break;
}
if (Echo) /* only do echo processing for non-newlines */
putchar(c);
++Column;
}
} /* while */
if (c == EOF)
scan_error("EOF in string literal");
}
/* count_ws
* count the columns in the buffer yytext (only white space, never nl)
* adjusting for tabs
*
* called by the lexer with the whitespace in yytext
*
*/
static void
count_ws(void)
{
register int i;
if (Echo)
ECHO;
for (i = 0; yytext[i] != '\0'; i++)
if (yytext[i] == '\t')
Column += 8 - (Column % 8);
else
Column++;
}
/* whitespace_line
* Process a line that is not empty but only has whitespace.
* The newline is *not* part of yytext at this point.
* Do not do *anything* except echo the line, if desired.
* Because the Column variable is not changed, it will be
* 0 when newline is called, which will count the blank line.
*
* called by the lexer with the whitespace in yytext
*
*/
static void
whitespace_line(void)
{
if (Echo)
ECHO;
}
/* newline
* Count all newline characters. Also check the value of the Column
* variable to catch lines with nothing but whitespace (blank lines).
*
* Note that blank lines are somewhat difficult to catch with a lex
* pattern, because the pattern ``^"\n"'', meaning match a newline that
* occurs first on a line, doesn't seem to work.
*
* Note also that if the lex pattern to match a line with only whitespace
* fires, then the Column variable will not be incremented, and this
* function will be called with Column = 0. I call this a virtual
* column 0, because newline really didn't occur as the first character
* on the line, we just pretend that it did.
*
* Called by the lexer *and* other functions; no dependence on yytext
*
*/
static void
newline(void)
{
if (Echo)
putchar('\n');
++C_newlines;
if (Column == 0) {
++C_blank_lines;
}
Column = 0;
}
/* count_line_w_comment
* Cuarantee that we don't count a line as having a comment twice
*
* Called by functions; no dependence on yytext
*
*/
static void
count_line_w_comment(void)
{
if (C_newlines != prev_line_with_comment) {
++C_lines_w_comments;
prev_line_with_comment = C_newlines;
}
}
/* comment
* Read characters until the closing two-character sequence '*' and '/'
* is found. Count the lines with comments. Count columns because a
* comment may be followed by other text on a line.
*
* Complications:
* Newline characters. These force the function to update the counts
* of newlines and the number of lines with comments. A line
* within a comment that has zero or more whitespace characters
* and a newline, although blank, will be counted as a comment line.
* Input errors:
* EOF condition before the terminating token
*
* This fn implements a simple FSA to recognize the end of a comment.
* The original code was reused from Jeff Lee's distribution and
* amended to count lines. While I don't much like gotos, his
* implementation is highly concise and keeps code redundancy to a
* minimum. This code is far more concise than, for example, the
* code from the flexdoc(1) man page, when you consider all the
* intermediate cases in which characters must be counted.
*
* Called by the lexer with the comment start token in yytext,
* and also called by pp_dir with garbage in yytext, so don't
* depend on yytext (and don't use ECHO). Argument tells whether
* it should echo the comment start token.
*
*/
static void
comment(int echo_start)
{
register int c;
int lookahead;
if (Echo && echo_start) {
putchar('/');
putchar('*');
}
Column += yyleng;
/* starting a comment, count the line if necessary */
count_line_w_comment();
/* deal with all characters in this while loop */
loop:
while ((c = input()) != EOF) { /* find a star */
if (c == '\n') {
if (Column == 0) /* if Column has value 0, newline() will count */
++Column; /* the line as blank, which we don't want */
newline();
count_line_w_comment();
}
else {
if (Echo)
putchar(c);
++Column;
}
if (c == '*') /* found one, leave the while loop */
break;
}
if (c != EOF && (lookahead = input()) != '/') { /* check for a slash */
unput(lookahead);
goto loop;
}
if (c == EOF) {
scan_error("EOF in comment");
}
else {
if (Echo)
putchar('/');
}
}
/* check_type
* pseudo code --- this is what it should check
*
* if (yytext == type_name)
* return(TYPE_NAME);
* else
* return(IDENTIFIER);
*
* If it did, it would be easy to build a C language
* recognizer, and then trivial to count declarations
* and executable statements.
*
* However, the function only returns IDENTIFIER
*
*/
static int
check_type(void)
{
return(IDENTIFIER);
}
/* scan_error
* print an informative error message with file, line info.
* if echoing is on, indicate the column where the error
* was found by using the Column variable
*
*/
static void
scan_error(char *s)
{
fflush(stdout);
++Lex_errors;
if (Echo)
printf("\n%*s\n%*s\n", Column, "^", Column, s);
else
printf("\"%s\", line %d: %s\n", Filename, C_newlines + 1, s);
}
/* bad_char
* bump up the column count and call the error function
*
* Called by the lexer with the unrecognized character in yytext
*
*/
static void
bad_char(void)
{
register int i;
if (Echo)
ECHO;
++Column;
scan_error("bad character");
if (Echo) /* restore output to column where we left off */
for (i = 0; i < Column; ++i)
putchar(' ');
}
/* yywrap
* supply a trivial version of yywrap
* to eliminate the need to link against libfl
* the value 1 means stop at the end of the file
* (the file pointer has not been aimed at a new file)
*
* Called by the lexer at the end of the input
*
*/
int
yywrap(void)
{
return 1;
}
|