%{ #include #include #include std::string varname; std::map vars; %} %option c++ %option noyywrap %option yylineno %x DECL_ID %x DECL_SEP %x DECL_VAL %x DECL_EOL %x DECL_VERBATIM %x KEYWORDS %x ADDITIONALCODE WS [ \r\t]+ oWS {WS}? nWS [^ \r\n\t]+ id [a-zA-Z0-9_\-]+ EOL \r?\n %% {oWS}{EOL} { /* ignore empty lines */ } {oWS}%% { BEGIN(KEYWORDS); } {oWS}%\{ { BEGIN(DECL_VERBATIM); } {oWS}%define{WS} { BEGIN(DECL_ID); } {oWS}% { BEGIN(DECL_ID); } {oWS}. { std::cerr << "unexpected character: " << yytext; } {id} { varname = yytext; for(int i = 0; i < varname.size(); i++) if(varname[i] == '_') varname[i] = '-'; BEGIN(DECL_SEP); } {oWS}{EOL} { vars[varname] = "true"; BEGIN(INITIAL); } ([ \t]+|{oWS}=){oWS} { BEGIN(DECL_VAL); } {nWS} { vars[varname] = yytext; BEGIN(DECL_EOL); } {oWS}{EOL} { BEGIN(INITIAL); } {oWS}({nWS}+{oWS})+{EOL} { std::cerr << "ignored text after declaration: " << yytext; BEGIN(INITIAL); } %\} { BEGIN(INITIAL); } (([^%]*)(\%*[^%\}]))* { std::cout << "VERBATIM: " << yytext << std::endl; } {oWS}{EOL} { /* ignore empty lines and \n at end-of-lines */ } ^%%{EOL} { BEGIN(ADDITIONALCODE); } ^[^\r\n]*$ { std::cout << "KEYWORD: " << yytext << std::endl; } {EOL} { } ^[^\r\n]*$ { std::cout << "ADDITIONAL CODE: " << yytext << std::endl; } <> { return 0; } %% #include int main(int argc, char** argv) { vars["delimiters"] = ","; vars["struct-type"] = ""; // ------------ vars["ignore-case"] = "false"; vars["language"] = "ANSI-C"; vars["slot-name"] = "name"; vars["initializer-suffix"] = ""; // ----------- vars["hash-function-name"] = "hash"; vars["lookup-function-name"] = "in_word_set"; vars["class-name"] = "Perfect_Hash"; vars["7bit"] = "false"; vars["compare-lengths"] = "false"; vars["compare-strncmp"] = "false"; vars["readonly-tables"] = "false"; vars["enum"] = "false"; vars["includes"] = "false"; vars["global-table"] = "false"; vars["pic"] = "false"; // ------------ vars["string-pool-name"] = "stringpool"; vars["null-strings"] = "false"; vars["constants-prefix"] = ""; vars["word-array-name"] = "wordlist"; vars["length-table-namename"] = "lengthtable"; vars["switch"] = "0"; vars["omit-struct-type"] = "false"; yyFlexLexer l(std::cin, std::cout); l.yylex(); int w = 0; for(auto i : vars) w = w < i.first.size() ? i.first.size() : w; for(auto i : vars) std::cout << i.first << std::setw(w-i.first.size()+3) << " = " << i.second << std::endl; return 0; }