代码高亮显示(编译原理课程设计)

编译原理的课程设计,这样可以算结束了吧??

代码直接复制到网页上是没高亮颜色的

cmd输入*.CPP文件,自动生成对应的*.html文件。转换后效果如下:

使用的xhtml,css样式

当然bug无数,不过眼不见心不烦啦~

/*

*    CtoHTML.cpp

*

* Created on: 2011/08/30

* Author: art

*/

/*

    标识符 用户定义的常量、类型、变量、过程名

    常量 12, 1997, 4.14, ‘A’, 等

    运算符 +,-,*,/,>,<>,!=,#等

    界限符 ; , ( ) 等

*/

#include <fstream>

#include <string>

using namespace std;

void CtoHTML( string &s );

bool Lighten_Comment( string &s );

void Lighten_Operator ( string &s);

void replacechar( string &s );

//    保留字 AND,BEGIN,FOR,TYPE,VAR等

void Lighten_Keyword( string &s );

bool string_replace(string & strBig, const string & strsrc,

                    const string & strdst );

string keyword[] = {

"auto ", "bool ", "break ", "string "

"case ", "catch ","char ", "const ", "continue ",

"define ","delete ","do ","double ", "else ",

"enum ", "extern ", "false ", "finally ", "float ",

"for ", "goto ", "if ", "include " ,"inline ", "int ", "long ",

"namespace ","new ","printf ","pi ","register","return ", "short ",

"signed ", "sizeof ", "static ", "struct ","switch ",

"template ", "this ", "throw ", "true ", "try ","typedef ",

"typeid ", "typename ", "union ", "unsigned ", "using ", "virtual ","void ","while "

};

string Operator[] = {

    "++", "--", "!=", "==", "#", "(", ")", "\\", ",", "[", "]"

};

int main(int argc, char** argv)

{

    while ( --argc != 0 ) {

        string oldname = *++argv;    // get file's name

        ifstream fin( oldname.c_str() );

//        assuer(fin, name);

        string newfile = oldname + ".html";

        ofstream fout( newfile.c_str() );

        

        string str = "<html xmlns=\"\"><head><meta http-equiv=\"Content-Type\

         \" content=\"text/html; charset=utf-8\" />\

            <title>Cpp2HTML</title><style type=\"text/css\">\

            <!--\

            body,td,th {font-family: YaHei Consolas Hybrid;font-size: 12px;}\

            .tab{text-indent: 2em;}\

            .KEYWORD {color: #3333FF}\

            .COMMENT {color: #33CC00}\

            .OPERATOR {color: #FF0000}\

            .DQUOTE {color: ##00FFFF}\

            -->\

            </style></head><body>";

        fout << str << "

        while ( getline( fin, str ) ) {

            // cpp to html ...

            CtoHTML( str );

            fout << "

        }    

        fout << "

        

        fin.close();

        fout.close();

        

        system(newfile.c_str())// open *.html by browser

    }

    return 0;

}

void CtoHTML( string &s ) {

    // < &alt >

    replacechar( s );

    Lighten_Operator( s );

    // int float

    Lighten_Keyword( s );

    // function

}

void Lighten_Operator( string &s ) {

    if ( !Lighten_Comment( s ) ) {    // be sure operator isn't in comment

        int num = sizeof(Operator)/sizeof(string);

        for ( int i = 0; i != num; ++) {

            string tmp = "<span class=\"OPERATOR\">" + Operator[i] + "</span>";

            string_replace( s, Operator[i], tmp );

        }

    }

}

bool Lighten_Comment ( string &s ) {

    // // /*...*/

    // 76 150 108

    // 4c 96 6C

    static bool comment_flag = 0;    

    if ( 1 == comment_flag ) {

        if ( string_replace( s, "*/", "<span class=\"COMMENT\">*/" ) ) {

            s += "</span>";    

            comment_flag = 0;

        } else {

            s.insert(0, "<span class=\"COMMENT\">");

            s += "</span>";

        }

    } else {

        if ( string_replace( s, "//", "<span class=\"COMMENT\">//" ) ) {

            s += "</span>";

        } else if ( string_replace( s, "/*", "<span class=\"COMMENT\">/*" ) ) {        

            s += "</span>";

            comment_flag = 1;

        }

    }

    return comment_flag;

}

void Lighten_Keyword( string &s ) {

    int num = sizeof(keyword)/sizeof(string);

    for ( int i = 0; i != num; ++) {

        string tmp = "<span class=\"KEYWORD\">" + keyword[i] + "</span>";

        string_replace( s, keyword[i], tmp );

        

    }

}

void replacechar( string &s ) {

    string_replace(s, "<", "<" );

    string_replace(s, ">", ">" );

    string_replace(s, "\t", "    ");

    // 键盘的Tab键

}

bool string_replace(string & strBig, const string & strsrc,

                    const string & strdst ) {

    bool flag = 0;

    string::size_type pos=0;

    string::size_type srclen = strsrc.size();

    string::size_type dstlen = strdst.size();

    while ( ( ( pos = strBig.find(strsrc, pos ) ) != string::npos ) ) {

        if ( ( strsrc == "/*" || strsrc == "//" )

            && ( strBig[strBig.find( strsrc, pos ) - 1] == '"' )

            || ( strBig[strBig.find( strsrc, pos ) + 2] == '"' )) {

            ;

        } else {

            strBig.replace( pos, srclen, strdst );

            flag = 1;

        }

        pos += dstlen;

    }

    

    return flag;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值