发博文
无效空间

http://blog.chinaunix.net/space.php?uid=20362799

空间欠费, 拒绝访问, 请返回.   
个人资料
  • 博客访问:54529
  • 博文数量:17
  • 博客积分:1415
  • 博客等级:上尉
  • 注册时间:2006-01-13 18:40:29
订阅我的博客
  • 订阅
  • 订阅到鲜果
  • 订阅到抓虾
  • 订阅到Google
字体大小: 博文
分类: yacc&lex

%{
#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;
}

我的更多文章
亲,您还没有登录,请[登录][注册]后再进行评论