- 论坛徽章:
- 0
|
%{
#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 编辑 ] |
|