免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
123下一页
最近访问板块 发新帖
查看: 10857 | 回复: 26
打印 上一主题 下一主题

[C] CU论坛C语言代码语法高亮工具 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2007-01-02 03:34 |只看该作者 |倒序浏览
%{
#include <ctype.h>
#include <stdio.h>
#include <string.h>

//格式串定义
const char *fmt_number = "[""color=Red]%s""[""/color]";
const char *fmt_keyword = "[""color=Blue]%s""[""/color]";
const char *fmt_ctype = "[""color=Purple]%s""[""/color]";
const char *fmt_lcomment = "[""color=LimeGreen]%s""[""/color]";
const char *fmt_bcomment1 = "[""color=LimeGreen]";         ///!!
const char *fmt_bcomment2 = "[""/color]";                  ///!!
const char *fmt_string = "[""color=Orange]%s%c""[""/color]";   ///!!
const char *fmt_char = "[""color=Navy]%s%c""[""/color]";       ///!!
const char *fmt_normal = "%s";          //default: black
const char *fmt_preproc = "[""color=Brown]%s""[""/color]";
%}

digit           [0-9]
xdigit          [0-9a-fA-F]
odigit          [0-7]

decnum          (0(\.{digit}+)?)|([1-9]{digit}*(\.{digit}+)?)
octnum          0{odigit}+
hexnum          0(x|X){xdigit}+
number          {decnum}|{octnum}|{hexnum}
lcomment        \/\/.*
string          \"[^"]*
char              \'[^']*
normal          [a-zA-Z_]+[a-z0-9A-Z_]*
preproc         #.*

keyword1        break|case|continue|default|do|else|enum
keyword2        extern|for|goto|if|return|sizeof|struct
keyword3        switch|typedef|union|volatile|while
keyword4        catch|class|delete|friend|inline|new|operator
keyword5        private|protected|public|template|this|throw|try|virtual
ctype1          auto|char|const|double|float|int|long|register
ctype2          short|signed|static|unsigned|void|bool

keyword         {keyword1}|{keyword2}|{keyword3}|{keyword4}|{keyword5}
ctype           {ctype1}|{ctype2}

%x comment

%%

"/*" {
        printf(fmt_bcomment1);
        ECHO;
        BEGIN(comment);
}
        <comment>[^*\n]* ECHO;
        <comment>"*"+[^*/\n]* ECHO;
        <comment>\n ECHO;
        <comment>"*"+"/" {
                BEGIN(INITIAL);
                ECHO;
                printf(fmt_bcomment2);
        }

{number} {
        printf(fmt_number, yytext);
}
{keyword} {
        printf(fmt_keyword, yytext);
}
{ctype} {
        printf(fmt_ctype, yytext);
}
{lcomment} {
        printf(fmt_lcomment, yytext);
}
{string} {
        if (yytext[yyleng - 1] == '\\') {
                int i;
                int more = 1;
                for (i = yyleng - 2; i >= 0; i--) {
                        if (yytext[ i ] == '\\') more = !more;
                        else break;
                }
                if (more) yymore();
                else printf(fmt_string, yytext, input());
        } else {
                printf(fmt_string, yytext, input());
        }
}
{char} {
        if (yytext[yyleng - 1] == '\\') {
                int i;
                int more = 1;
                for (i = yyleng - 2; i >= 0; i--) {
                        if (yytext[ i ] == '\\') more = !more;
                        else break;
                }
                if (more) yymore();
                else printf(fmt_char, yytext, input());
        } else {
                printf(fmt_char, yytext, input());
        }
}
{normal} {
        printf(fmt_normal, yytext);
}
{preproc} {
        printf(fmt_preproc, yytext);
}

%%

void printhelp()
{
        char *h =
        "cucolor 0.2\n"
        "a C/C++ code syntax highlighting utility for chinaunix forum\n"
        "copyright (c) 2007 nully\n"
        "this is GPLed freeware\n\n"
        "Usage: cucolor [input file] [output file]\n\n";

        fprintf(stderr, "%s\n", h);
}

int main(int argc, char **argv)
{
        if (argv[1]) {
                if (strcmp(argv[1], "--help") == 0 ||
                    strcmp(argv[1], "-h") == 0) {
                        printhelp();
                        return 0;
                }
                if (freopen(argv[1], "r", stdin) == NULL) {
                        fprintf(stderr, "cannot open input file %s\n", argv[1]);
                        return -1;
                }
        }
        if (argv[2]) {
                if (freopen(argv[2], "w", stdout) == NULL) {
                        fprintf(stderr, "cannot open output file %s\n", argv[1]);
                        return -1;
                }
        }

        yylex();
        return 0;
}

int yywrap()
{
        return 1;
}


====================================
保存为 cucolor.lex
编译:
$ lex cucolor.lex
$ gcc lex.yy.c -o cucolor

已确认论坛代码完整,原附件被删。

[ 本帖最后由 nully 于 2007-1-3 15:56 编辑 ]

论坛徽章:
0
2 [报告]
发表于 2007-01-02 03:53 |只看该作者
编译:
$ lex cucolor.lex
$ gcc -o cucolor lex.yy.c

论坛徽章:
0
3 [报告]
发表于 2007-01-02 07:50 |只看该作者
好。

原创?辛苦了。下一步可以尝试把 RTF 转换为 Discuz! 代码。

P.S.

给条建议,把 formater 改成 syntax highlighting utility 吧。formater 也有词法错误,应作 formatter,双写 t

[ 本帖最后由 langue 于 2007-1-2 08:00 编辑 ]

论坛徽章:
0
4 [报告]
发表于 2007-01-02 08:44 |只看该作者
是不是以后的代码支持语法高亮了?
厉害啊
造福全人类了

论坛徽章:
0
5 [报告]
发表于 2007-01-02 10:56 |只看该作者
原帖由 langue 于 2007-1-2 07:50 发表
好。

原创?辛苦了。下一步可以尝试把 RTF 转换为 Discuz! 代码。

P.S.

给条建议,把 formater 改成 syntax highlighting utility 吧。formater 也有词法错误,应作 formatter,双写 t


哈哈,本人英语不好。。。
已更改好。。。


这是刚学lex后写的,用于匹配的正则表达式还写得不是很好。。。。

论坛徽章:
0
6 [报告]
发表于 2007-01-02 11:13 |只看该作者
discuz!代码没有“转义的么”?
想把[ .color = XXX] 加入代码不容易啊。。。
搞得格式化串里要这么写。。。。

论坛徽章:
0
7 [报告]
发表于 2007-01-02 11:47 |只看该作者
我有自己用ASP写的一个,感觉还可以,不过就是有点小毛病。什么时候高兴了就发出来,可惜是ASP的.

论坛徽章:
0
8 [报告]
发表于 2007-01-02 23:28 |只看该作者
厉害啊
造福大家阿

论坛徽章:
0
9 [报告]
发表于 2007-01-03 11:08 |只看该作者
顶文师兄一个再说。

论坛徽章:
0
10 [报告]
发表于 2007-01-03 16:03 |只看该作者
看了bison手册。。。
原来的块注释有问题,这里替换了手册里的例子,呵呵
加入了CPLUSSULP的几个关键字

PS: 谁知道
keyword   auto|break|continue...这个要怎么换行?

PPS:
Discuz!代码要怎么转义?如 [ i ] (不含空格)这些东西都被吃掉了。。。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP