123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107 |
- #include <stdio.h>
- /* This file contains the scanner for mylex(), the following functions and
- * variables are exported :
- *
- * int yylineno; - The current line number.
- *
- * char scanc(); - Return next character.
- *
- * backc( c); - Push back one character.
- * char c;
- *
- * FILE *switch_input( new); - Scanner will from now on read from
- * FILE *new; - 'new', old input-file is returned.
- *
- * The scanner must perform a lookahead of more than one character, so it uses
- * it's own internal buffer.
- */
- int yylineno = 1;
- /********* Internal variables + functions ***********************/
- #define BUF_SIZE 16
- static char buf[BUF_SIZE], /* Bufer to save backc()-characters */
- *bufptr = buf; /* Pointer to space for backc()-character */
- static FILE *infile = stdin;
- static char nextc()
- {
- if ( bufptr > buf)
- return( *--bufptr);
- else
- return( getc( infile));
- }
- /***************************************************************/
- char scanc()
- /* Get next character, but delete al C-comments and count lines */
- {
- char c, nextc();
- c = nextc();
- while ( c == '/') {
- c = nextc();
- if ( c == '*') { /* start of comment */
- while ( nextc() != '*' || nextc() != '/')
- ;
- c = nextc();
- }
- else {
- backc( c);
- return( '/');
- }
- }
- if ( c == '\n')
- yylineno++;
- return( c);
- }
- backc( c)
- char c;
- {
- if ( bufptr >= buf + BUF_SIZE)
- error( "backc(), no space in buffer left!");
- else {
- if ( c == '\n')
- yylineno--;
- *bufptr++ = c;
- }
- }
- FILE *switch_input( new)
- FILE *new;
- /* Switch to a new input file, try to save the lookahead-characters in buf[]
- * by calling ungetc(). If they can't be saved return NULL.
- */
- {
- char *ptr; FILE *old;
- /* Clean buf[] */
- for ( ptr = buf; ptr < bufptr; ptr++)
- if ( ungetc( *ptr, infile) == EOF && *ptr != EOF)
- return( NULL);
- bufptr = buf;
- old = infile;
- infile = new;
- return( old);
- }
|