%{ #include #include #include using namespace std; long posy, counter = 1; map ind; map notes; template T cut(const char* s); %} %option outfile="footnotes.cpp" c++ 8bit warn noyywrap yylineno full %x FOOT TAIL %% . { cout << YYText(); } \[[[:digit:]]+\] { ind.insert(pair(cut(YYText()), counter)); cout << "[" << counter++ << "]"; } ^"@footnote:"\n { cout << YYText(); counter = 1; } BEGIN(FOOT); ^\[[[:digit:]]+\] { posy = ind[cut(YYText())]; } BEGIN(TAIL); .*\n { notes[posy] = string(YYText()); } BEGIN(FOOT); %% int main(long argc, char* argv[]) { if (argc == 1) freopen(argv[1], "r", stdin); yyFlexLexer flex; while (flex.yylex()); for (map::iterator b = notes.begin(), e = notes.end(); b != e; ++b, ++counter) cout << "[" << b->first << "]" << b->second; } template T cut(const char* s) { T ret = NULL; sscanf(s, "[%d]", &ret); return ret; }