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

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

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

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

使用的xhtml,css样式

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

/*

*    CtoHTML.cpp

*

* Created on: 2011/08/30

* Author: art

*/

/*

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

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

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

    界限符 ; , ( ) 等

*/

#include 

#include 

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 = "\"\">\"Content-Type\

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

            Cpp2HTML";

        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 = "\"OPERATOR\">" + Operator[i] + "";

            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, "*/", "\"COMMENT\">*/" ) ) {

            s += "";    

            comment_flag = 0;

        } else {

            s.insert(0, "\"COMMENT\">");

            s += "";

        }

    } else {

        if ( string_replace( s, "//", "\"COMMENT\">//" ) ) {

            s += "";

        } else if ( string_replace( s, "/*", "\"COMMENT\">/*" ) ) {        

            s += "";

            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 = "\"KEYWORD\">" + keyword[i] + "";

        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;

}

你可能感兴趣的:(个人日记)