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

📄 test.c

📁 一个使用Lex编写的C语言词法加亮的小程序。能将指定的C源程序进行词法加亮
💻 C
📖 第 1 页 / 共 4 页
字号:
    /* first move last chars to start of buffer */
    number_to_move = yy_c_buf_p - yytext;

    for ( i = 0; i < number_to_move; ++i )
	*(dest++) = *(source++);

    if ( yy_eof_has_been_seen )
	/* don't do the read, it's not guaranteed to return an EOF,
	 * just force an EOF
	 */
	yy_n_chars = 0;

    else
	{
	int num_to_read = YY_BUF_SIZE - number_to_move - 1;

	if ( num_to_read > YY_READ_BUF_SIZE )
	    num_to_read = YY_READ_BUF_SIZE;

	/* read in more data */
	YY_INPUT( (&yy_ch_buf[number_to_move]), yy_n_chars, num_to_read );
	}

    if ( yy_n_chars == 0 )
	{
	if ( number_to_move == 1 )
	    ret_val = EOB_ACT_END_OF_FILE;
	else
	    ret_val = EOB_ACT_LAST_MATCH;

	yy_eof_has_been_seen = 1;
	}

    else
	ret_val = EOB_ACT_RESTART_SCAN;

    yy_n_chars += number_to_move;
    yy_ch_buf[yy_n_chars] = YY_END_OF_BUFFER_CHAR;
    yy_ch_buf[yy_n_chars + 1] = YY_END_OF_BUFFER_CHAR;

    /* yytext begins at the second character in
     * yy_ch_buf; the first character is the one which
     * preceded it before reading in the latest buffer;
     * it needs to be kept around in case it's a
     * newline, so yy_get_previous_state() will have
     * with '^' rules active
     */

    yytext = &yy_ch_buf[1];

    return ( ret_val );
    }


/* yy_get_previous_state - get the state just before the EOB char was reached
 *
 * synopsis
 *     yy_state_type yy_get_previous_state();
 */

static yy_state_type yy_get_previous_state()

    {
    register yy_state_type yy_current_state;
	register unsigned char  *yy_cp;

    yy_current_state = yy_start;

    for ( yy_cp = yytext; yy_cp < yy_c_buf_p; ++yy_cp )
	{
	register unsigned char   yy_c = yy_ec[*yy_cp];
	if ( yy_accept[yy_current_state] )
	    {
	    yy_last_accepting_state = yy_current_state;
	    yy_last_accepting_cpos = yy_cp;
	    }
	while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
	    {
	    yy_current_state = yy_def[yy_current_state];
	    if ( yy_current_state >= 494 )
		yy_c = yy_meta[yy_c];
	    }
	yy_current_state = yy_nxt[yy_base[yy_current_state] + yy_c];
	}

    return ( yy_current_state );
    }


static void yyunput( int c, register unsigned char  *yy_bp )
    {
	register unsigned char  *yy_cp = yy_c_buf_p;

    *yy_cp = yy_hold_char; /* undo effects of setting up yytext */

    if ( yy_cp < yy_ch_buf + 2 )
	{ /* need to shift things up to make room */
	register int number_to_move = yy_n_chars + 2; /* +2 for EOB chars */
	register unsigned char  *dest = &yy_ch_buf[YY_BUF_SIZE + 2];
	register unsigned char  *source = &yy_ch_buf[number_to_move];

	while ( source > yy_ch_buf )
	    *--dest = *--source;

	yy_cp += dest - source;
	yy_bp += dest - source;

	if ( yy_cp < yy_ch_buf + 2 )
	    YY_FATAL_ERROR( "flex scanner push-back overflow" );
	}

    if ( yy_cp > yy_bp && yy_cp[-1] == '\n' )
	yy_cp[-2] = '\n';

    *--yy_cp = c;

    YY_DO_BEFORE_ACTION; /* set up yytext again */
    }


static int input()

    {
    int c;
	unsigned char  *yy_cp = yy_c_buf_p;

    *yy_cp = yy_hold_char;

    if ( *yy_c_buf_p == YY_END_OF_BUFFER_CHAR )
	{ /* need more input */
	yytext = yy_c_buf_p;
	++yy_c_buf_p;

	switch ( yy_get_next_buffer() )
	    {
	    /* this code, unfortunately, is somewhat redundant with
	     * that above
	     */
	    case EOB_ACT_END_OF_FILE:
		{
		if ( yywrap() )
		    {
		    yy_c_buf_p = yytext;
		    return ( EOF );
		    }

		yy_ch_buf[0] = '\n';
		yy_n_chars = 1;
		yy_ch_buf[yy_n_chars] = YY_END_OF_BUFFER_CHAR;
		yy_ch_buf[yy_n_chars + 1] = YY_END_OF_BUFFER_CHAR;
		yy_eof_has_been_seen = 0;
		yytext = yy_c_buf_p = &yy_ch_buf[1];
		yy_hold_char = *yy_c_buf_p;

		return ( input() );
		}
		break;

	    case EOB_ACT_RESTART_SCAN:
		yy_c_buf_p = yytext;
		break;

	    case EOB_ACT_LAST_MATCH:
		YY_FATAL_ERROR( "unexpected last match in input()" );
	    }
	}

    c = *yy_c_buf_p;
    yy_hold_char = *++yy_c_buf_p;

    return ( c );
    }


static void yyrestart( FILE *input_file )
    {
    if ( yyin != stdin )
	fclose( yyin );

    yyin = input_file;
    yy_init = 1;
    }
# line 182 "cscanner.txt"

/*以下是辅助函数部分*/
/*对词法着色并且输出的函数*/
int OutPut(int type)
{
    switch(type)
        {
		case OP:
		case ERRORCHAR:
			OutPutHTMLEncodeStr();
			break;
        default:
            fprintf(outfp,"<font color=%s>",colors[type]);
            OutPutHTMLEncodeStr();
            fprintf(outfp,"</font>");
        }
    return 0;
}

/*将字符串进行HTML编码并输出的函数*/
void OutPutHTMLEncodeStr()
{
    int i = 0;
	int j;

    while(yytext[i]!='\0')
        {
        switch(yytext[i])
            {
            case '<':
				colnum++;
                fprintf(outfp,"&lt;");
                break;
            case '>':
				colnum++;
                fprintf(outfp,"&gt;");
                break;
            case ' ':
				colnum++;
                fprintf(outfp,"&nbsp;");
                break;
            case '\n':
				colnum = 0;
				rownum++;
                fprintf(outfp,"<BR>\n");
                break;
			case '\t':
				colnum=(colnum/4+1)*4;
				for(j=colnum;j<(colnum/4+1)*4;j++)
					fprintf(outfp,"&nbsp;");
				break;
            default:
				colnum++;
                fprintf(outfp,"%c",yytext[i]);
            }
        i++;
        }
}

/*处理命令行输入的函数*/
void ReadCommand(int argc,char *argv[])
{
	int   i;

	for(i=0;i<argc;i++)
		{
		if(argv[i][0]=='-')
			{
			switch(argv[i][1])
				{
				case 'c':
					strcpy(configfile,argv[++i]);
					break;
				case 't':
					tabsize = sscanf(argv[++i],"%d",&tabsize);
					break;
				case 'o':
					strcpy(outputfile,argv[++i]);
					break;
				case 'i':
					strcpy(inputfile,argv[++i]);
					break;
				default:
					printf("无效的命令行参数:%s\t%s",argv[i],argv[i+1]);
					exit(0);
				}
			}
		else strcpy(inputfile,argv[i]);
		}

	if(configfile != NULL)
		LoadColorConfig(configfile);
	if(inputfile == NULL)
		strcpy(inputfile,"rock.c");
	if(outputfile == NULL)
		strcpy(outputfile,"output.htm");
}

/*装载颜色配置文件的函数*/
void LoadColorConfig(char *file)
{
	int   i;
	FILE *configfp = NULL;

	configfp = fopen(file,"r");
	if(configfp==NULL)
		{
		printf("打开颜色配置文件:%s失败!",file);
		getch();
		exit(0);
		}

	for(i=0;i<COLOR_NUM;i++)
		fscanf(configfp,"%s\n",colors[i]);
}

/*主函数*/
void main(int argc,char *argv[])
{
	printf("C语言词法着色器 V0.1\n");
	printf("作者:RockCarry\n");
	printf("日期:2005.7.14\n");
	printf("RockCarry工作室\n");
	printf("Http://rockcarry.home.sunbo.net\n\n");

	if(argc>1)
		ReadCommand(argc,argv);
	else{
		printf("请输入C文件名:");
		scanf("%s",inputfile);

		printf("请输入输出文件名(*.htm):");
		scanf("%s",outputfile);

		printf("请输入颜色配置文件名(0-默认):");
		scanf("%s",configfile);
		if(strcmp(configfile,"0")!=0)
			LoadColorConfig(configfile);

		printf("请输入TABSIZE:");
		scanf("%d",&tabsize);
		}

    yyin  = fopen(inputfile,"r");
	if(yyin==NULL)
		{
		printf("打开输入文件:%s失败!\n",inputfile);
		getch();
		exit(0);
		}

    outfp = fopen(outputfile,"w");
	if(outfp==NULL)
		{
		printf("建立输出文件:%s失败!\n",outputfile);
		getch();
		exit(0);
		}

	printf("\n正在进行着色处理...\n");
    while(yylex()!=0);
	printf("\n词法着色处理完毕,一共处理源程序 %d 行\n",rownum);

    getch();
    fclose(outfp);
    fclose(yyin);
}




⌨️ 快捷键说明

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