From c5d8db8d39ab03dd579900b948b0e206bc0e5a0a Mon Sep 17 00:00:00 2001 From: Vern Paxson Date: Sat, 2 Oct 1993 15:19:20 +0000 Subject: [PATCH] Switched from FILE*'s to stream's --- FlexLexer.h | 39 ++++++++++++++++----------------------- flex.skl | 51 +++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 63 insertions(+), 27 deletions(-) diff --git a/FlexLexer.h b/FlexLexer.h index f8e0d35..0e9d183 100644 --- a/FlexLexer.h +++ b/FlexLexer.h @@ -1,12 +1,12 @@ // $Header$ -// FlexLexer.h -- define a base class for lexical analyzers generated by flex +// FlexLexer.h -- define classes for lexical analyzers generated by flex // Copyright (c) 1993 The Regents of the University of California. // All rights reserved. // // This code is derived from software contributed to Berkeley by -// Kent Williams. +// Kent Williams and Tom Epperly. // // Redistribution and use in source and binary forms are permitted provided // that: (1) source distributions retain this entire copyright notice and @@ -22,10 +22,8 @@ // WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. -#ifndef __FLEXCXX_H -#define __FLEXCXX_H - -#include +#ifndef __FLEX_LEXER_H +#define __FLEX_LEXER_H // This file defines two classes. The first, FlexLexer, is an abstract @@ -42,9 +40,9 @@ class FlexLexer { virtual ~FlexLexer() { } virtual void yy_switch_to_buffer( YY_BUFFER_STATE new_buffer ) = 0; - virtual YY_BUFFER_STATE yy_create_buffer( FILE* file, int size ) = 0; + virtual YY_BUFFER_STATE yy_create_buffer( istream* s, int size ) = 0; virtual void yy_delete_buffer( YY_BUFFER_STATE b ) = 0; - virtual void yyrestart( FILE *input_file ) = 0; + virtual void yyrestart( istream* s ) = 0; virtual int yylex() = 0; }; @@ -52,7 +50,9 @@ class FlexLexer { class yyFlexLexer : public FlexLexer { public: - yyFlexLexer( FILE* arg_yyin = 0, FILE* arg_yyout = 0 ) + // arg_yyin and arg_yyout default to the cin and cout, but we + // only make that assignment when initializing in yylex(). + yyFlexLexer( istream* arg_yyin = 0, ostream* arg_yyout = 0 ) { yyin = arg_yyin; yyout = arg_yyout; @@ -81,35 +81,28 @@ class yyFlexLexer : public FlexLexer { } void yy_switch_to_buffer( YY_BUFFER_STATE new_buffer ); - YY_BUFFER_STATE yy_create_buffer( FILE* file, int size ); + YY_BUFFER_STATE yy_create_buffer( istream* s, int size ); void yy_delete_buffer( YY_BUFFER_STATE b ); - void yyrestart( FILE *input_file ); + void yyrestart( istream* s ); virtual int yylex(); protected: - virtual int LexerInput( char* buf, int max_size ) - { - return read( fileno(yyin), buf, max_size ); - } - - virtual void LexerOutput( const char* buf, int size ) - { - (void) fwrite( (char*) buf, size, 1, yyout ); - } + virtual int LexerInput( char* buf, int max_size ); + virtual void LexerOutput( const char* buf, int size ); void yyunput( YY_CHAR c, YY_CHAR* buf_ptr ); int yyinput(); void yy_load_buffer_state(); - void yy_init_buffer( YY_BUFFER_STATE b, FILE* file ); + void yy_init_buffer( YY_BUFFER_STATE b, istream* s ); yy_state_type yy_get_previous_state(); yy_state_type yy_try_NUL_trans( yy_state_type current_state ); int yy_get_next_buffer(); - FILE* yyin; // input source for default LexerInput - FILE* yyout; // output sink for default LexerOutput + istream* yyin; // input source for default LexerInput + ostream* yyout; // output sink for default LexerOutput YY_BUFFER_STATE yy_current_buffer; diff --git a/flex.skl b/flex.skl index 26407f2..3615ab4 100644 --- a/flex.skl +++ b/flex.skl @@ -20,6 +20,9 @@ #ifdef __cplusplus #include +%+ +#include +%* #include /* Use prototypes in function declarations. */ @@ -120,8 +123,11 @@ #define YY_FATAL_ERROR(msg) \ do \ { \ - (void) fputs( msg, stderr ); \ +%- (void) putc( '\n', stderr ); \ +%+ + cerr << msg << '\n'; \ +%* exit( 1 ); \ } \ while ( 0 ) @@ -171,7 +177,9 @@ typedef struct yy_buffer_state *YY_BUFFER_STATE; extern int yyleng; +%- extern FILE *yyin, *yyout; +%* #ifdef __cplusplus extern "C" { @@ -204,7 +212,11 @@ extern "C" { struct yy_buffer_state { +%- FILE *yy_input_file; +%+ + istream* yy_input_file; +%* YY_CHAR *yy_ch_buf; /* input buffer */ YY_CHAR *yy_buf_pos; /* current position in input buffer */ @@ -323,10 +335,18 @@ YY_DECL yy_start = 1; /* first start state */ if ( ! yyin ) +%- yyin = stdin; +%+ + yyin = &cin; +%* if ( ! yyout ) +%- yyout = stdout; +%+ + yyout = &cout; +%* if ( yy_current_buffer ) yy_init_buffer( yy_current_buffer, yyin ); @@ -481,7 +501,11 @@ do_action: /* This label is used only to access EOF actions. */ default: #ifdef FLEX_DEBUG +%- printf( "action # %d\n", yy_act ); +%+ + cout << "action # " << yy_act << '\n'; +%* #endif YY_FATAL_ERROR( "fatal flex scanner internal error--no action found" ); @@ -489,6 +513,25 @@ do_action: /* This label is used only to access EOF actions. */ } /* end of scanning one token */ } /* end of yylex */ +%+ +int yyFlexLexer::LexerInput( char* buf, int max_size ) + { + if ( yyin->eof() || yyin->fail() ) + return 0; + + (void) yyin->read( buf, max_size ); + + if ( yyin->bad() ) + return -1; + else + return yyin->gcount(); + } + +void yyFlexLexer::LexerOutput( const char* buf, int size ) + { + (void) yyout->write( buf, size ); + } +%* /* yy_get_next_buffer - try to read in a new buffer * @@ -772,7 +815,7 @@ void yyrestart( input_file ) FILE *input_file; #endif %+ -void yyFlexLexer::yyrestart( FILE *input_file ) +void yyFlexLexer::yyrestart( istream* input_file ) %* { if ( ! yy_current_buffer ) @@ -843,7 +886,7 @@ FILE *file; int size; #endif %+ -YY_BUFFER_STATE yyFlexLexer::yy_create_buffer( FILE* file, int size ) +YY_BUFFER_STATE yyFlexLexer::yy_create_buffer( istream* file, int size ) %* { YY_BUFFER_STATE b; @@ -897,7 +940,7 @@ YY_BUFFER_STATE b; FILE *file; #endif %+ -void yyFlexLexer::yy_init_buffer( YY_BUFFER_STATE b, FILE* file ) +void yyFlexLexer::yy_init_buffer( YY_BUFFER_STATE b, istream* file ) %* { b->yy_input_file = file; -- 2.40.0