*BSD News Article 24431


Return to BSD News archive

Newsgroups: comp.os.386bsd.misc
Path: sserve!newshost.anu.edu.au!munnari.oz.au!news.Hawaii.Edu!ames!agate!howland.reston.ans.net!noc.near.net!ceylon!genesis!steve2
From: steve2@genesis.nred.ma.us (Steve Gerakines)
Subject: flex yylineno support in lex.skel
Message-ID: <CH2zDz.1Fy@genesis.nred.ma.us>
Keywords: lex, flex
Organization: Genesis Public Access Unix +1 508 664 0149
Date: Fri, 26 Nov 1993 04:05:58 GMT
Lines: 182

Several people have asked for this, so here it is.  It includes the fread()
change someone mentioned.

If you encounter a program that has it's own yylineno support built-in,
define YY_USE_LINENO=0 to disable flex yylineno generation.  yylineno
is always updated after the action is taken, so yylineno will always
point to where yytext starts.  If you remove or add eol characters in
yytext during an action, yylineno will probably become incorrect.

Have fun,
- Steve
steve2@genesis.nred.ma.us
---- snip ---- snip ---- snip ----
*** lex.skel.save	Wed Nov 24 01:00:14 1993
--- lex.skel	Thu Nov 25 22:47:42 1993
***************
*** 28,33 ****
--- 28,38 ----
  /* the "const" storage-class-modifier is valid */
  #define YY_USE_CONST
  
+ /* include yylineno support unless told not to */
+ #ifndef YY_USE_LINENO
+ #define YY_USE_LINENO	1
+ #endif
+ 
  #else	/* ! __cplusplus */
  
  #ifdef __STDC__
***************
*** 42,47 ****
--- 47,55 ----
  
  #define YY_USE_PROTOS
  #define YY_USE_CONST
+ #ifndef YY_USE_LINENO
+ #define YY_USE_LINENO	1
+ #endif
  
  #endif	/* __STDC__ */
  #endif	/* ! __cplusplus */
***************
*** 67,73 ****
   */
  char *malloc();
  int free();
- int read();
  #endif
  
  
--- 75,80 ----
***************
*** 91,98 ****
   * is returned in "result".
   */
  #define YY_INPUT(buf,result,max_size) \
! 	if ( (result = read( fileno(yyin), (char *) buf, max_size )) < 0 ) \
! 	    YY_FATAL_ERROR( "read() in flex scanner failed" );
  #define YY_NULL 0
  
  /* no semi-colon after return; correct usage is to write "yyterminate();" -
--- 98,105 ----
   * is returned in "result".
   */
  #define YY_INPUT(buf,result,max_size) \
! 	if ((result = fread((char *)buf, sizeof(char), max_size, yyin)) < 0) \
! 	    YY_FATAL_ERROR("fread() in flex scanner failed");
  #define YY_NULL 0
  
  /* no semi-colon after return; correct usage is to write "yyterminate();" -
***************
*** 199,204 ****
--- 206,214 ----
  
      YY_CHAR *yy_ch_buf;		/* input buffer */
      YY_CHAR *yy_buf_pos;	/* current position in input buffer */
+ #if YY_USE_LINENO
+     int yy_buf_line;		/* current line number in buffer */
+ #endif
  
      /* size of input buffer in bytes, not including room for EOB characters */
      int yy_buf_size;	
***************
*** 245,250 ****
--- 255,263 ----
  
  YY_CHAR *yytext;
  int yyleng;
+ #if YY_USE_LINENO
+ int yylineno;
+ #endif
  
  FILE *yyin = (FILE *) 0, *yyout = (FILE *) 0;
  
***************
*** 287,292 ****
--- 300,308 ----
      register yy_state_type yy_current_state;
      register YY_CHAR *yy_cp, *yy_bp;
      register int yy_act;
+ #if YY_USE_LINENO
+     register YY_CHAR *yy_cp2;
+ #endif
  
  %% user's declarations go here
  
***************
*** 452,457 ****
--- 468,477 ----
  		YY_FATAL_ERROR(
  			"fatal flex scanner internal error--no action found" );
  	    }
+ #if YY_USE_LINENO
+ 	    for (yy_cp2 = yy_bp; yy_cp2 < yy_cp; yy_cp2++)
+ 		if (*yy_cp2 == '\n') yylineno++;
+ #endif
  	}
      }
  
***************
*** 622,627 ****
--- 642,651 ----
      if ( yy_cp > yy_bp && yy_cp[-1] == '\n' )
  	yy_cp[-2] = '\n';
  
+ #if YY_USE_LINENO
+     if ( c == '\n' ) yylineno--;
+ #endif
+ 
      *--yy_cp = c;
  
      /* note: the formal parameter *must* be called "yy_bp" for this
***************
*** 695,700 ****
--- 719,728 ----
      c = *yy_c_buf_p;
      yy_hold_char = *++yy_c_buf_p;
  
+ #if YY_USE_LINENO
+     if ( c == '\n' ) yylineno++;
+ #endif
+ 
      return ( c );
      }
  
***************
*** 729,734 ****
--- 757,765 ----
  	*yy_c_buf_p = yy_hold_char;
  	yy_current_buffer->yy_buf_pos = yy_c_buf_p;
  	yy_current_buffer->yy_n_chars = yy_n_chars;
+ #if YY_USE_LINENO
+ 	yy_current_buffer->yy_buf_line = yylineno;
+ #endif
  	}
  
      yy_current_buffer = new_buffer;
***************
*** 752,757 ****
--- 783,791 ----
      {
      yy_n_chars = yy_current_buffer->yy_n_chars;
      yytext = yy_c_buf_p = yy_current_buffer->yy_buf_pos;
+ #if YY_USE_LINENO
+     yylineno = yy_current_buffer->yy_buf_line;
+ #endif
      yyin = yy_current_buffer->yy_input_file;
      yy_hold_char = *yy_c_buf_p;
      }
***************
*** 822,827 ****
--- 856,864 ----
  
      b->yy_ch_buf[0] = '\n';
      b->yy_n_chars = 1;
+ #if YY_USE_LINENO
+     b->yy_buf_line = 1;
+ #endif
  
      /* we always need two end-of-buffer characters.  The first causes
       * a transition to the end-of-buffer state.  The second causes
---- EOF ---- EOF ---- EOF ----