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

📄 cscanner.txt

📁 一个使用Lex编写的C语言词法加亮的小程序。能将指定的C源程序进行词法加亮
💻 TXT
字号:
/*C语言语法着色器*/
/*作者:RockCarry*/

/*定义部分*/
%{
/*文件包含*/
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*常量定义*/
#define KEY       0  /*关键字*/
#define IDEN      1  /*标识符*/
#define NUMBER    2  /*数字*/
#define STRING    3  /*字符串*/
#define DEFINE    4  /*预处理*/
#define COMMENT   5  /*注释*/
#define OP        6  /*运算符*/
#define ERRORCHAR 7  /*其他符号*/
#define FILENAME  8  /*文件名*/
#define COLOR_NUM 9  /*颜色数*/

/*全局变量定义*/
int   rownum        = 0;    /*当前行号*/
int   colnum        = 0;    /*当前列号*/
int   tabsize       = 4;    /*Tab键大小*/

FILE *outfp         = NULL;
char configfile[30];
char outputfile[30];
char inputfile[30];

/*颜色数组*/
char  colors[COLOR_NUM][10] =
{
    "#0000ff",    /* KEY       */
    "#000000",    /* IDEN      */
    "#ee0000",    /* NUMBER    */
    "#057595",    /* STRING    */
    "#a0009e",    /* DEFINE    */
    "#008800",    /* COMMENT   */
    "#000000",    /* OP        */
    "#000000",    /* ERRORCHAR */
    "#000000"     /* FILENAME  */
};

/*函数声明*/
int  OutPut(int type);
void OutPutHTMLEncodeStr();
void ReadCommand(int argc,char *argv[]);
void LoadColorConfig(char *file);
%}

/*以下是宏定义*/
letter       [A-Za-z_]
D            [0-9]
O            [0-7]
H            [0-9a-fA-f]
suffix       [lLuU]
alnum        [A-Za-z0-9_]
white        [\t\n\040]
iden         {letter}{alnum}*
%start       C_COMMENT
%start       C_INCLUDE

%%
                 /*以下是识别规则部分*/
                 /*识别注释*/
"/*"             {ECHO; BEGIN 0; BEGIN C_COMMENT; fprintf(outfp,"<font color=%s>/*",colors[COMMENT]);; colnum+=yyleng;}
<C_COMMENT>[^*]* |
<C_COMMENT>"*"   {ECHO; OutPutHTMLEncodeStr();}
<C_COMMENT>"*/"  {ECHO; BEGIN 0; fprintf(outfp,"*/</font>\n"); colnum+=yyleng;}


                 /*识别关键字*/
auto             |
break            |
case             |
char             |
const            |
continue         |
default          |
do               |
double           |
else             |
extern           |
float            |
for              |
goto             |
if               |
int              |
long             |
register         |
return           |
short            |
sizeof           |
static           |
struct           |
switch           |
typedef          |
union            |
unsigned         |
void             |
while            {ECHO; OutPut(KEY);}

                            /*识别预处理*/
"#"[\t\040]*"include"       {
                            ECHO; BEGIN C_INCLUDE; 
                            OutPut(DEFINE);
                            }
<C_INCLUDE>["<]{letter}+\.{letter}+[>"] {ECHO; OutPut(FILENAME);}
<C_INCLUDE>\n               {ECHO; BEGIN 0; OutPutHTMLEncodeStr();}

"#"[\t\040]*"define"        |
"#"[\t\040]*"ifdef"         |
"#"[\t\040]*"ifndef"        |
"#"[\t\040]*"endif"         {ECHO; OutPut(DEFINE);}

                 /*识别标识符*/
{iden}           {ECHO; OutPut(IDEN);}

                 /*识别字符串*/
\"(\\.|[^\"])*\" {ECHO; OutPut(STRING);}


                          /*识别数字*/
'.'                       |
'\\.'                     |
'\\{O}({O}{O}?)?'         |
'\\[xX]{H}({H}{H}?)?'     |
0{O}*{suffix}?            |
0[xX]{H}+{suffix}?        |
[1-9]{D}*{suffix}?        |
([1-9]{D}*|{D}+\.{D}*|{D}*\.{D}+)([eE][-+]?{D}+)?[fF]?  {ECHO; OutPut(NUMBER);}

"="              |
"+"              |
"-"              |
"*"              |
"/"              |
"%"              |
"++"             |
"--"             |

"("              |
")"              |
"["              |
"]"              |

"->"             |
"."              |

"&"              |
"|"              |
"~"              |
"^"              |

"!"              |
"&&"             |
"||"             |

"<"              |
"<="             |
"=="             |
">="             |
">"              |
"!="             |

"?"              |
","              |
":"              |
";"              {ECHO; OutPut(OP);}


\n               |
\t               |
\040             {ECHO; OutPutHTMLEncodeStr();}

.                {ECHO; OutPut(ERRORCHAR);}

%%
/*以下是辅助函数部分*/
/*对词法着色并且输出的函数*/
int OutPut(int type)
{
    switch(type)
        {
        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 + -