⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 misc.c

📁 生成C++的词法/语法分析的Flex语法分析器
💻 C
📖 第 1 页 / 共 2 页
字号:
int is_hex_digit( ch )int ch;    {    if ( isdigit( ch ) )	return ( 1 );    switch ( clower( ch ) )	{	case 'a':	case 'b':	case 'c':	case 'd':	case 'e':	case 'f':	    return ( 1 );	default:	    return ( 0 );	}    }/* line_directive_out - spit out a "# line" statement */void line_directive_out( output_file_name )FILE *output_file_name;    {    if ( infilename && gen_line_dirs )      {char *p;        fprintf( output_file_name, "#line %d \"",linenum);        for(p=infilename;*p;p++)          if(*p=='"' || *p=='\\') fprintf(output_file_name,"\\%c",*p);          else if(!isprint(*p)) fprintf(output_file_name,"\\%3.3o",*p);          else fprintf(output_file_name,"%c",*p);        fprintf( output_file_name,"\"\n");      }    }/* mk2data - generate a data statement for a two-dimensional array * * synopsis *    int value; *    mk2data( value ); * *  generates a data statement initializing the current 2-D array to "value" */void mk2data( value )int value;    {    if ( datapos >= NUMDATAITEMS )	{	putchar( ',' );	dataflush();	}    if ( datapos == 0 )	/* indent */	fputs( "    ", stdout );    else	putchar( ',' );    ++datapos;    printf( "%5d", value );    }/* mkdata - generate a data statement * * synopsis *    int value; *    mkdata( value ); * *  generates a data statement initializing the current array element to *  "value" */void mkdata( value )int value;    {    if ( datapos >= NUMDATAITEMS )	{	putchar( ',' );	dataflush();	}    if ( datapos == 0 )	/* indent */	fputs( "    ", stdout );    else	putchar( ',' );    ++datapos;    printf( "%5d", value );    }/* myctoi - return the integer represented by a string of digits * * synopsis *    Char array[]; *    int val, myctoi(); *    val = myctoi( array ); * */int myctoi( array )Char array[];    {    int val = 0;    (void) sscanf( (char *) array, "%d", &val );    return ( val );    }/* myesc - return character corresponding to escape sequence * * synopsis *    Char array[], c, myesc(); *    c = myesc( array ); * */Char myesc( array )Char array[];    {    Char c, esc_char;    register int sptr;    switch ( array[1] )	{#ifdef __STDC__	case 'a': return ( '\a' );#endif	case 'b': return ( '\b' );	case 'f': return ( '\f' );	case 'n': return ( '\n' );	case 'r': return ( '\r' );	case 't': return ( '\t' );	case 'v': return ( '\v' );	case '0':	case '1':	case '2':	case '3':	case '4':	case '5':	case '6':	case '7':	case '8':	case '9':	    { /* \<octal> */	    sptr = 1;	    while ( isascii( array[sptr] ) && isdigit( array[sptr] ) )		/* don't increment inside loop control because if		 * isdigit() is a macro it might expand into multiple		 * increments ...		 */		++sptr;	    c = array[sptr];	    array[sptr] = '\0';	    esc_char = otoi( array + 1 );	    array[sptr] = c;	    return ( esc_char );	    }	case 'x':	    { /* \x<hex> */	    int sptr = 2;	    while ( isascii( array[sptr] ) && is_hex_digit( array[sptr] ) )		/* don't increment inside loop control because if		 * isdigit() is a macro it might expand into multiple		 * increments ...		 */		++sptr;	    c = array[sptr];	    array[sptr] = '\0';	    esc_char = htoi( array + 2 );	    array[sptr] = c;	    return ( esc_char );	    }	default:	    return ( array[1] );	}    }/* otoi - convert an octal digit string to an integer value * * synopsis: *    int val, otoi(); *    Char str[]; *    val = otoi( str ); */int otoi( str )Char str[];    {    int result;    (void) sscanf( (char *) str, "%o", &result );    return ( result );    }/* readable_form - return the the human-readable form of a character * * synopsis: *    int c; *    char *readable_form(); *    <string> = readable_form( c ); * * The returned string is in static storage. */char *readable_form( c )register int c;    {    static char rform[10];    if ( (c >= 0 && c < 32) || c >= 127 )	{	switch ( c )	    {	    case '\n': return ( "\\n" );	    case '\t': return ( "\\t" );	    case '\f': return ( "\\f" );	    case '\r': return ( "\\r" );	    case '\b': return ( "\\b" );	    default:		(void) sprintf( rform, "\\%.3o", c );		return ( rform );	    }	}    else if ( c == ' ' )	return ( "' '" );    else	{	rform[0] = c;	rform[1] = '\0';	return ( rform );	}    }/* reallocate_array - increase the size of a dynamic array */void *reallocate_array( array, size, element_size )void *array;int size, element_size;    {    register void *new_array;    /* same worry as in allocate_array(): */    if ( size * element_size <= 0 )        flexfatal( "attempt to increase array size by less than 1 byte" );    new_array =	(void *) realloc( (char *)array, (unsigned) (size * element_size ));    if ( new_array == NULL )	flexfatal( "attempt to increase array size failed" );    return ( new_array );    }/* any_skelout - write out one section of any skeleton  file * * synopsis *    any_skelout(FILE *theskel,FILE *thefile,int *thecounter,char *thename,int use_name,int *back_lf); * * DESCRIPTION *    Copies from theskel to thefile until a line beginning with a single or more "%" or *    EOF is found. * convert @ to the value of lexer_name in name_defined!=0 and use_name!=0 * generate comments and #line (#line only if thecounter!=NULL and  *back_lf!=1) * back_lf indicate that the last characters where backslash-newline * and it forbide #line because #line are not accepted in macro defs * do nothing if thefile==NULL */void any_skelout(theskel,thefile,thecounter,thename,use_name,back_lf)FILE *theskel,*thefile;int *thecounter;char *thename;int use_name;int *back_lf;{int ch; int col=0; if(thefile==0 )    return; if ( gen_line_dirs && thecounter!=(int *)0 && thename !=NULL && *back_lf!=1)       {char *p;        fprintf( thefile, "#line %d \"",*thecounter);        for(p=thename;*p;p++)          if(*p=='"' || *p=='\\') fprintf(thefile,"\\%c",*p);          else if(!isprint(*p)) fprintf(thefile,"\\%3.3o",*p);           else fprintf(thefile,"%c",*p);        fprintf( thefile,"\"\n");      } while((ch=getc(theskel))!=EOF)  {col++;   if(ch=='\\') *back_lf=-1;   else if(ch=='\n' && *back_lf==-1) *back_lf=1;   else if(ch=='%' && *back_lf==1) *back_lf=1;   else *back_lf=0;   if(ch=='@' && use_name)      {if(name_defined==0)        { fprintf(thefile,"@Undefined_name@");          if(thecounter!=(int *)0 && thename !=NULL)            fprintf( stderr, "bad skeleton: %s, line %d\n", thename, *thecounter );          flexfatal("@ found in skeleton before %%name \n");        }      else fprintf(thefile,"%s",lexer_name);     }   else if(ch=='\n')     { putc(ch,thefile);       col=0;       if(thecounter!=(int *)0)          (*thecounter)++;}   else if(col==1 && ch=='%')      {fprintf(thefile,"/* ");      while((ch=getc(theskel))!=EOF && ch!='\n')          putc(ch,thefile);      fprintf(thefile," */ %s\n",(*back_lf==1)?"\\":"");  /* use \\\n to continue macros def */      if(thecounter!=(int *)0)          (*thecounter)++;      return;      }   else putc(ch,thefile);  };}/* action_out - write the actions from the temporary file to lex.yy.c * * synopsis *     action_out(); * *     Copies the action file up to % (or end-of-file) to lex.yy.c */void action_out(){static int bklf=0; any_skelout(temp_action_file,stdout,(int *)0,(char *)0,0,&bklf);}/* skelout - write out one section of the skeleton file * * synopsis *    skelout(); * * DESCRIPTION *    Copies from skelfile to stdout until a line beginning with "%" or *    EOF is found....  */void skelout(){static int skel_line=1,bklf=0; any_skelout(skelfile,stdout,&skel_line,skelname,1,&bklf);}/* header_skeleton_out - write out one section of the skeleton header file * * synopsis *    header_skeleton_out(); * * DESCRIPTION *    Copies from skelheaderfile to headerfile until a line beginning with a single or more "%" or *    EOF is found.... * convert ... */void header_skeleton_out(){static int hskel_line=1,bklf=0;     any_skelout(skelheaderfile,headerfile,&hskel_line,skelheaderfilename,1,&bklf);    }/* transition_struct_out - output a yy_trans_info structure * * synopsis *     int element_v, element_n; *     transition_struct_out( element_v, element_n ); * * outputs the yy_trans_info structure with the two elements, element_v and * element_n.  Formats the output with spaces and carriage returns. */void transition_struct_out( element_v, element_n )int element_v, element_n;    {    printf( "%7d, %5d,", element_v, element_n );    datapos += TRANS_STRUCT_PRINT_LENGTH;    if ( datapos >= 75 )	{	putchar( '\n' );	if ( ++dataline % 10 == 0 )	    putchar( '\n' );	datapos = 0;	}    }/* set lexer name. check redefinition. *//* if name==NULL and not yet defined use default value as if defined *//* include section of sheader skeleton if definition made */void set_lexer_name(name)char *name;{if(name==NULL)   {    if(name_defined==0)     {name_defined++;      fprintf( stderr,"No %%name given before line %d . Assuming %%name %s\n",				 linenum, lexer_name );     } else return;    }  else if(name_defined)    {synerr( "Multiple %%name declaration" );return;}  else    {strncpy(lexer_name,name,sizeof(lexer_name)-1);    name_defined++;   } fprintf(stdout,"#define YY_%s_FLEX_SCANNER\n",lexer_name); if ( ddebug )   fprintf(headerfile, "#define YY_%s_FLEX_DEBUG\n",lexer_name ); if ( csize == 256 )   fprintf(headerfile, "#define YY_%s_CHAR unsigned char\n",lexer_name ); else   fprintf(headerfile, "#define YY_%s_CHAR char\n",lexer_name ); header_skeleton_out();}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -