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