Написанный мною код для программы, преобразующей Compressed-HTML в HTML (см. подробнее о Compressed-HTML).
compressed-html.cpp
//============================================================================ // Name : compressed-html.cpp // Author : Vasilii Bodnariuk // Version : // Copyright : The MIT License (MIT). Copyright (c) 2015 Bodnariuk Vasilii // Description : compressed-html to html in C++, Ansi-style //============================================================================ #include <iostream> #include <cstdlib> #include <wchar.h> #include "Scanner.h" #include "Parser.h" using namespace std; int main(int argc, char **argv) { try { wchar_t dst[100]; mbstowcs(dst, argv[1], 100); Scanner *scanner = new Scanner(dst); Parser *parser = new Parser(scanner); parser->Parse(); delete parser; delete scanner; cout << argv[1] << " check succeed."<< endl; } catch (...) { cout << "Smth went wrong. Check your PC existence." << endl; } return 0; }
Output.h
/* * Output.h * * Created on: 15 мая 2015 г. * Author: vasilesk */ #ifndef OUTPUT_H_ #define OUTPUT_H_ #include <string> #include <iostream> #include <fstream> #include <stack> class Output_processing { public: Output_processing (); ~Output_processing (); void write (std::string str_to_write); void write (wchar_t *str_to_write); void write_without_quotes (wchar_t *str_to_write); void push_unpaired_tag (std::string tag_to_push); void push_paired_tag (std::string tag_to_push); void set_text_buffer (std::string str_to_set); void write_tag_buffer (); void write_text_buffer (); private: std::ofstream output_file; std::string tag_buffer; std::string text_buffer; std::stack<std::string> tag_stack; }; #endif /* OUTPUT_H_ */
Output.cpp
/* * Output.cpp * * Created on: 15 мая 2015 г. * Author: vasilesk */ #include <string> #include <iostream> #include <fstream> #include "Output.h" using namespace std; Output_processing::Output_processing () { output_file.open("output.html"); } Output_processing::~Output_processing () { output_file.close(); } void Output_processing::write (std::string str_to_write) { output_file << str_to_write; } void Output_processing::write (wchar_t* str_to_write) { wstring ws(str_to_write); string str(ws.begin(), ws.end()); output_file << str; } void Output_processing::write_without_quotes (wchar_t *str_to_write) { wstring ws(str_to_write); string str(ws.begin(), ws.end()); output_file << str.substr(1,str.length()-2); } void Output_processing::push_unpaired_tag (std::string tag_to_push) { tag_stack.push(tag_to_push); } void Output_processing::push_paired_tag (std::string tag_to_push) { tag_stack.push(tag_to_push); tag_stack.push(tag_to_push); } void Output_processing::set_text_buffer (std::string str_to_set) { text_buffer = str_to_set; } void Output_processing::write_tag_buffer () { tag_buffer = tag_stack.top(); tag_stack.pop(); output_file << tag_buffer; } void Output_processing::write_text_buffer () { output_file << text_buffer; }
compressed-html_object.atg
#include#include #include #include "Output.h" using namespace std; COMPILER object Output_processing output; IGNORECASE CHARACTERS CharInLine = ANY - '\r' - '\n'. cr = '\r'. lf = '\n'. tab = '\t'. AnyButDoubleQuote = CharInLine - '\"'. TOKENS String = '"' {AnyButDoubleQuote | "\\\""} '"'. COMMENTS FROM "/*" TO "*/" NESTED COMMENTS FROM "//" TO cr lf IGNORE cr + lf + tab PRODUCTIONS object = "{" [tag {"," tag}] "}". tag = unpaired_tag | paired_tag | text_tag. unpaired_tag = unpaired_list (. output.write ("<"); output.write_tag_buffer(); .) [":" "[" attributes "]"] (. output.write (">"); .). paired_tag = paired_list (. output.write ("<"); output.write_tag_buffer(); .) ":" ["[" attributes "]"] (. output.write (">"); .) object (. output.write (""); output.write_tag_buffer(); output.write (">"); .). text_tag = "#" ":" String (. output.write_without_quotes (t->val); .). attributes = String (. output.write (" ");output.write_without_quotes (t->val); .) ":" (. output.write ("="); .) String (. output.write (t->val); .) {"," String (. output.write (" ");output.write_without_quotes (t->val); .) ":" (. output.write ("="); .) String (. output.write (t->val); .)}. paired_list = "a" (. output.push_paired_tag ("a"); .) | "b" (. output.push_paired_tag ("b"); .) | "body" (. output.push_paired_tag ("body"); .) | "form" (. output.push_paired_tag ("form"); .) | "head" (. output.push_paired_tag ("head"); .) | "html" (. output.push_paired_tag ("html"); .) | "p" (. output.push_paired_tag ("p"); .) | "div" (. output.push_paired_tag ("div"); .) . unpaired_list = "br" (. output.push_unpaired_tag ("br"); .) | "hr" (. output.push_unpaired_tag ("hr"); .) | "img" (. output.push_unpaired_tag ("img"); .) | "input" (. output.push_unpaired_tag ("input"); .) | "link" (. output.push_unpaired_tag ("link"); .) | "meta" (. output.push_unpaired_tag ("meta"); .) . END object .
Parser.cpp
, Parser.h
, Scanner.cpp
, Scanner.h
генерируются с помощью генератора компиляторов Coco/R из файла compressed-html_object.atg
, поэтому здесь не приводятся.