ack/util/ceg/EM_parser/common/scan.c

111 lines
1.9 KiB
C
Raw Normal View History

1987-11-20 11:12:07 +00:00
#include <stdio.h>
1987-11-25 11:49:48 +00:00
/* 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.
*/
1987-11-20 11:12:07 +00:00
int yylineno = 1;
1987-11-25 11:49:48 +00:00
/********* 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 = NULL;
1987-11-25 11:49:48 +00:00
static char nextc()
1987-11-20 11:12:07 +00:00
{
FILE* fp = infile ? infile : stdin;
1987-11-20 11:12:07 +00:00
if ( bufptr > buf)
return( *--bufptr);
else
return( getc( fp));
1987-11-20 11:12:07 +00:00
}
1987-11-25 11:49:48 +00:00
/***************************************************************/
1987-11-20 11:12:07 +00:00
char scanc()
1987-11-25 11:49:48 +00:00
/* Get next character, but delete al C-comments and count lines */
1987-11-20 11:12:07 +00:00
{
1987-11-25 11:49:48 +00:00
char c, nextc();
1987-11-20 11:12:07 +00:00
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);
}
1987-11-25 11:49:48 +00:00
backc( c)
char c;
{
if ( bufptr >= buf + BUF_SIZE)
error( "backc(), no space in buffer left!");
else {
if ( c == '\n')
yylineno--;
*bufptr++ = c;
}
}
1987-11-20 11:12:07 +00:00
FILE *switch_input( new)
FILE *new;
1987-11-25 11:49:48 +00:00
/* 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.
*/
1987-11-20 11:12:07 +00:00
{
FILE* fp = infile ? infile : stdin;
1987-11-20 11:12:07 +00:00
char *ptr; FILE *old;
/* Clean buf[] */
for ( ptr = buf; ptr < bufptr; ptr++)
if ( ungetc( *ptr, fp) == EOF && *ptr != EOF)
1987-11-20 11:12:07 +00:00
return( NULL);
bufptr = buf;
old = infile;
infile = new;
return( old);
}